array
i cant seem to find the way on how to find the avg going down for the students how i want it to run is all the way in the bottom
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#include <iostream>
#include <iomanip>
using namespace std;
const int STU_SIZE=5;
const int TEST_SIZE=3;
void showTable(const int ar[][TEST_SIZE]);
int main()
{
int ar[STU_SIZE][TEST_SIZE];
showTable(ar);
return 0;
}
void showTable(const int ar[][TEST_SIZE])
{
int total=0;
double avg;
cout << setw(10) << "Test#" << setw(10) << "Stu 1" << setw(10) << "Stu 2" << setw(10) << "Stu 3"
<< setw(10) << "Stu 4" << setw(10) << "Stu 5" << setw(10) << "Average" << endl;
for(int test = 0; test < TEST_SIZE; test++)
{
cout<< setw(10)<< test+1;
for(int score = 0; score < STU_SIZE; score++)
{
cout<< setw(10)<< ar[score][test];
total+=ar[score][test];
avg=(double)total/STU_SIZE;
}
cout<<fixed<<setprecision(1)<<setw(10)<<avg;
total=0;
cout<<endl;
}
for(int score=0;score<STU_SIZE;score++)
{
for(int test=0;test<TEST_SIZE;test++)
{
total+=ar[test][score];
avg=(double)total/STU_SIZE;
}
cout<<setw(10)<<"Average ";
//cout<<fixed<<setprecision(1)<<avg;
//total=0;
}
cout<<setw(10)<<"Average "<<avg;
}
|
Last edited on
How to compute average speed with a distance and time travel using a JFrame Form in Java . thaks.
With a few of changes, your code works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <iostream>
#include <iomanip>
using namespace std;
const int STU_SIZE=5;
const int TEST_SIZE=3;
void showTable(const int ar[][TEST_SIZE]);
void fillArray(int ar[][TEST_SIZE]);
int main()
{
int ar[STU_SIZE][TEST_SIZE];
fillArray(ar);
showTable(ar);
return 0;
}
void showTable(const int ar[][TEST_SIZE])
{
int i, j;
int total=0;
double avg;
cout << setw(12) << "Test#" << setw(10) << "Stu 1" << setw(10) << "Stu 2" << setw(10) << "Stu 3"
<< setw(10) << "Stu 4" << setw(10) << "Stu 5" << setw(10) << "Average" << endl;
for(int test = 0; test < TEST_SIZE; test++)
{
cout<< setw(12) << test+1;
for(int score = 0; score < STU_SIZE; score++)
{
cout<< setw(10)<< ar[score][test];
total+=ar[score][test];
avg=(double)total/STU_SIZE;
}
cout<<fixed<<setprecision(1)<<setw(10)<<avg;
total=0;
cout<<endl;
}
double scoreTotal;
double averageTotal;
cout << " Average ";
for(i = 0; i < STU_SIZE; i++)
{
scoreTotal = 0;
for(j = 0; j < TEST_SIZE; j++)
scoreTotal += ar[i][j];
if(i == 0) cout << setw(15) << scoreTotal/ double(TEST_SIZE); else cout << setw(10) << scoreTotal / double(TEST_SIZE);
}
averageTotal = 0;
for(i = 0; i < TEST_SIZE; i++)
{
scoreTotal = 0;
for(j = 0; j < STU_SIZE; j++) scoreTotal += ar[j][i]; scoreTotal /= double(STU_SIZE);
averageTotal += scoreTotal;
}
cout << setw(9) << averageTotal / (double)TEST_SIZE;
}
void fillArray(int ar[][TEST_SIZE])
{
for(int stu=0;stu<TEST_SIZE;stu++)
{
cout<<"Enter scores for student#"<<stu+1<<endl;
for(int test=0;test<STU_SIZE;test++)
{
cout<<"Student#"<<test+1<<":";
cin>>ar[test][stu];
}
}
}
|
Enter scores for student#1
Student#1:45
Student#2:78
Student#3:86
Student#4:78
Student#5:67
Enter scores for student#2
Student#1:67
Student#2:88
Student#3:96
Student#4:77
Student#5:57
Enter scores for student#3
Student#1:78
Student#2:56
Student#3:78
Student#4:67
Student#5:57
Test# Stu 1 Stu 2 Stu 3 Stu 4 Stu 5 Average
1 45 78 86 78 67 70.8
2 67 88 96 77 57 77.0
3 78 56 78 67 57 67.2
Average 63.3 74.0 86.7 74.0 60.3 71.7 |
Topic archived. No new replies allowed.