Write a C++ program that creates a 2dimensional array of 10x5 elements that are
ID: 3844789 • Letter: W
Question
Write a C++ program that creates a 2dimensional array of 10x5 elements that are populated by random numbers between 0 and 49 (hint: use the rand() function and reuse code from the multidimensional 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 2D array, otherwise, it returns a message that says “I did not find this number.” Rerun 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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.