Question

In: Computer Science

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 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

If you have any problem with the code feel free to comment.

Program

#include <iostream>

using namespace std;

int main( )
{
    //pointer to int
    int* ar = NULL;
    //getting the length of the array
    int len;
    cout<<"Please input the number of id numbers to be read"<<endl;
    cin>>len;

    //allocating space to the array according to len
    ar = new int[len];
    //taking id inputs
    for(int i=0; i<len; i++){
        cout<<"Please enter an id number"<<endl;
        cin>>ar[i];
    }

    //taking the search variable input
    int serach;
    cout<<"Please input an id number to be searched"<<endl;
    cin>>serach;

    //checking if the id for search is present in the array or not
    bool flag = false;
    for(int i=0; i<len; i++){
        if(serach == ar[i]){
            flag = true;
            break;
        }
    }
    if(flag)
        cout<<serach<<" is in the array"<<endl;
    else
        cout<<serach<<" is not in the array"<<endl;

    //freeing up the allocated space
    delete [] ar;
    ar = NULL;

    return 0;
}

Output


Related Solutions

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...
Using C# programming language, Write a program that sort three numbers entered by the user using...
Using C# programming language, Write a program that sort three numbers entered by the user using only if and nested if statements. If for instance the user entered 5, 2, and 7; the program should display 2,5,7.
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
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...
Code in C++ programming language description about read and write data to memory example.
Code in C++ programming language description about read and write data to memory example.
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT