Do you know you can earn heaps of $$ by answering questions on kodlogs, for more info contact us through feedback link at footer? We are Australian based web site and pay you for answering questions

  • Register
Welcome to Kodlogs Q&A, where you can ask programming questions and receive answers from other members of the community. For programmers, by programmers Kodlogs is an open community for anyone that does coding. We help you get answers to your toughest coding questions, share knowledge with your coworkers in private, and find your next IT dream job.
0 votes
5 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 (2.7k 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.4k 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.2k points)  
...