Array

I have to question about an array:

How do you stop at 3 and calculate the avg and then keeping moving.

Since in a file there are 15 num. each student have 3 test and i need to calculate the per student avg
You make it sound like that the file doesn't have 15 numbers, but five groups of three numbers. Therefore, you should read one group at a time.
closed account (j3Rz8vqX)
Use an algorithm that includes a counter and condition it to calculate an average whenever the counter reaches 3 or more; reset counter afterwards.

My assumption is that you are reading from a file.

counter=sum=number=0;
Loop
---read number
---counter++;//counting from 0 to 2; 3 == reset;
---if(counter>2)
------calculate average of sums;
------sum=0;//reset sum;
---else
------sum = sum + number;//The one read
If you reset number, it will count same? Like 123 stop and then 123 again? It should 456 n 789
closed account (j3Rz8vqX)
Design from web and implementation may require some alterations, but something of this sort:
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>
#include <cstdlib>
#include <ctime>
using namespace std;
const int mySize = 15;
const int myStop = 3;
void fillArray(int *&myFakeFile)
{
    for(int i=0;i<15;i++)
    {
        myFakeFile[i] = rand()%10;//Fill the array with dummy data, for demonstration;
    }
}
void impliedTask(int *&myFakeFile,float *&avg)
{
    int sum=0;
    cout<<"===Start==="<<endl;
    for(int i=0,j=0;i<mySize;i++)
    {
        cout<<endl;
        cout<<"Number @index["<<i<<"] is: "<<myFakeFile[i]<<endl;
        sum+=myFakeFile[i];//accumulate sum
        cout<<"Sum @index["<<i<<"] is: "<<sum<<endl;
        if(j>=myStop-1)//stop to make adjust if j counter >=2 (0,1,2) == 3 numbers
        {
            avg[i/myStop] = float(sum)/float(myStop);//insert avg into index i/3
            cout<<"Average @index["<<i/myStop<<"] is: "<<float(int(10*avg[i/myStop]))/10<<endl;
            j=sum=0;//reset j counter and sum
        }
        else
        {
            j++;
        }
    }
}

int main ()
{
    srand(time(0));
    int *myFakeFile = new int[mySize];
    float *avg = new float[mySize/myStop];

    fillArray(myFakeFile);
    impliedTask(myFakeFile,avg);

    delete []avg;
    delete []myFakeFile;
    return 0;
};

In your case, you'd be reading from file; my assumption.
yes thank you tips
Topic archived. No new replies allowed.