Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a project Lab10. Q1) Write a Java program that will read n integers from the user and store them in an array called arr.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Create a project Lab10. Q1) Write a Java program that will read n integers from the user and store them in an array called arr. Then do the following: 1. print all the elements of arr in the order they were entered. 2. print the elements of the array in reverse. 3. Print the odd numbers only 4. Print the even indices only Name your class ReadArray Sample Run: Please enter how many numbers: 7 The numbers in order: 24924123 The numbers in reverse: 31242942 The odd numbers only: 93 The even indices only: 2943 Q2) Write a Java program that will read several integers (not more than a 50 integers) ending with -1. Read and store those numbers in an array ar. After that, you should do the following: 1. Display how many integers were entered (excluding -1) 2. Print all elements of the array in order 3. Print the elements of ar in reverse 4. Print the first half of the array. (The first half of the numbers) 5. Add all the integers in the array and display their Average. Name your class halfArray Sample Run: Please enter the integers: 7428832281 The number of integers that have been read is 7 The array elements in order: 7432883228 The first half of array elements: 7432 Average of all numbers: 21,428571428571427 Q3) Write a Java program that will read several integers from the user ending with - 1 and store them in an array ar with Max_Size =50. Then, read a number x and check if it is in ar or not. If it is in the array then print the a message stating that it is in the array and its location, otherwise, print a message stating that its not one of the elements in the array. Call your class arraysearch. Sample Run: Please enter the numbers: 5931081091 Please enter the search number: 4 Sorry 4 is not in the Array. Sample Run: Please enter the numbers: 5993108109 - 1 Please enter the search number: 99 The number 99 is at Index 1 CSC111 Lab Arrays - Lab II Lab 10 part 2 -.- Create a project Lab10 (Or use same project from part 1). Q1) Write a Java program that will declare an array of integers ar with Max_Size of 50 . Then, read array elements from the user and store them in ar. You must stop reading integers when the user enters - 1. Then, do the following: - Print the size of the array. - Print all elements in the array. - read a number x and check if it is in ar or not. If it is in the array then print a suitable message stating that it is in the array and its location, otherwise, print a message stating that its not one of the elements in the array. - Shift all the elements of the array one position to the right. - Print all elements in the array. Call your class arrayOps. Sample Run: Please enter the numbers: 5993108109-1 The array is of size: 6 The elements of the array are: 5993108 Please enter the search number: 23 Sorry. Number 23 is not in the Array. After shifting, The elements are: 1095993108 Sample Run: Please enter the numbers: 59931081091 The array is of size: 6 The elements of the array are: 5993108109 Please enter the search number: 99 The number 99 is at Index 1 After shifting, The elements are: 1095993108 Q2) Improve arrayOps from the previous question by converting the print, search and shift operations into methods: - static void printArray(int[] a, int size) Prints all the elements in the array a. - ststic int search(int[] a, int x, int size) Searches inside a for x. If found, the method return its location, otherwise it returns 1. - static void shiftRight(int[] a, int size) Shifts the array a to the right one position. You must also improve the main program. After reading the array, create a suitable menu that gives the user options to print, search or shift the array. Print the elements of the array after each operation. You must do this repeatedly until the user chooses to stop. Q3) Write a Java program that will read n integers from the user and store them in an array ar. Then, read two integers, x and y, both less than n and switch the two array elements located at indecies x and y. (i.e. switch ar[x] and ar[y] with each other. This is called swap) Call your class arraySwitch. Hint: To swap two array elements at indecies i and j, you'll need a temporary variable temp: temp = ar [j]; ar[j]=ar[1]; ar[1]= tempi Where temp is of the same type as ar elements. Sample Run: Please enter how many numbers: 7 The elements of the array are: 24924123 Please enter the locations to switch: 25 The elements of the array are: 224122493 Q4) Write a Java program that will read n integers from the user and store them in an array ar. But make sure when adding a new element that it is unique and not in the array. If the number already exists, do not add it. Finally, print size and all elements of the array. Call your class uniqueArray. Sample Run: Please enter how many numbers: 7 Please enter the numbers: 59931038109 The number 3 is a repeat and was not added to the array. The array is of size: 6 The elements of the array are: 5993108109 Extra: 1) Convert shiftRight(int[] a, int size) from Q2 into shiftRight(int[] a, int size, int n ) where you shift the array a to the right n times. 2) Implement void shiftLeft(int[] a, int size) Which shifts the array to the left one position

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Sham Navathe

4th Edition

0321122267, 978-0321122261

More Books

Students also viewed these Databases questions