0

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

5
  • 5
    Move int sum = 0; outside the loop. Commented Aug 30, 2013 at 9:37
  • 1
    What this code does: sum += cole[0]; cole[0]++; ? Commented Aug 30, 2013 at 9:41
  • Why do you increase the array entries after summing them up? Commented Aug 30, 2013 at 9:41
  • Also you include cole[0] twice. Commented Aug 30, 2013 at 9:42
  • start using debugger please :) Commented Aug 30, 2013 at 9:45

6 Answers 6

1

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

Sign up to request clarification or add additional context in comments.

Comments

1
    #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;

}

Comments

0

Get rid of int sum = 0 and cole[x]++; inside the loop. Also so lose:

sum += cole[0];
cole[0]++;

after the loop.

Comments

0
int sum = 0;

This needs to be outside the loop, you are continually resetting the sum.

Comments

0

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]++;
}

Comments

0

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;

}

1 Comment

There is still a double declaration of sum

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.