Why my program is going to infinite loop

Write a loop that sets newScores to oldScores shifted once left, with element 0 copied to the end. Ex: If oldScores = {10, 20, 30, 40}, then newScores = {20, 30, 40, 10}.

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
  #include <iostream>
using namespace std;

int main() {
   const int SCORES_SIZE = 4;
   int oldScores[SCORES_SIZE];
   int newScores[SCORES_SIZE];
   int i;

   oldScores[0] = 10;
   oldScores[1] = 20;
   oldScores[2] = 30;
   oldScores[3] = 40;

   for(int i=SCORES_SIZE-1;i>=SCORES_SIZE-SCORES_SIZE;i++)
   {
      cout<<newScores[SCORES_SIZE]<<",";
   }

   for (i = 0; i < SCORES_SIZE; ++i) {
      cout << newScores[i] << " ";
   }
   cout << endl;

   return 0;
}


I cannot understand why the program is going to the infinite loop. Can anyone please explain this. Thanks in advance.
You are initializing the first loop index to the end of the array and are therefore presumably wanting to count downwards, but you incrememt i. You need to decrement it.

In the body of that loop you only access newScores[SCORE_SIZE], which is outside the array (indices only go from 0 to SCORE_SIZE-1). Also, you are just "cout"ing the elements, which have not been initialized. And cout is certainly not going to shift anything anyway.

I don't see why you are counting downwards. There's no particular reason for that here. You may as well count upwards much like the second loop.
closed account (E0p9LyTq)
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
#include <iostream>

int main()
{
   const int SCORES_SIZE = 4;

   int oldScores[SCORES_SIZE] = { 10, 20, 30, 40 };
   int newScores[SCORES_SIZE];

   for (int i = 0; i < SCORES_SIZE; ++i)
   {
      std::cout << oldScores[i] << ' ';
   }
   std::cout << '\n';

   newScores[SCORES_SIZE - 1] = oldScores[0];

   for (int i = 0; i < SCORES_SIZE - 1; i++)
   {
      newScores[i] = oldScores[i + 1];
   }

   for (int i = 0; i < SCORES_SIZE; ++i)
   {
      std::cout << newScores[i] << ' ';
   }
   std::cout << '\n';
}

10 20 30 40
20 30 40 10
That's terrible. You didn't give the guy a chance to solve it himself. You are truly a cruel man. :-(
could also use std::copy to copy old[1..size-1] to new[0.. ] , and then just populate new's last element. Does that count as a loop? ;D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main() 
{
    const int SCORES_SIZE = 4;

    int old[SCORES_SIZE] = { 10, 20, 30, 40 };
    int neu[SCORES_SIZE];
    std::copy(&old[0] + 1, &old[0] + SCORES_SIZE,
              &neu[0]);
    neu[SCORES_SIZE-1] = old[0];

    for (auto n : neu)
        std::cout << n << ' ';
    std::cout << '\n';

    return 0;
}

20 30 40 10 
closed account (E0p9LyTq)
Careful there, icy1, someone might denounce you as being terrible because you "gave the entire answer."

I do like your solution, I might have used it if I had given the problem more than a quick bit of thought. :)
@icy1,
why should calling a function count as writing a loop?
Also why std::copy and not std::rotate?
Topic archived. No new replies allowed.