Question

In: Computer Science

Write a loop that sets newScores to oldScores shifted once left, with element 0 copied to...

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}.

--------------Code Below-----------

#include <iostream>
using namespace std;

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

for (i = 0; i < SCORES_SIZE; ++i) {
cin >> oldScores[i];
}

/* Your solution goes here */

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

return 0;
}

Solutions

Expert Solution

#include <iostream>
using namespace std;

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

for (i = 0; i < SCORES_SIZE; ++i) {
cin >> oldScores[i];
}
//  oldScores with element 0 copied to the end of newScores.
newScores[SCORES_SIZE-1]=oldScores[0];

//a loop that sets newScores to oldScores shifted once left
for(j=1;j<SCORES_SIZE;j++){
    newScores[j-1]=oldScores[j];
}

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

return 0;
}

//screenshot of the code

output:

Explanation:

  • First we are assigning the 0th position of oldscore to the end position of a new score.
  • Next we are going to write a for loop that starts with 1 position to size of score and shifting left elements to newscore
  • eg when j=1 ,oldscore [1] =20 is assigned to newscore[j-1] i.e newscore[0] =20 similarly all the elements are shifted left
  • Later we printing the result of newscore

Related Solutions

Write a loop that sets each array element to the sum of itself and the next...
Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial scores: 10, 20, 30, 40 Scores after the loop: 30, 50, 70, 40 The first element is 30 or 10 + 20, the second element is 50 or 20 + 30, and the third element is 70 or 30 + 40. The last...
Write a loop that sets each array element to the sum of itself and the next...
Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial scores: 10, 20, 30, 40 Scores after the loop: 30, 50, 70, 40 The first element is 30 or 10 + 20, the second element is 50 or 20 + 30, and the third element is 70 or 30 + 40. The last...
Write a loop that subtracts 1 from each element in lowerScores. If the element was already...
Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}. please bold the solution import java.util.Scanner; public class StudentScores { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int SCORES_SIZE = 4; int[] lowerScores = new int[SCORES_SIZE]; int i; for (i = 0; i < lowerScores.length; ++i) {...
In C. Write a loop that subtracts 1 from each element in lowerScores. If the element...
In C. Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}.
write a for loop that uses the loop control variable to take on the values 0...
write a for loop that uses the loop control variable to take on the values 0 through 10. In the body of the loop, multiply the value of the loop control variable by 2 and by 10. Execute the program by clicking the Run button at the bottom of the screen. Is the output the same?
THE FOLLOWING IS CODED IN C Write a function that sets each element in an array...
THE FOLLOWING IS CODED IN C Write a function that sets each element in an array to the sum of the corresponding elements in two other arrays. That is, if array 1 has the values 2,4, 5, and 8 and array 2 has the values 1, 0, 4, and 6, the function assigns array 3 the values 3, 4, 9, and 14. The function should take three array names and an array size as arguments. Test the function in a...
2. If an image is shifted in spatial domain by 10 units to the left and...
2. If an image is shifted in spatial domain by 10 units to the left and at the same time rotated counter clockwise by 30 degree. What impact will you expect in the magnitude spectrum? Explain.
Write out a algorithm that sets last equal to the last element in a queue, leaving...
Write out a algorithm that sets last equal to the last element in a queue, leaving the queue unchanged. Write out a algorithm to create a copy of myQueue, leaving myQueue unchanged. Write out a algorithm Replace that takes a stack and two items. If the first item is in the stack, replace it with the second item, leaving the rest of the stack unchanged. Write out a algorithm Replace that takes a queue and two items. If the first...
If graph of given function is shifted 6 units to the left, the new graph will...
If graph of given function is shifted 6 units to the left, the new graph will represent the function ___________. If the graph of the given (first) function is shifted upwards by 6 units, the graph will represent the function ___________. Choose answers for the first two blanks from these answers: a. y = f(x + 6) = {x + 6)^ b. y = f(x - 6) = (x - 6)^ c. y = fx + 6 = x^ +...
The C language permits a variable to be left- or right-shifted by a nonconstant amount (e.g....
The C language permits a variable to be left- or right-shifted by a nonconstant amount (e.g. i >> j where i and j are variables), but the MIPS instruction set only supports shifts by a constant value (e.g. i >> 2). In words rather than code, describe how a variable-length right shift could be performed using MIPS instructions.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT