Discover answers to your most pressing questions at Westonci.ca, the ultimate Q&A platform that connects you with expert solutions. Discover a wealth of knowledge from professionals across various disciplines on our user-friendly Q&A platform. Connect with a community of professionals ready to provide precise solutions to your questions quickly and accurately.
Sagot :
Answer:
The code is as follows:
for(int j = 0; j < newScores.length-1; j++){
newScores[j] = oldScores[j+1];
}
newScores[oldScores.length-1] = oldScores[0];
Explanation:
This loop iterates through the elements of oldScores
for(int j = 0; j < newScores.length-1; j++){
This enters the elements of oldScores to newScores starting from the element at index 1
newScores[j] = oldScores[j+1];
}
This moves the first element of index 0 to the last index of newScores
newScores[oldScores.length-1] = oldScores[0];
Following are the required code to calculate the shift of one left value in the array by using Java Programming:
Required Code:
for(i=0; i <SIZE; i++)//defining loop that calculates array value
{
if(i == SIZE - 1)//defining if block that checks size value
{
j = 0;//holding value 0 in j variable
}
else//defining else block
{
j = i+1;//holding incrememted value of i in j variable
}
newScores[i] = oldScores[j];//holding array vlue
}
for (i = 0; i < SIZE; ++i)//defining loop that prints array values
{
System.out.print(newScores[i] + " ");//print array
}
Complete Java program:
public class Main //defining the class Main
{
public static void main (String [] a)//defining the main method
{
final int SIZE = 4;//defining a final type integer variable SIZE
int i,j;//defining integer variable i
int[] oldScores = new int[SIZE];//defining an array oldScores
int[] newScores = new int[SIZE];//defining an array newScores
oldScores= new int[] {10,20,30,40};//defining an array that holds value
for(i=0; i <SIZE; i++)//defining loop that calculates array value
{
if(i == SIZE - 1)//defining if block that checks size value
{
j = 0;//holding value 0 in j variable
}
else//defining else block
{
j = i+1;//holding incrememted value of i in j variable
}
newScores[i] = oldScores[j];//holding array vlue
}
for (i = 0; i < SIZE; ++i)//defining loop that prints array values
{
System.out.print(newScores[i] + " ");//print array
}
}
}
Output:
Please find the attached file.
Program Explanation:
- Defining the class Main, and inside the class main method is declared.
- Inside the method, a final variable "SIZE" is declared that hold an integer value, and declare two integer variable "i,j".
- In the next line, two integer array "oldScores, newScores" is declared in which the "oldScores" array holds some integer values.
- In the next step, the two for loop is declared, in which the first loop is used to calculate the one left shift value of the array, and holds its value into the "newScores" array.
- The second loop is used to print the "newScores" value of the array.
Find out more information about the shifts in array value here:
brainly.com/question/14521251
We hope you found what you were looking for. Feel free to revisit us for more answers and updated information. Thanks for using our service. We're always here to provide accurate and up-to-date answers to all your queries. Thank you for choosing Westonci.ca as your information source. We look forward to your next visit.