Hey guys i need some help..
Here is my code given below + the question statement..
The problem is simple i just wanted to ask what and how i can use variable to calculate average in array questions..

Problem Statement:
"Write a program that declares an array of 25 integers and populates the array by reading values from the user (i.e. reads 25 numbers from the user and puts them into the array). Now your program computes and displays the average of all the numbers that are between 10-80."

CODE:

#include<iostream>
#include<conio.h>

using namespace std;
int main()
{
    int x[25];
    int count = 0, avg;
    for(int i=0; i<25; i++)
    {
        cout<<"Please enter a number.."<<endl;
        cin>>x[i];
    }
    for(int i=0; i<25; i++)
    {
        if(x[i]> 10 && x[i]<80)
        {
            count = count + 1;
        }
    }
    //here lies the problem
    avg = count/ 
    //????

    getch();
    return 0;
}

i dont know explain but the code should be like this...
But not sure also

    if(x[i]> 10 && x[i]<80)
    {
        count = count + 1;
        avg=x[i];
    }
    }
    int total=0;
    total=avg/count;
    cout<<total<<endl;
    return 0;
    }

ok let me apply that.. thanks BTW.:)

nope its not working..
its not adding the values in avg=x[i]. rather taking only one value and dividing it with total number of inputs .

#include<iostream>
using namespace std;
int main()
{
    int x[25];
    int count = 0, avg=0;
    int tot=0;
    for(int i=0; i<25; i++)
    {
        cout<<"Please enter a number.."<<endl;
        cin>>x[i];
    }
    for(int i=0; i<25; i++)
      if(x[i]> 10 && x[i]<80)
    {
        count = count + 1;
        avg+=x[i];  //  avg=avg+x[i]
    }


    tot=avg/count;
    cout<<tot<<endl;
    return 0;
    }

Edited 1 Year Ago by phoenix254: .....

Here you go, try this:

    int x[25];
    int count = 0, avg, sum = 0;
    for (int i = 0; i<25; i++)
    {
        cout << "Please enter a number.." << endl;
        cin >> x[i];
    }
    for (int i = 0; i<25; i++)
    {
        if (x[i]> 10 && x[i]<80)
        {
            count++;
            sum += x[i];
        }
    }
    //here lies the problem
    avg = sum / count;
    cout << "The average is: " << avg << endl;
        //????
        //getch();
    system("pause");
    return 0;

You need to declare a sum variable then add all the valid values to add then calcuate the avg using sum/count.
Hope this helps.

Edited 1 Year Ago by akgh2010

This question has already been answered. Start a new discussion instead.