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

Write a C++ program that creates a 2­dimensional array of 10x5 elements that are

ID: 3844789 • Letter: W

Question

Write a C++ program that creates a 2­dimensional array of 10x5 elements that are populated by random numbers between 0 and 49 (hint: use the rand() function and re­use code from the multi­dimensional array demonstration in class). The program should then ask the user for a number between 0 and 49 and searches the array for that number. If it finds it, it returns both indicies of the 2­D array, otherwise, it returns a message that says “I did not find this number.” Re­run your program multiple times to make sure that it works (it should show you different answers each time).

Explanation / Answer

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

using namespace std;

int main() {
   srand(time(NULL)); //this makes sure that the numbers are randomized properly
   int randArr[10][5], guess,flag = 0;
   for (int i = 0; i<10; i++)
       for (int j = 0; j < 5 ; j++)
           randArr[i][j] = rand()%50;

   cout << "Enter number between 0 and 49: ";
   cin >> guess;
   for (int i = 0; i<10; i++)
       for (int j = 0; j < 5 ; j++)
           if (randArr[i][j] == guess) {
               cout << "found at location: row: " << i <<" column: "<<j <<endl;
               i = j = 10; //to break outer loop
               flag = 1;
           }
   if (flag == 0)
       cout << "I did not find this number." <<endl;
}

Here you go champ.... This code will help you. If you have any doubt, please feel free to comment below. I shall be glad to help you

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