average count

Hello, want to write a code with average count.

program is supposed to count the average number, then put out the numbers that are below the average number. E.g: I insert 4, then I input 4 integers; 1 2 3 4 and it outputs 2, because the average number of 1 2 3 and 4 is 2.5 and there are 2 integers below 2.5

so here's my current code
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
#include <iostream>

using namespace std;


int main()
{
    int number = 0;
    int tala = 0;
    int avrg = 0;
    int input = 0;
    int i = 0;

    cin >> input;

    int* arr = new int [input];


    for(i = 0; i < input; i++)
    {
        cin >> tala;

        arr[i] = tala;
        number += tala;

        /*if(arr[i] == 0)
        {
            arr[i]++;
        }
        */
    }

    avrg = number / tala;

    for(i = 0; i < input; i++)
    {
        arr[i] = tala;
        number = number / tala;
        if(tala < avrg)
        {
            tala++;
        }
    }

    cout << avrg << endl;



}


any inputs?
nevermind I changed the int in to double and reversed tala = arr[i]
because I wanted to count the amount of numbers in the arr[i].

Got the correct output now.
Hi Ariamn,

I updated your code and it should work fine:
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
#include <iostream>

using namespace std;

int main()
{
    float number = 0.0; // in case the user enters a decimal value
    float avrg; // You will need the decimal precision, doens't need to be declared here
    int input; //also doesn't need to be declared here

    cout << "Enter how many values you are going to use: " << endl;
    cin >> input;

    float* arr = new float [input]; // to allow the user to enter a decimal value

    cout << "Enter all the values consecutively: " << endl;
    for(int i = 0; i < input; i++) // Loop for user to enter in all the values
    {

        cin >> arr[i];
        number += arr[i];
        /*if(arr[i] == 0)
        {
            arr[i]++;
        }
        */
    }

    avrg = number / input;

    for(int i = 0; i < input; i++) // Loops through arr[] and outputs if arr[i] is lower than avrg
    {
        if(arr[i] < avrg)
        {
            cout << "Under the average: " <<arr[i] << endl;
        }
    }

    cout << "The average: " <<avrg << endl;

}


I made some changes however the comments should help you along the way, if you need any help feel free to PM me, good luck !
Topic archived. No new replies allowed.