Okay, getting towards the end of the semester and this self study course has been interesting to say the least.
This weeks issue deals with Arrays and Averages.
I'll give the simplified version since I may have more questions later.
I need to create some code that will create and array and then create a loop which will assign numbers to the array. I'm using student gpa's in my example. Once the numbers are input into the array, we then are supposed to average all the gpa's. I'm trying out some code which will just add the first two inputs so I know I'm on the right track.
Can you tell me what I need to change or add so the code will SUM the two elements from the array.
For example,
Let's say I enter in the following 5 numbers once we start the code.
10
20
30
40
50
I want this to add the 10 & 20 and COUT 30. What am I doing wrong here? Why isn't the sample(0) and sample(1) adding up? Thanks in advance.
#include <iostream>
usingnamespace std;
int main()
{
int sample[5];
int gpa;
int total;
for (int i = 0; i < 5; i++)
{
//enter input items
cout << "Please enter a GPA." << endl;
cin >> gpa;
sample[i] = gpa;
}
int sum =0;
for(int i = 0; i < 5; i++)
{
cout << sample[i] << " ";
sum += sample[i];
}
cout<< " The sum is " << sum <<endl;
cout << endl << "Finished!" << endl;
return 0;
}
#include <iostream>
usingnamespace std;
int main()
{
int sample[5]; // create sample with 5 elements
int gpa; // set up integer called gpa
int total; // set up integer called total
for (int i = 0; i < 5; i++) // for (initialization; condition; increase) statement;
// 1. initialization is executed. Generally it is an initial value setting for a counter variable. This is executed only once.
// 2. condition is checked. If it is true the loop continues, otherwise the loop ends and statement is skipped (not executed).
// 3. statement is executed. As usual, it can be either a single statement or a block enclosed in braces { }.
// 4. finally, whatever is specified in the increase field is executed and the loop gets back to step 2.
{
cout << "Please enter a GPA." << endl;
cin >> gpa; //enter gpa
sample[i] = gpa; // all the elements in the sample[i] are called gpa
}
double sum = 0.00; // set up double called sum
double avg = 0.00;
for(int i = 0; i < 5; i++) // for (initialization; condition; increase) statement;
{
cout << sample[i] << " ";
sum += sample[i]; //sum = sum + sample[i]
avg = sum/5;
}
cout<< " The sum is " << sum <<endl;
cout<< " The avg is " << avg <<endl;
cout << endl << "Finished!" << endl;
return 0;
}
Thanks Grey Wolf. Hopefully this is my last question...
What's the code to force the output to show two decimals? Even if someone enters a 2 I want it to show as 2.00 in the final comparison. I looked through the tutorials but didn't see it and remember something that forces or converts the variable to show a set number of places.
#include <iostream>
usingnamespace std;
int main()
{
double sample[5]; // create sample with 5 elements
double gpa = 0.0; // set up double called gpa
double total = 0.0; // set up double called total
for (int i = 0; i < 5; i++) // for (initialization; condition; increase) statement;
{
cout << "Please enter a GPA." << endl;
cin >> gpa; //enter gpa
sample[i] = gpa; // all the elements in the sample[i] are called gpa
}
double sum = 0.0; // set up double called sum
double avg = 0.0;
for(int i = 0; i < 5; i++) // for (initialization; condition; increase) statement;
{
// cout << sample[i] << " "; THIS WOULD SHOW ALL THE NUMBERS ENTERED
sum += sample[i]; //sum = sum + sample[i]
avg = sum/5;
}
cout<< "The average GPA is " << avg <<endl<<endl;
for(int i = 0; i < 5; i++) // for (initialization; condition; increase) statement;
if (sample[i] > avg)
cout << sample[i] <<" is above average" << endl;
elseif (sample[i] < avg)
cout << sample[i] <<" is less than average" << endl;
else
cout << sample[i] <<" is equal to the average" << endl;
return 0;
}
cout.precision(2);
cout<< "The average GPA is " << avg <<endl<<endl;
or you can use cout<< setprecision(2) << "The average GPA is " << avg <<endl<<endl;
I would personally prefer the former if I wanted it done for every case where I use cout. However, I would use the latter if I only wanted to output it for two decimal places once. You can find it in <iomanip> if it isn't already included.
Okay, it's not working. If I enter in a 1 as the GPA the cout give a 1 instead of a 1.00 like I want it to. Where am I going wrong? I thought the setprecision would work. Here is my code...
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
double sample[5]; // create sample with 5 elements
double gpa = 0.00; // set up double called gpa
double total = 0.00; // set up double called total
for (int i = 0; i < 5; i++) // for (initialization; condition; increase) statement;
// 1. initialization is executed. Generally it is an initial value setting for a counter variable. This is executed only once.
// 2. condition is checked. If it is true the loop continues, otherwise the loop ends and statement is skipped (not executed).
// 3. statement is executed. As usual, it can be either a single statement or a block enclosed in braces { }.
// 4. finally, whatever is specified in the increase field is executed and the loop gets back to step 2.
{
cout << "Please enter a GPA." << endl;
cin >> gpa; //enter gpa
sample[i] = gpa; // all the elements in the sample[i] are called gpa
}
double sum = 0.00; // set up double called sum
double avg = 0.00;
for(int i = 0; i < 5; i++) // for (initialization; condition; increase) statement;
{
// cout << sample[i] << " "; THIS WOULD SHOW ALL THE NUMBERS ENTERED
sum += sample[i]; //sum = sum + sample[i]
avg = sum/5;
}
cout << "The average GPA is " << setprecision (3) << avg << endl <<endl;
for(int i = 0; i < 5; i++) // for (initialization; condition; increase) statement;
if (sample[i] > avg)
cout << setprecision (3) << sample[i] <<" is above average" << endl;
elseif (sample[i] < avg)
cout << setprecision (3) << sample[i] <<" is less than average" << endl;
else
cout << setprecision (3) << sample[i] <<" is equal to the average" << endl;
return 0;
}
floatfield needs to be changed to fixed point (I should have noted that in my previous post): cout << fixed << setprecision (3) << samples[i] <<" is above average" << endl;
for example.