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?
    }
}
Fergoso
  • 1,584
  • 3
  • 21
  • 44
  • 1
    Short answer: No. – NathanOliver Jul 08 '21 at 17:48
  • 1
    No, there is no way to do that. C++ does not work this way. – Sam Varshavchik Jul 08 '21 at 17:48
  • 1
    You can template the function over a size type and pass a reference [like so](https://stackoverflow.com/a/62201889/12334309). 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. – Nathan Pierson Jul 08 '21 at 17:48
  • Thank you so much for your answers. +1's – Fergoso Jul 08 '21 at 17:49
  • 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. – Drew Dormann Jul 08 '21 at 17:53
  • 2
    You *could* make `infoArray` a global variable. But I'm not saying that would be a *good* thing to do. – Adrian Mole Jul 08 '21 at 18:03
  • you can print it right in `main` without passing anything – Aykhan Hagverdili Jul 08 '21 at 18:20
  • Because the `infoArray` is a local variable, one would at least need to store a pointer to it somewhere. For example, if there were a global variable `float* printMe` then you could set `printMe = infoArray` before calling `PrintReport`, and have `PrintReport` access `printMe`, which would access the values of `infoArray` indirectly. It is certainly not good style to do so, though. In your case it may make more sense to create a class that stores all three floats associated with a single array index and have a `std::vector` of that class. – Wyck Jul 08 '21 at 18:26

1 Answers1

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
IwillbeBetter
  • 83
  • 1
  • 4