Question

Write a program that will create an array of ten integers, allow the user to enter...

Write a program that will create an array of ten integers, allow the user to enter those integers, and

eventually search through the array based on index position:

1.

Declare

the array. You cannot assign

elements

to an array until it has

first been created.

2.

Next, run a for loop so that the user can enter an integer to save in each

index position

of the array. Since you this array holds ten

elements

, the

index position

starts at 0 and ends at 9.

Think about this when creating the for loop

. How can you

make it so that each loop iteration is specific to

one

specific index

of the array?

3.

Once the array has been filled with values, allow the user to search for a specific element based on

index position

. Allow them to enter an

index position

, and then display the

element

at that given

position. Let the user keep searching until they enter -1.

4.

What happens when you search for an index position that doesn’t exist,

such as 10? Add some code that, if the user tries searching for an index

that is

out-of-bounds

, displays a message and asks for a new index.

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

Note: As language was not given so question is solved in C, C++ and java and it is done accordingly. Please comment for any problem. Please Uprate. Thanks.

C code:

#include<stdio.h>
void main(){
   int arrayOfInt[10]; // part 1
   int i;
   int choosenIndex=0;
   for(i=0;i<10;i++){ // part 2
       printf("Please give Integer for index %d :",i);
       scanf("%d",&arrayOfInt[i]);
   }
   while(choosenIndex!=-1){
       printf("Please give index of element to search(-1 to exit) :"); // part 3
       scanf("%d",&choosenIndex);
       if(choosenIndex>9 || choosenIndex<-1){ // part 4
       printf("Index out of bound.\n");
       continue;
       }else if(choosenIndex==-1){ // part 4
           continue;
       }else{
           printf("Value at index %d is %d\n",choosenIndex,arrayOfInt[choosenIndex]);
       }
   }
}

----------------------------------

c++ code:

#include<iostream>
using namespace std;

void main(){
   int arrayOfInt[10]; // part 1
   int i;
   int choosenIndex=0;
   for(i=0;i<10;i++){ // part 2
       cout<<"Please give Integer for index "<<i<<":";
       cin>>arrayOfInt[i];
   }
   while(choosenIndex!=-1){
       cout<<"Please give index of element to search(-1 to exit) :"; // part 3
       cin>>choosenIndex;
       if(choosenIndex>9 || choosenIndex<-1){ // part 4
       cout<<"Index out of bound.\n";
       continue;
       }else if(choosenIndex==-1){ // part 4
           continue;
       }else{
           cout<<"Value at index "<<choosenIndex<<" is "<<arrayOfInt[choosenIndex]<<endl;
       }
   }
   system("pause");
}

------------------

Java:

import java.util.Scanner;


public class main {
   public static void main(String args[]){
       int[] arrayOfInt=new int[10];// part 1
           int i;
           Scanner input=new Scanner(System.in);
           int choosenIndex=0;
           for(i=0;i<10;i++){ // part 2
               System.out.println("Please give Integer for index "+i+":");
               arrayOfInt[i]=input.nextInt();
           }
           while(choosenIndex!=-1){
               System.out.println("Please give index of element to search(-1 to exit) :"); // part 3
               choosenIndex=input.nextInt();;
               if(choosenIndex>9 || choosenIndex<-1){ // part 4
                   System.out.println("Index out of bound.\n");
               continue;
               }else if(choosenIndex==-1){ // part 4
                   continue;
               }else{
                   System.out.println("Value at index "+choosenIndex+" is "+arrayOfInt[choosenIndex]+"\n");
               }
           }
   }
}

Output:

C

C++ :

Java :

Add a comment
Know the answer?
Add Answer to:
Write a program that will create an array of ten integers, allow the user to enter...
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
  • For Java - Write a program that creates an array of 10 integers. Ask the user...

    For Java - Write a program that creates an array of 10 integers. Ask the user to enter the subscript (index) of an element, then displays the element located at that subscript. If the subscript entered is out of bounds (less than 0 or greater than length - 1), display the message "Out of Bounds".

  • Write a program that asks the user to input 10 integers of an array. The program...

    Write a program that asks the user to input 10 integers of an array. The program then inserts a new value at position (or index) given by the user, shifting each element right and dropping off the last element. For example, in the sample input/output below, the program will insert the value 30 at index 3. All the previous numbers of the array starting from position 3 (i.e., 7 1 20 9 23 8 22 will be shifted one position...

  • (ArrayIndexOutOfBoundsException) Write a program that meets the following requirements: ■ Creates an array with 100 randomly...

    (ArrayIndexOutOfBoundsException) Write a program that meets the following requirements: ■ Creates an array with 100 randomly chosen integers. ■ Prompts the user to enter the index of the array, then displays the corresponding element value. If the specified index is out of bounds, display the message Out of Bounds. Following modifications: * Use either a Scanner object or JOptionPane.showInputDialog() for input. * Use either System.out.println() or JOptionPane.showMessageDialog() for output. * In addition to ArrayIndexOutOfBoundsException, also catch InputMismatchException or NumberFormatException, and...

  • Write code that dynamically allocates an array of 20 integers, then uses a loop to allow...

    Write code that dynamically allocates an array of 20 integers, then uses a loop to allow the user to enter values for each element of the array. In C++ please, do not google, thanks!

  • Please help C++ language Instructions Write a program that prompts the user to enter 50 integers...

    Please help C++ language Instructions Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are sum of two other array elements. If an array element is the sum of two other array elements, then for this array element, the program should output all such pairs.

  • JAVA: (15 marks) Write a program that creates an integer array with 50 random values, prompts...

    JAVA: (15 marks) Write a program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. "Out of Bounds") and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • In Java - Write a program that prompts the user to enter two integers and displays...

    In Java - Write a program that prompts the user to enter two integers and displays their sum. If the input is incorrect, prompt the user again. This means that you will have a try-catch inside a loop.

  • Write a program that initializes an array with ten random integers and then prints out the...

    Write a program that initializes an array with ten random integers and then prints out the following: Every element at an even index; Every even element All elements in reverse order; Only the first and last elements; The minimum and maximum element The sum of all elements The alternating sum of all elements, where the alternating sum contains all elements at even index added, and the elements at odd index subtracted. Please write comments above the piece of code that...

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

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