printing an array in fields

i am trying to get my array userVals[] to print in 5 columns for rows for it has twenty values entered into it?
1
2
3
4
5
6
7
8
9
10
11
12
void everything(float userVals[], float total ,float average){
    int count;
    for (int count= 0; count< 20; count++){
        cout<< userVals[count]<< " ";
        
        if (count == 5){
            cout << endl;
        count=0;
        }
        
        
    }
Last edited on
You're resetting count to 0 on every fifth iteration of the loop, which means the condition count< 20 will never stop being true.

Why not just check if count is divisible by 5?
Last edited on
okey i fixed it an the whole program is running good except the last output number

[code]
#include <iostream>
#include <cctype>
#include <iomanip>

using namespace std;
/********************PROTOTYPES**************************/
void getValues(float userVals[], const int number);
float getTotal (float userVals[], const int number);
float getAverage(const int number, float total);
void everything(float userVals[], float total, float average);

/***********************************************************/
int main(){
float userVals[20];
const int number = 20;
float total;
float average;

getValues(userVals, number);
total = getTotal(userVals, number);
average = getAverage(number,total);
everything(userVals, total, average);

return 0;
}

/**********************Functions************************/

void getValues(float userVals[], const int number){

int i = 0;
for (i = 0; i < number; ++i) {
cout << "Enter 20 integer values greater then zero and less than 100 " << endl;
cin >> userVals[i];
if (userVals[i] < 0 || userVals[i] > 100 ){
cout << "You have entered an invalid number, please enter another!" << endl;
i--;
}
else
continue;
}
}
float getTotal (float userVals[], const int number){
float total;
total = 0;
int index;
for(index=0; index < number; index++){
total = total + userVals[index];

}

return total;
}
float getAverage(const int number, float total){
float average;
average = total / number;

return average;
}
void everything(float userVals[], float total ,float average){

for (int count= 1; count<= 20; count++){
cout<< userVals[count] << " ";

if (count %5 == 0){
cout<< endl;

}

}
cout<< endl;
cout << "The total is " << total << endl;
cout << "the average is " << average<< endl;
}
[code/]
Topic archived. No new replies allowed.