Hello, guys, I got stuck on this problem. The program below is fully functional
ID: 3889582 • Letter: H
Question
Hello, guys, I got stuck on this problem. The program below is fully functional but now I wanted to add another function where I can search and output for the location of the number that user input and so how can we do that. Would much appreciate for fully slightly modified program base on mine, thank you so much for helping.
#include<iostream>
using namespace std;
void fillArray(int a[][5], int size);
void printArray(int a[][5], int size);
int countNums(int ar[][5], int size, int search);
int main()
{
int ar[5][5];
fillArray(ar, 5);
printArray(ar, 5);
cout << "What number do you want to search for? " << endl;
int num;
cin >> num;
int count = countNums(ar, 5, num);
cout << "Your Number Appears " << count << " Times in the Array" << endl;
return 0;
}
void fillArray(int a[][5], int size)
{
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 5; col++)
{
a[row][col] = rand() % 10 + 1;
}
}
}
void printArray(int a[][5], int size)
{
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 5; col++)
{
cout << a[row][col] << " ";
}
cout << endl;
}
}
int countNums(int ar[][5], int size, int search)
{
int count = 0;
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 5; col++)
{
if (ar[row][col] == search)
count++;
}
}
return count;
}
#include using namespace std; void fil1Array(int a[][5], int size); void printArray(int a[][5], int size); int countNums (int ar[][5], int size, int search); int mainO 7 int ar[5]05] fillArray(ar, 5); printArray (ar, 5); 10 14 15 16 17 cout "What number do you want to search for? " endl; int num; cin > num; int count -countNums (ar, 5, num); 19 20 coutExplanation / Answer
Thank you for taking facility from us.you almost did the search .i just add a simple function named searchArray.hope this help you.
#include<iostream>
using namespace std;
void fillArray(int a[][5], int size);
void searchArray(int a[][5],int size,int i);
void printArray(int a[][5], int size);
int countNums(int ar[][5], int size, int search);
int main()
{
int ar[5][5];
fillArray(ar, 5);
printArray(ar, 5);
cout << "What number do you want to search for? " << endl;
int num;
cin >> num;
searchArray(ar,5,num);
int count = countNums(ar, 5, num);
cout << "Your Number Appears " << count << " Times in the Array" << endl;
return 0;
}
void fillArray(int a[][5], int size)
{
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 5; col++)
{
a[row][col] = rand() % 10 + 1;
}
}
}
void printArray(int a[][5], int size)
{
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 5; col++)
{
cout << a[row][col] << " ";
}
cout << endl;
}
}
int countNums(int ar[][5], int size, int search)
{
int count = 0;
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 5; col++)
{
if (ar[row][col] == search)
count++;
}
}
return count;
}
void searchArray(int a[][5],int size,int i)
{
for(int row =0;row<5;row++)
{
for(int col=0;col<5;col++)
{
if (i==a[row][col])
{
cout<<"the location is "<<row <<col <<endl;
}
}
}
}
the searcharray function searching for the item inputted by the user and display the position of the number.
hope this might help you.see you again
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.