Question

In: Computer Science

C++. Write a program that will read in id numbers and place them in an array.The...

C++. Write a program that will read in id numbers and place them in an array.The array is dynamically allocated large enough to hold the number of id numbers given by the user. The program will then input an id and call a function to search for that id in the array. It will print whether the id is in the array or not.

Sample Run:

Please input the number of id numbers to be read

4

Please enter an id number

96

Please enter an id number

97

Please enter an id number

98

Please enter an id number

99

Please input an id number to be searched

67

67 is not in the array

Solutions

Expert Solution

ANSWER :

CODE :

#include <iostream>
using namespace std;
void search(int arr[], int n,int x)
{
int a = 0;
for(int i=0; i<n; i++)
{
if(arr[i] == x)
{
a= 1;
break;
}
}
if(a==0)
{
cout<<x<<" is not in the array";
}
else
{
cout<<x<<" is in the array";
}
}
int main()
{
int n;
int x;
cout<<"Please input the number of id numbers to read"<<endl;
cin>>n;
int arr[n];
for(int i=0; i<n; i++)
{
cout<<"Please enter an id number"<<endl;
cin>>arr[i];
}
cout<<"Please input an id number to be searched"<<endl;
cin>>x;
search(arr,n,x);
return 0;
}
OUTPUT :


Related Solutions

C++ programming language. Write a program that will read in id numbers and place them in...
C++ programming language. Write a program that will read in id numbers and place them in an array.The array is dynamically allocated large enough to hold the number of id numbers given by the user. The program will then input an id and call a function to search for that id in the array. It will print whether the id is in the array or not. Sample Run: Please input the number of id numbers to be read 4 Please...
C++ Program: Write a program that prompts the user for two numbers and stores them in...
C++ Program: Write a program that prompts the user for two numbers and stores them in signed integers. The program should then add those two numbers together and store the result in a signed integer and display the result. Your program should then multiply them by each other and store the result in another integer and display the result. Then do the same but with dividing the first number by the second. Display an error message to the screen if...
write a c++ program that prompts a user to enter 10 numbers. this program should read...
write a c++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the two numbers and the average of the 10 numbers PS use file I/o and input error checking methods
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write a C++ program to read in a list of 10 integers from the keyboard. Place...
Write a C++ program to read in a list of 10 integers from the keyboard. Place the even numbers into an array called even, the odd numbers into an array called odd, and the negative numbers into an array called negative. Keep track of the number of values read into each array. Print all three arrays after all the numbers have been read. Print only the valid elements (elements that have been assigned a value). a. Use main( ) as...
Write a C++ program to read a collective of integer numbers. I f the number is...
Write a C++ program to read a collective of integer numbers. I f the number is greater than zero and less than 15 then terminate the loop and find factorial of the number
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
C++ Write a whole program to read 50 salaries from the file “Salaries.txt” and print them...
C++ Write a whole program to read 50 salaries from the file “Salaries.txt” and print them on the screen (each value on a separate line), you have to also save the grades (each value on a separate line) into another file “SalariesWithBonus.txt” after you add a bonus of two percent to each salary (example For the salary 100000 a bonus of 2000 will be added as 100000 X 0.02 = 2000). Your code should check whether the file “Salaries.txt” opened...
in c, write a probram thats will read from the user: id, names, and marks for...
in c, write a probram thats will read from the user: id, names, and marks for a chosen number of students. the program should he able to read the total number of students from the user. Then should be able to read the id, name, and marks for each student and store it into three arrays, where each record is given in a line of text, with id, name, and marks seperated by a comma. The use of scanf is...
Write a program in Easy68K: a) Define an array of numbers in the memory. b) Read...
Write a program in Easy68K: a) Define an array of numbers in the memory. b) Read two numbers from keyboard. The first number is the size of the array and the second number is what index of the array you want to access. The index you entered can be larger than the array. c) Display the element indexed by (index % size) in the array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT