Reputation: 21
I am currently trying to use C++. I've learned so far to make a program that allows me to input 10 variables as well as 5 variables that are already assigned values and then find the average of these numbers. However I cannot figure out how to do this, I've made a for loop for the array but didn't seem to find the answer average. What's wrong with the following code? This is what I have so far:
#include <iostream>
using namespace std;
int cole[10];
int sum = 0;
int main()
{
int a = 10;
int b = 10;
int c = 10;
int d = 10;
int e = 35;
cout << "Please input ten numbers, one at a time" << endl;
cin >> cole[0];
cin >> cole[1];
cin >> cole[2];
cin >> cole[3];
cin >> cole[4];
cin >> cole[5];
cin >> cole[6];
cin >> cole[7];
cin >> cole[8];
cin >> cole[9];
cout << "There will now be 5 assigned variables, so that we have 15 variables" << endl;
for(int x = 0; x < 10; x++ )
{
int sum = 0;
cout << cole[x] << " ";
sum += cole[x];
cole[x]++;
}
sum += cole[0];
cole[0]++;
cout << "and " << a << " " << b << " " << c << " " << d << " " << e << endl;
cout << "The average of these numbers is: ";
sum = sum + a + b + c + d + e;
cout << sum / 15;
}
Thanks in advance
Upvotes: 0
Views: 2714
Reputation: 8986
#include <iostream>
using namespace std;
int coleCount = 10;
int cole[coleCount];
int sum = 0;
int main()
{
int a = 10;
int b = 10;
int c = 10;
int d = 10;
int e = 35;
cout << "Please input ten numbers, one at a time" << endl;
for(int i = 0; i < coleCount; i++)
{
cin >> cole[i];
}
cout << "There will now be 5 assigned variables, so that we have 15 variables" << endl;
for(int i = 0; i < coleCount; i++ )
{
cout << cole[i] << " ";
sum += cole[i];
//cole[x]++; why are you incrementing this?
}
//sum += cole[0]; why?
//cole[0]++; why?
cout << "and " << a << " " << b << " " << c << " " << d << " " << e << endl;
cout << "The average of these numbers is: ";
sum += (a + b + c + d + e);
cout << sum / 15;
}
Upvotes: 1
Reputation: 2541
Please try this code: IDEONE
#include <iostream>
using namespace std;
int cole[10];
int sum = 0;
int main()
{
int a = 10;
int b = 10;
int c = 10;
int d = 10;
int e = 35;
double avg = 0;
cout << "Please input ten numbers, one at a time" << endl;
for (int i=0;i<10;i++) {
cin >> cole[i];
}
cout << "There will now be 5 assigned variables, so that we have 15 variables" << endl;
for(int x = 0; x < 10; x++ )
{
cout << cole[x] << " ";
sum += cole[x];
}
cout << "and " << a << " " << b << " " << c << " " << d << " " << e << endl;
cout << "The average of these numbers is: ";
sum = sum + a + b + c + d + e;
avg = sum / 15.0;
cout << avg;
}
Remember that average should not be an integer value - it will round the number to the floor. And remember you can input every value to the array as well in loop, never do it one by one. You also declared the "int sum = 0" again and again in loop, so the global sum was not visible inside the loop. I deleted some of unneeded code too. You can check it out, cheers
Upvotes: 1
Reputation: 3801
Fixed code:
#include <iostream>
using namespace std;
int cole[10];
int sum = 0;
int main()
{
int a = 10;
int b = 10;
int c = 10;
int d = 10;
int e = 35;
cout << "Please input ten numbers, one at a time" << endl;
cin >> cole[0];
cin >> cole[1];
cin >> cole[2];
cin >> cole[3];
cin >> cole[4];
cin >> cole[5];
cin >> cole[6];
cin >> cole[7];
cin >> cole[8];
cin >> cole[9];
cout << "There will now be 5 assigned variables, so that we have 15 variables" << endl;
int sum = 0; //initialize sum outside the for loop
for(int x = 0; x < 10; x++ )
{
cout << cole[x] << " ";
sum += cole[x];
cole[x]++;
}
//sum += cole[0]; //this seems unnecessary
//cole[0]++;
cout << "and " << a << " " << b << " " << c << " " << d << " " << e << endl;
cout << "The average of these numbers is: ";
sum = sum + a + b + c + d + e;
cout << sum / 15;
}
Upvotes: 0
Reputation: 1870
You are declaring sum
twice. Remove the declaration from inside the for loop!
for(int x = 0; x < 10; x++ )
{
cout << cole[x] << " ";
sum += cole[x];
cole[x]++;
}
Upvotes: 0
Reputation: 1783
int sum = 0;
This needs to be outside the loop, you are continually resetting the sum.
Upvotes: 0
Reputation: 27577
Get rid of int sum = 0
and cole[x]++;
inside the loop. Also so lose:
sum += cole[0];
cole[0]++;
after the loop.
Upvotes: 0