Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

4. Thirty random numbers between 1 and 20 are entered into an array named nums a

ID: 3904351 • Letter: 4

Question

4. Thirty random numbers between 1 and 20 are entered into an array named nums and the number to be searched is entered through the keyboard by the user. Write a C program, numSearch.c, to display the generated random numbers and find if the number to be searched is present in the array and if it is present, display the number of times it appears in the array. The user can enter more than one number to be tested for search. The program terminates when the user enters a number that is less or equal to 0. A Sample interaction is as follow Generated numbers are: 4, 7,18,16,14,16, 7,13,10,2 3, 8,11,20, 4, 7, 1,7,13,17, 12, 9, 8,10, 3,11, 3,4,8,16 Enter a number to be found(-0 for exit): 13 13 appears 2 time(s). Enter a number to be found (

Explanation / Answer

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main() {
   srand(time(NULL));
   int *arr = new int[30];
   for(int i = 0; i < 30; ++i) {
       arr[i] = rand() % 20 + 1;
   }
   cout << "Generated numbers are:" << endl;
   for(int i = 0; i < 30; ++i) {
       cout << arr[i] << ", ";
       if((i+1) % 10 == 0) {
           cout << endl;
       }
   }
   int num, count;
   while(true) {
       cout << "Enter a number to be found(<=0 for exit): ";
       cin >> num;
       if(num <= 10) {
           break;
       }
       count = 0;
       for(int i = 0; i < 30; ++i) {
           if(arr[i] == num) {
               count++;
           }
       }
       cout << num << " appears " << count << " time(s)." << endl;
   }
   cout << "END." << endl;
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote