First of all fix this code because it has some errors and then modify countNum s
ID: 3569729 • Letter: F
Question
First of all fix this code because it has some errors and then modify countNum so that it will actually print out the location in row column where each of them was found.
#include <iostream>
#include <ctime>
using namespace std;
void fillArray(ar[][10], int size);
void printArray(int ar[][10], int size);
int countNum(int a[][10], int size, int find);
int main()
{
srand((unsigned int)time(0));
int ar[10][10];
fillArray(ar, 10);
printArray(ar, 10);
int count;
cout << "Input a number" << endl;
cin >> count;
cout << count << " was found " << countNum(ar, 10, count) << " times" << endl;
return 0;
}
void fillArray(ar[][10], int size)
{
for (int row = 0; row < size; row++)
{
for (int col = 0; col < 10; col++)
{
ar[row][col] = rand() % 101;
}
}
}
void printArray(int ar[][10], int size)
{
for (int row = 0; row < size; row++)
{
for (int col = 0; col < 10; col++)
{
cout << ar[row][col] << " ";
}
cout << endl;
}
}
int countNum(int a[][10], int size, int find)
{
int count = 0;
for (int row = 0; row < size; row++)
{
for (int col = 0; col < 10; col++)
{
if (a[row][col] == find)
count++;
}
}
return count;
}
Explanation / Answer
#include <iostream>
#include <ctime>
#include<cstdlib>
using namespace std;
void fillArray(int ar[][10], int size);
void printArray(int ar[][10], int size);
int countNum(int a[][10], int size, int find);
int main()
{
srand((unsigned int)time(0));
int ar[10][10];
fillArray(ar, 10);
printArray(ar, 10);
int count;
cout << "Input a number" << endl;
cin >> count;
cout << count << " was found " << countNum(ar, 10, count) << " times" << endl;
return 0;
}
void fillArray(int ar[][10], int size)
{
int row,col;
for (int row = 0; row < size; row++)
{
for (int col = 0; col < 10; col++)
{
ar[row][col] = rand() % 101;
}
}
}
void printArray(int ar[][10], int size)
{
int row,col;
for (int row = 0; row < size; row++)
{
for (int col = 0; col < 10; col++)
{
cout << ar[row][col] << " ";
}
cout << endl;
}
}
int countNum(int a[][10], int size, int find)
{
int count = 0,row,col;
for (int row = 0; row < size; row++)
{
for (int col = 0; col < 10; col++)
{
if (a[row][col] == find)
count++;
}
}
return count;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.