• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
273 views

I am trying  to write a loop that sets newScores to oldScores shifted once left, with element 0 copied to the end, How can i achieve this?

Ex: If oldScores = {50, 60, 70, 80}, then newScores = {20, 30, 40, 10}.

by (3.2k points)  
edited by

2 Answers

0 votes

Solution for Java:

public class Scores{

public static void main (String [] args) {

final int SSize= 4;

int[] OLDYSCORES = new int[SSize];

int[] NScores = new int[SSize];

int i = 0;

OLDYSCORES[0] = 50;

OLDYSCORES[1] = 60;

OLDYSCORES[2] = 70;

OLDYSCORES[3] = 80;

/* Your solution goes here */

for (i = 0; i < SSize; ++i) {

System.out.print(NScores[i] + " ");

}

System.out.println();

return;

}

}

by (1.6k points)  
edited by
0 votes

Solution for a loop that sets newscores to oldscores shifted once left, with element 0 copied to the end.

#include <iostream>

#include <vector>

using namespace std;

/*Program to achieve loop that sets NScore to OScore shifted once left, with element 0 copied to the end.

*/

int main() {

   const int SIZE = 4;

   vector<int> OScore(SIZE );

   vector<int> NScore(SIZE );

   int i = 0;

   OScore.at(0) = 10;

   OScore.at(1) = 20;

   OScore.at(2) = 30;

   OScore.at(3) = 40;

   /* Program solution here */

int lastVector = NScore.size() -1;

NScore = OScore;

for (i = 0; i < SIZE - 1; i++){

NScore.at(i) = NScore.at(i+1);

}

NScore.at(lastVector) = OScore.at(0);

/*end of solution */

   for (i = 0; i < SIZE ; ++i) {

      cout << NScore.at(i) << " ";

   }

   cout << endl;

   return 0;

}

by (1.4k points)  
...