Solving the Loop Puzzle: Shifting Old Scores to New Scores

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Loop Puzzle
Click For Summary

Discussion Overview

The discussion revolves around implementing a loop to shift elements in an array, specifically moving elements in an array of scores to the left and placing the first element at the end. Participants explore various coding approaches and share their thoughts on efficiency and correctness.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant suggests using a for loop to iterate through the array and shift elements, indicating a need for a conditional statement to handle the last element separately.
  • Another participant proposes a more complex mathematical approach using the math library, although it may not be necessary for this problem.
  • A simpler method is presented that populates all but the last element in the loop and assigns the first element to the last position after the loop.
  • One participant expresses a preference for the simpler method due to its efficiency, noting that it avoids unnecessary conditional checks within the loop.
  • There is a correction regarding the indexing in a suggested solution, with a participant initially believing it to be incorrect but later realizing it is valid due to the different arrays being used.
  • A participant shares a solution using UBasic, illustrating a similar shifting concept, while expressing a preference for that programming language.

Areas of Agreement / Disagreement

Participants present multiple approaches to the problem, and while some methods are favored for their simplicity or efficiency, there is no consensus on a single best solution. The discussion remains open with various perspectives on implementation.

Contextual Notes

Some participants note limitations in their ability to modify the initial setup of the code, such as the inclusion of libraries, which may affect their proposed solutions.

ineedhelpnow
Messages
649
Reaction score
0
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}.

Sample program:

Code:
#include <iostream>
#include <vector>
using namespace std;

int main() {
   const int SCORES_SIZE = 4;
   vector<int> oldScores(SCORES_SIZE);
   vector<int> newScores(SCORES_SIZE);
   int i = 0;

   oldScores.at(0) = 10;
   oldScores.at(1) = 20;
   oldScores.at(2) = 30;
   oldScores.at(3) = 40;

   <STUDENT CODE>

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

   return 0;
}

(Worried) I've been staring at this problem for a while. how do i do it? i need to use a for loop i believe (i=0;i<SCORES_SIZE;++i). i don't know what to put in the loop.
 
Technology news on Phys.org
Perhaps you could try something like:

Code:
   for (i = 0; i < SCORES_SIZE; i++)
   {
      if (i == (SCORES_SIZE - 1))
      {
         newScores.at(i) = oldscores.at(0);
      }
      else
      {
         newScores.at(i) = oldscores.at(i + 1);
      }
   }
 
Another alternative (include the math library):

Code:
   for (i = 0; i < SCORES_SIZE; i++)
   {
      newScores.at(i) = oldscores.at(SCORES_SIZE*((i - (SCORES_SIZE - 2)/2)/SCORES_SIZE - floor(1/2 + (i - (SCORES_SIZE - 2)/2)/SCORES_SIZE)) + SCORES_SIZE/2);
   }
 
Perhaps simpler still would be:

Code:
   for (i = 0; i < SCORES_SIZE - 1; i++)
   {
         newScores.at(i) = oldscores.at(i + 1);
   }
   newScores.at(SCORES_SIZE - 1) = oldscores.at(0);
 
(Giggle) thanks. I am going to make a folder on my desktop to hold all the possible solutions for this question. (Rofl) in my activity the way it is i can only edit the student code part so since the math library wasnt included at the beginning there is no way for me to add it. ill try the first one because it looks similar to something i tried.
 
ineedhelpnow said:
(Giggle) thanks. I am going to make a folder on my desktop to hold all the possible solutions for this question. (Rofl) in my activity the way it is i can only edit the student code part so since the math library wasnt included at the beginning there is no way for me to add it. ill try the first one because it looks similar to something i tried.

The last suggestion is similar to the first, but rather than using an if/else construct within the loop, all but the last element of the new array is populated in the for loop, and then the last element is handled separately after the loop. That would be my choice for efficiency. :D
 
So in the end the student part of the code should look like this for all our java users out there:

Code:
   for (i = 0; i < SCORES_SIZE - 1; i++)
   {
         newScores[i] = oldScores[i + 1];
   }
   newScores[SCORES_SIZE - 1] = oldScores[0];
 
CABradfish said:
So in the end the student part of the code should look like this for all our java users out there:

Code:
   for (i = 0; i < SCORES_SIZE - 1; i++)
   {
         newScores[i] = oldScores[i + 1];
   }
   newScores[SCORES_SIZE - 1] = oldScores[0];
This is incorrect because the 0th element is overwritten in the loop, and therefore it won't end up at the end of the array.

Edit: Sorry, I did not notice that the new array is different, so the code is correct after all. Well, this raises a natural question how to update the original array in a similar way.
 
Just in case this helps...

Using my favorite UBasic:

Array A contains: A(1)=10, A(2)=20, A(3)=30, A(4)=40

Program:
k = A(1)
FOR i = 1 to 3
A(i) = A(i+1)
NEXT i
A(4) = k

I'm the kind of dummy that can't see the sense
in anyone using anything other than UBasic :)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
12
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
Replies
1
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K