1

Program:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//Structure
struct Student{
    string name;
    int pMark;
    int eMark;
    double avg;
    string result;
};

//Functions
Student info(int arrSize, Student arrStudent[10]);
void display(int arrSize, Student arrStudents[10]);

//Main program
int main() {
    Student arrStudents[10];
    int arrSize;

    cout << "How many Students are there(max 10): ";
    cin >> arrSize;

    info(arrSize, arrStudents);
    display(arrSize, arrStudents);

    return 0;
}

//Student Function
Student info(int arrSize, Student arrStudent[10]){
    int counter = 0;
    while(counter < arrSize){
        cout << "\nStudent " << (counter + 1) << " info:";
        cout << "\nEnter the name: ";
        cin >> arrStudent[counter].name;
        cout << "Enter the participation mark: ";
        cin >> arrStudent[counter].pMark;
        cout << "Enter the exam mark: ";
        cin >> arrStudent[counter].eMark;

        arrStudent[counter].avg = (arrStudent[counter].pMark + arrStudent[counter].eMark) / 2.00;

        if (arrStudent[counter].avg >= 50) {
            arrStudent[counter].result = "Pass";
        }
        else {
            arrStudent[counter].result = "Fail";
        }
        counter++;
    }

    return arrStudent;//(Return Array)?
}

//Display Function
void display(int arrSize, Student arrStudents[10]) {
    cout << endl << "Name\t\t Average\t\t Result" << endl;

    for (int counter = 0; counter < arrSize; counter++) { 
        cout << arrStudents[counter].name << "\t\t"
             << fixed << setprecision(2) 
             << arrStudents[counter].avg << "\t\t\t"
             << arrStudents[counter].result << endl;
    }
}

I tried using the function as such, but I'm not sure if it's correct?

//Student Function
Student info(int arrSize, Student arrStudent[10]){
    int counter = 0;
    while (counter < arrSize) {
        cout << "\nStudent " << (counter + 1) << " info:";
        cout << "\nEnter the name: ";
        cin >> arrStudent[counter].name;
        cout << "Enter the participation mark: ";
        cin >> arrStudent[counter].pMark;
        cout << "Enter the exam mark: ";
        cin >> arrStudent[counter].eMark;

        arrStudent[counter].avg = (arrStudent[counter].pMark + arrStudent[counter].eMark) / 2.00;

        if (arrStudent[counter].avg >= 50) {
            arrStudent[counter].result = "Pass";
        }
        else {
            arrStudent[counter].result = "Fail";
        }
        counter++;
    }
    return arrStudent[arrSize];
}

I'm new to coding(In University) so we still need to learn about vectors, pointers and references. That's why I haven't tried any other methods. I would highly appreciate the solutions if it is possible to solve it by avoiding those methods.

Chris
  • 26,361
  • 5
  • 21
  • 42
WD de Wet
  • 25
  • 4
  • Your loop will add all of the information collected directly into the array passed into the function. You need not return anything. I do recommend adding some checking to the user input to catch simple mistakes like the user inputting "banana" instead of a valid mark, though. Eg `cin >> xxx;` becomes `if (!(cin >> xxx)) { cerr << ""Bad user input. Quitting.\n"; exit (-1);}` Sucks when the program accepts impossible values and tries to use them anyway. Better to catch the mistake and do something about it rather than debugging the program later. – user4581301 Sep 01 '22 at 23:19
  • Thank you! I would usually add input checks but we have limited time to do these so our lecturer said that input checks are not compulsory for now. Any other advice will be appreciated. – WD de Wet Sep 01 '22 at 23:41

1 Answers1

0

You are passing values of array by value, you should pass them by referance, thus you should use pointer,

Example function:

Student info(int arrSize, int *arrStudent)
  • Interesting fun fact: One cannot easily pass arrays by value. [They always decay](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay). Example: https://godbolt.org/z/PvMPGP1xf Note the emitted sizes of the array: 800 where it is still an array, and 8 where it has been decayed to a pointer. Also note the compiler warning announcing `sizeof` is measuring a pointer, not an array. This makes the advice of this answer not useful. Plus it doesn't address the problem the asker asked about: returning the `Student`. – user4581301 Sep 02 '22 at 19:20