-1

I try to save declare a double array in C++ on Xcode.

double array[size];

But when I print the values,

for(int i=0; i<size; i++){
    cout<<array[i]<<"  ";
}

It's printing integers.

void display(int grade[][size_of_qz]) {

    double stAve[size_of_st];
    computeStAve(grade, stAve);

    cout << "Student      Ave         Quizes\n";
    for (int row = 0; row < size_of_st; row++) {
        cout << row + 1 << "        " << stAve[row] << "          ";
        for (int col = 0; col < size_of_qz; col++) {
            cout << grade[row][col] << "  ";
        }
        cout << endl;
    }
}

void computeStAve(int grade[][size_of_qz], double stAve[]) {
    int temp = 0;
    for (int row = 0; row < size_of_st; row++) {
        for (int col = 0; col < size_of_qz; col++) {
            temp += grade[row][col];
        }
        stAve[row] = temp / size_of_qz;
        temp = 0;
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

2

Looks like this line is the cause of your problems:

stAve[row]=temp/size_of_qz;

It is the same as: double = (int / int). Your division is getting converted to an int. You need to do something like:

stAve[row]=(double)temp/size_of_qz;
g-radam
  • 527
  • 1
  • 7
  • 20
-2

If you are expecting to print a certain number of digits, start your main function with a

    std::cout << std::fixed << std::setprecision(2); 

and the library to include would be iomanip.

Drimik Roy
  • 17
  • 2
  • 2
    The default C++ precision is 6, changing it to 2 is not going to change the fact that he's seeing integers when expecting floats. – ChrisD Oct 22 '18 at 03:43
  • right, that's my bad. misinterpreted the question. Thanks for the feedback – Drimik Roy Oct 23 '18 at 04:25