CSE 110 – Lab 8 solution

$14.99

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (3 votes)

This lab is for practicing 1-D arrays. Use the following Coding Guidelines: • When declaring a variable, you usually want to initialize it. • Use white space to make your program more readable. • Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Assignments Documentation: At the beginning of each programming assignment you must have a comment block with the following information: /*————————————————————————–// AUTHOR: (Put your name here) // FILENAME: Lab6.java // SPECIFICATION: This program is for practicing arrays. // INSTRUCTIONS: Read the following code skeleton and add your own code // according to the comments. Ask your TA or your class// mates for help and/or clarification. When you see // //–> that is where you need to add code. // LAB LETTER: (Put your lab letter) //————————————————————————-*/ Getting Started Create a class called Lab8. Use the same setup for setting up your class and main method as you did for the previous assignments. Be sure to name your file Lab8.java. Hints Please replace //–> with the correct program to finish the task according to the corresponding comment. Please replace ??? with the correct program to enable the program to run as required. /*————————————————————————–// AUTHOR: (Put your name here) // FILENAME: Lab8.java // SPECIFICATION: This program is for practicing arrays. // // INSTRUCTIONS: Read the following code skeleton and add your own code // according to the comments. Ask your TA or your class// mates for help and/or clarification. When you see // //–> that is where you need to add code. // LAB LETTER: (Put your lab letter) //————————————————————————-*/ //import Scanner class import java.util.Scanner; //declare the class Lab6 public class Lab8 { //declare the main method public static void main(String[] args) {
// Define scan object of the type Scanner class //–> //define an int variable //—>
//Declare an integer array int[] int_arr; //Assign it memory location. Specify the dimension of the array(Let’s take 5) int_arr = new int[5];
//Using a for loop which runs till , read values from the user and store it in the integer array. for (???; ??? ; ???) { // Read the value the user enters //—> //Assign it to the ith element of the array. //—> } //TASK 1 //Using a for loop which runs till , print all the values of the array and find the sum of all elements of the array. sum = 0; for (???; ??? ; ???) { // Print all the values of the array. //—> //Add the element to sum //—> } //Print the value of sum //—> //TASK 2 //Using the array that we’ve created, we’ll rotate the elements in the arrays. //Given an array, after computation the array will be with the elements “rotated left” so {1, 2, 3} yields {2, 3, 1}. //Store the last element: int last = int_arr[int_arr.length – 1]; //Store the first element: int first = int_arr[0];
for(int i =0; i< int_arr.length -1; i++) { //Shift the elements one position upward. } //Assign the last and first variables to their positions. //Display the array again using a for-loop for (???; ??? ; ???) { // Print all the values of the array. //—> }
//close scanner object
} }
SAMPLE OUTPUT:
Please enter value for index 0: 10 Please enter value for index 1: 20 Please enter value for index 2: 33 Please enter value for index 3: 40 Please enter value for index 4: 50 Value at index 0: 10 Value at index 1: 20 Value at index 2: 33 Value at index 3: 40 Value at index 4: 50 The sum of all the elements of the array: 153 Array after left rotation: Value at index 0: 20 Value at index 1: 33
Value at index 2: 40 Value at index 3: 50 Value at index 4: 10