I have written a code that will post random numbers into a 10x10 row/col setup. I'm trying to edit the last function of code so that the user see's something like this:

Input a number
5
5 was found 3 times
5 is located at row 2, col 1; row 5, col 2 ; row 8, col 2

The current code does everything except output the last line... I've done some edits to the original code and below is what I got, can you help me figure out where Im going wrong please.. ??

#include <iostream>
#include <ctime>
using namespace std;
void fillArray(int ar[][10], int size);
void printArray(int ar[][10], int size);
int countNum(int ar[][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;
	cout<< count << " is located at col " << countNum(ar,10,col)<<" row " <<countNum(ar,10,row)<<endl;

	//return 0;

}
void fillArray(int 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]<< "\t";
		}
		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)
		{
			for(int row =0; row<10; ++row)
			{
				cout << ar[col][row]<<" ";
			}
			cout <<endl;
		for (int col = 0; col < 10; col++)
		{
			if (a[row][col]== find)
				count++;
		}
		
}
		return count;
}

Edited 6 Years Ago by cortez716: n/a

1) First, you should include cstdlib:

#include <cstdlib>

Not doing so causes compiler errors on many systems.

2) Another, more serious error is:

cout<< count << " is located at col " << countNum(ar,10,col)<<" row " <<countNum(ar,10,row)<<endl;

'col' and 'row' are not defined! You need to return them by reference from your countNum function I would assume (see #3).

3) COMMENT COMMENT COMMENT!!! There is no reason to shorten variable names ("ar" I'm assuming is array?) Also, your functions should have comments describing what they do. What does countNum do? What is the input argument 'size'? What about 'find'? Writing these things will help you keep track of what is going on.

David

This article has been dead for over six months. Start a new discussion instead.