0

Is there a way to print the values of infoArray from PrintReport() without passing the infoArray as a parameter?

int main() {
    int sizeOfArray = 3;
    float subjectP[sizeOfArray];
    float subjectQ[sizeOfArray];
    float infoArray[sizeOfArray];
    
    InputMarks(subjectP, sizeOfArray);
    InputMarks(subjectQ, sizeOfArray);
    
    StoreData(subjectP, subjectQ, infoArray, sizeOfArray, 'G');
    StoreData(subjectP, subjectQ, infoArray, sizeOfArray, 'T');

    PrintReport(subjectP, subjectQ, sizeOfArray);
    
    return 0;
}

void StoreData(float value1[], float value2[], float value3[], int sizeOfArray, char mode) {
    for(int i=0; i<sizeOfArray; i++) {
        float val1 = value1[i];
        float val2 = value2[i];
        switch(mode) {
            case 'G':
                value3[i] = MaxMark(val1, val2);
                break;
            case 'T':
                value3[i] = TotalMark(val1, val2);
                break;
            default : 
                cout << "invalid mode";
        }
    }
}

void PrintReport(float value1[], float value2[], int sizeOfArray) {
    cout << setw(10) << left << "Class";
    cout << setw(10) << left << "Subject P";
    cout << setw(10) << left << "Subject Q";
    cout << setw(10) << left << "Min";
    cout << setw(10) << left << "Max" << endl;
    
    for(int i=0; i<sizeOfArray; i++) {
        cout << setw(10) << left << "A";
        cout << setw(10) << left << value1[i];
        cout << setw(10) << left << value2[i];
        /// How to get infoArray[i] values here?
    }
}
8
  • 1
    Short answer: No. Commented Jul 8, 2021 at 17:48
  • 1
    No, there is no way to do that. C++ does not work this way. Commented Jul 8, 2021 at 17:48
  • 1
    You can template the function over a size type and pass a reference like so. It's probably easier to just switch to using std::array or std::vector if you really want to pass things to functions smoothly, though. Commented Jul 8, 2021 at 17:48
  • 3
    What issues are you having? It's not clear what problem you are trying to solve. Why is passing the array as a parameter undesirable? There could be good answers if the question were clearer. Commented Jul 8, 2021 at 17:53
  • 2
    You could make infoArray a global variable. But I'm not saying that would be a good thing to do. Commented Jul 8, 2021 at 18:03

1 Answer 1

3

You can either make the variable infoArray[] global, or make use of classes.

I recommend using a class, making the variable infoArray be a private member. You can access it within the class, and you have the possibility to create a public getter for that variable, if needed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.