Question

Playing With Arrays The following problems are to give you practice with programming with arrays. Write...

Playing With Arrays

The following problems are to give you practice with programming with arrays. Write a program to:

1.Ask the user for numbers and place them in an array sequencially.

2.Next, print out the values in the array in the order they are stored.

3.Search the array for the postion containing the smallest value.

4.Print out the position where the smallest value was found.

5.Swap the smallest value with the value in the first postition of the array.

6.Print out the array in the order they are now stored.

7.Reverse the elements of the array.

8.Print out the array in the order they are now stored.

9.Add 1 to every odd number in the array.

10.Print out the array in the order they are now stored.

11.Repeatedly ask the user for a number and printout the position in the array where that number is stored. Print "Not Found" if the number is not in the array.
Quit when the user enters a sentinel value (such as -1).

Directions

You must write a method to print out the elements of the array in the order they are stored. Use that method every time the array is printed.

Test your program thoroughly. Include a screen-grab printout of your tests and the results.

Hints:

Build up the program incrementally. Just do step 1 above and test it. Once working, add in step 2. Then test it. Then step 3, and so on.

Thanks for the help

0 0
Add a comment Improve this question Transcribed image text
Answer #1

NOTE: I have completed code according to your requirement and assuming the language is Java. If not, please let me know the language required i will code accordingly. Please check and let me know if you have any questions. I will acknowledge back within 24 hours. Thanks for your patience.

Code:
import java.util.Scanner;

class Main{
public static void main(String[] args){
// declaring variables
Scanner sc = new Scanner(System.in);
int n, min_pos = 0, min;
int[] arr = new int[100];
int[] temp_arr = new int[100];
  
// checking how many numbers user want to enter
System.out.println("Enter how many values? ");
n = sc.nextInt();
// getting the set of numbers user want to enter
for(int i = 0; i < n; i++){
System.out.println("Enter "+(i+1)+ " number: ");
arr[i] = sc.nextInt();
}
System.out.println("Given array elements are:");
//printing the numbers stored in array
for(int i = 0; i < n; i++)
System.out.println(arr[i]+ " ");
// searching array for smallest number position
min = arr[0];
for(int i = 1; i < n; i++)
if(arr[i] < min){
min_pos = i;
min = arr[i];
}
// printing the smallest number position
System.out.println("Smallest number is at position: "+ min_pos);
// swap smallest value with value in first position of the array
int t = arr[0];
arr[0] = arr[min_pos];
arr[min_pos] = t;
// printing the numbers in arrays again
System.out.println("Array elements after swapping with min value position is:");
for(int i = 0; i < n; i++)
System.out.println(arr[i]+ " ");
// reversing numbers in array
int k = 0;
for(int j = n-1; j >= 0; j--)
temp_arr[k++] = arr[j];
for(int i = 0; i < n; i++)
arr[i] = temp_arr[i];
// printing the array in the order now it is stored
System.out.println("Array elements after reversing:");
for(int i = 0; i < n; i++)
System.out.println(arr[i]+ " ");
// add 1 to every odd number in the array
for(int i = 0; i < n; i++)
if(arr[i] % 2 == 1)
arr[i] += 1;
// printing the array in the order it is stored now
System.out.println("Array elements after adding 1 to odd numbers:");
for(int i = 0; i < n; i++)
System.out.println(arr[i]+ " ");
int x;
while(true){
System.out.println("Enter a number to check in array: ");
x = sc.nextInt();
if(x == -1) break;
int i = 0;
for(; i < n; i++)
if(arr[i] == x){
System.out.println("Given number found and it is position "+ i);
break;
}
if(i == n)
System.out.println("Not Found. Try again");
}
}
}

Code output screenshot:

Add a comment
Know the answer?
Add Answer to:
Playing With Arrays The following problems are to give you practice with programming with arrays. Write...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Java In this assignment we are working with arrays. You have to ask the user...

    Using Java In this assignment we are working with arrays. You have to ask the user to enter the size of the integer array to be declared and then initialize this array randomly. Then use a menu to select from the following Modify the elements of the array. At any point in the program, if you select this option you are modifying the elements of the array randomly. Print the items in the array, one to a line with their...

  • In C++, write a complete program that receives a series of student records from the keyboard...

    In C++, write a complete program that receives a series of student records from the keyboard and stores them in three parallel arrays named studentID and courseNumber and grade. All arrays are to be 100 elements. The studentID array is to be of type int and the courseNumber and grade arrays are to be of type string. The program should prompt for the number of records to be entered and then receive user input on how many records are to...

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to...

    This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to Using Individual Variables One of the main advantages of using arrays instead of individual variables to store values is that the program code becomes much smaller. Write two versions of a program, one using arrays to hold the input values, and one using individual variables to hold the input values. The programs should ask the user to enter 10 integer values, and then it...

  • Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values,...

    Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values, of same size         b. Add these 2 arrays. ........................................................................................................................... Q2. Write a program in java (using loop) input any10 numbers from user and store in an array. Check whether each of array element is positive, negative, zero, odd or even number. ............................................................................................................................ Q3. Write a program in Java to display first 10 natural numbers. Find the sum of only odd numbers. .............................................................................................................................. Q4....

  • (C programing, Not C++) Write a program in C that asks the user to input 10...

    (C programing, Not C++) Write a program in C that asks the user to input 10 numbers. Store these numbers in an array. Then ask the user to input a single number. Your program should execute a linear search on the array, trying to find that number. If the number exists in the array, have the program print out which position/element the number was found at. If the target number is not in the array, have the program print out...

  • Please answer using C ++ programming language ONLY! We have only used <stdio.h> if that helps....

    Please answer using C ++ programming language ONLY! We have only used <stdio.h> if that helps. This is a basic course and the furthest we have gone is BASIC arrays. Write the code for the following problems using arrays. Write what would go in the “int main()” part of the program. For these problems, the entire program and program output is not needed. Just include what goes in the int main() part of the program. 1. Declare an integer array...

  • This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This...

    This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This program will use the ArrayManager class. The final version of the program is described below, but initially use it to test your ArrayManager class as you write it. Complete the ArrayManager class as specified below: The class will have exactly two instance variables:  An array of integers  A count of the number of elements currently in the array The class will have...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • C++ Program - Arrays- Include the following header files in your program:     string, iomanip, iostream Suggestion:...

    C++ Program - Arrays- Include the following header files in your program:     string, iomanip, iostream Suggestion: code steps 1 thru 4 then test then add requirement 5, then test, then add 6, then test etc. Add comments to display assignment //step 1., //step 2. etc. This program is to have no programer created functions. Just do everything in main and make sure you comment each step. Create a program which has: 1. The following arrays created:                 a. an array...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT