0

I have been struggling with this problem and that is why I am asking here for help.

Let's imagine the following set of values for an array called "scores"

20 30 40 50 10 20 40 10

What I want to do is to create a loop that calculates the average each 4 values . In this case and doing manual math it should be

(20+30+40+50)/4
(10+20+40+10)/4 

Now , my point and my problem is that I do not know how to do that with a single loop. I understand that I can setup two loops and then do so , but as obvious if the array were bigger then I would have to setup hundreds of loops.Right now the solution I made is very far from the real solution

#include <iostream>
#include <string>

using namespace std;

int main()
{
  string names[4] = {"Peter","Mark","July","Sarah"};
  double scores [16] = {20,30,100,60,30,40,10,90,10,99,100,12,100,100,10,100};
  double average[4] ;
  int i;

    for (int d = 0, i = 0; d < 4 - 1; d++)
    {
        average[i] += scores[d];
        if (d % 4 == 0) {
            average[i] /= 4;
            i++;
        }
    }

    for (int l = 0 ; l < 4 ; l++)
    {

        cout << "\nName : " << names[l] << endl << endl ;
        cout << "Average :  " << average[l] << endl;

    }

}

Desired Output :

Name : Peter Average : 52.5 Name : Mark Average : 42.5 Name : July Average : 55.25 Name : Sarah Average : 77.5

Actual Output : Name : Peter Average : 5 Name : Mark Average : 130 Name : July Average : 3.11076e-317 Name : Sarah Average : 6.90921e-310

4
  • 3
    hint: you can increase d by more than 1 on each iteration of the loop Commented Nov 12, 2014 at 22:00
  • Why hundreds of loops? It would always be two loops, the outer one over the students and the inner one over each student's scores (what ever way the data is physically organized -- these two indices are always needed to access a specific score for a specific student). Each of the loops may be iterated arbitrarily often, but that's logically different. So this is an argument for using nested loops, explicitly or implicitly as via accumulate. Commented Nov 12, 2014 at 22:13
  • So, is this a C or a C++ question? Or are you restricted to the intersection? Commented Nov 12, 2014 at 22:17
  • I added more details and an example code Commented Nov 12, 2014 at 22:37

5 Answers 5

2

I think you want to use the basics of c++. The following code will give you the output you want.

      string names[4] = {"Peter","Mark","July","Sarah"};
      double scores [16] = {20,30,100,60,30,40,10,90,10,99,100,12,100,100,10,100};
      double average[4] ;
      int i;

        for (int d = 1, i = 0; d <= 16; d++)
        {
            average[i] += scores[d-1];
            if (d % 4 == 0) {
                average[i] /= 4;
                i++;
            }
        }

        for (int l = 0 ; l < 4 ; l++)
        {

            cout << "\nName : " << names[l] << endl << endl ;
            cout << "Average :  " << average[l] << endl;

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

2 Comments

I have edited the code and checked the code. This will work for sure.
Take a look at the example I posted with your solution and the results obtained vs desired
2

Assuming I understand your input data, and your goal, I believe something simple like this should work:

for (int i=0; i < num_students; i++)
    average[i] = std::accumulate(scores + 4*i, scores + 4*i + 4, 0) / 4;

1 Comment

I never covered accumulate on classes...thanks for your help but I cannot use that for this exercise
1

Try this:

average[0] = 0;
average[1] = 0;
for (unsigned int i = 0; i < scores_students; ++i)
{
  average[0] += scores[0 + i];
  average[1] += scores[4 + i];
}
average[0] = average[0] / scores_students;
average[1] = average[1] / scores_students;

Note that scores[0 + i] uses the scores in the first set of 4.
Also scores[4 + i] uses scores in the second set of 4.

2 Comments

This works..but If the array were bigger I would have to make more average[x] and know how much I have to add to i . Right?
If you were to have more averages, you would use more than one for loop, but your requirement is restricted to one for loop.
0

This is the first thing that crossed my mind.

   #include <iostream>
#include <string>

using namespace std;

int main()
{
    string names[4] = {"Peter","Mark","July","Sarah"};
    double scores[16] = {20,30,100,60,30,40,10,90,10,99,100,12,100,100,10,100};
    double average[4];
    for(int i = 0; i < 16 + 1; i+=4)
    {
        if(i != 0 && i % 4 == 0)
            average[(i / 4)-1] = (scores[i-4]+scores[i-3]+scores[i-2]+scores[i-1]) / 4;
    }
    for (int l = 0 ; l < 4 ; l++)
    {

        cout << "\nName : " << names[l] << endl << endl ;
        cout << "Average :  " << average[l] << endl;

    }
    return 0;
}

1 Comment

Why not use i+=4 in the for loop?
0

As I understand your problem, your main goal is just creating the speediest code. You want to have only one loop w/o any if statement.

My solution #1:

int scores[] = {20,30,100,60,30,40,10,90,10,99,100,12,100,100,10,100};
double avgs[4] = {};

for (int it = 0; it < 16; ++it)
{
    avgs[it / 4] += scores[it] / 4.0;
}

for (int it = 0; it < 4; ++it)
{
    cout << it << ": " << avgs[it] << endl;
}

My solution #2:

double accumulate(int input[4])
{
    return (input[0] + input[1] + input[2] + input[3]) / 4.0;
}

for (int it = 0; it < 4; ++it)
{
    avgs[it] = accumulate(scores + 4 * it);
}

Comments

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.