Okay heres the background on the code. I've written a code that prints a 10x10 array or random numbers. Then prompts the user to pick a number. After user picks a number, the program then tells user the number of times number was found in the array and supossed to give the location of that number.. Everything in the code worked up until my printLocations function.. Meaning that program would print the array take input, and tell user howmany times found, but not returning the location. Here is the code below, can you tell me how i can fix the printLocations function, and 2nd am i calling it correctly in main:

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void fillArray(int ar[][10], int size);
void printArray(int ar[][10], int size);
void printLocations(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];
	//int a;
	fillArray(ar, 10);
	printArray(ar,10);
	printLocations(ar,10);
	int count;
	int row;
	int col;
	cout<<" Input a number " <<endl;
	cin >> count;
	cout<< count << " was found "<< countNum(ar,10,count)<< " times "<<endl;
	cout << count <<" is located at row: "<< row << " col: "<<col;
	cout << endl;



	return 0;

}
void fillArray(int ar[][10], int size)//fills the array with random numbers in 10 rows and colums
{
	for(int row = 0; row < size; row++)
	{
		for (int col = 0; col < 10; col++)
		{
			ar[row][col] = rand() % 10;
		}
	}
}
void printArray(int ar[][10], int size)//prints the array neatly
{
	for(int row = 0; row < size; row++)
	{
		for (int col = 0; col < 10; col++)
		{
			cout <<ar[row][col]<< "\t";
		}
		cout<<endl;
	}
}
void printLocations(int ar[][10], int size)
{
	int count = 0;
	int find;
	for(int row = 0; row < size; row++)
	{
		for (int col = 0; col < 10; col++)
		{
			
			if (ar[row][col]== find)
			printLocations  ar[row][col];
			
		}

		
}
		
} 




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++;
			//if (a[row][col]== find)
			//cout<<  " that number is found in col " << col << " row "<< row <<endl;
		}

		
}
		return count;
}

Okay heres the background on the code. I've written a code that prints a 10x10 array or random numbers. Then prompts the user to pick a number. After user picks a number, the program then tells user the number of times number was found in the array and supossed to give the location of that number.. Everything in the code worked up until my printLocations function.. Meaning that program would print the array take input, and tell user howmany times found, but not returning the location. Here is the code below, can you tell me how i can fix the printLocations function, and 2nd am i calling it correctly in main:

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void fillArray(int ar[][10], int size);
void printArray(int ar[][10], int size);
void printLocations(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];
	//int a;
	fillArray(ar, 10);
	printArray(ar,10);
	printLocations(ar,10);
	int count;
	int row;
	int col;
	cout<<" Input a number " <<endl;
	cin >> count;
	cout<< count << " was found "<< countNum(ar,10,count)<< " times "<<endl;
	cout << count <<" is located at row: "<< row << " col: "<<col;
	cout << endl;



	return 0;

}
void fillArray(int ar[][10], int size)//fills the array with random numbers in 10 rows and colums
{
	for(int row = 0; row < size; row++)
	{
		for (int col = 0; col < 10; col++)
		{
			ar[row][col] = rand() % 10;
		}
	}
}
void printArray(int ar[][10], int size)//prints the array neatly
{
	for(int row = 0; row < size; row++)
	{
		for (int col = 0; col < 10; col++)
		{
			cout <<ar[row][col]<< "\t";
		}
		cout<<endl;
	}
}
void printLocations(int ar[][10], int size)
{
	int count = 0;
	int find;
	for(int row = 0; row < size; row++)
	{
		for (int col = 0; col < 10; col++)
		{
			
			if (ar[row][col]== find)
			printLocations  ar[row][col];
			
		}

		
}
		
} 




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++;
			//if (a[row][col]== find)
			//cout<<  " that number is found in col " << col << " row "<< row <<endl;
		}

		
}
		return count;
}

Syntactically your function call has no problem:

printLocations(ar,10);

In function printLocations that's the line that has the problem:

printLocations  ar[row][col];

You're trying to use recursion...and i don't think you need this. What do you want to do with your printLocation function? You want to return the row and column that you found the number? If you do then you just have to modify your countNum function..

When u say modify my countNum function, what do you mean? I understand how to do it if i'm using "cout" in my function there, but I'm sure there is a way to do it wihthout using "cout" , by creating a function to caluculate the row and col and simply call the results from the printLocations functions into main... That is what Im trying to do? Can anyone point me in the right direction towards acheiving that?

When u say modify my countNum function, what do you mean? I understand how to do it if i'm using "cout" in my function there, but I'm sure there is a way to do it wihthout using "cout" , by creating a function to caluculate the row and col and simply call the results from the printLocations functions into main... That is what Im trying to do? Can anyone point me in the right direction towards acheiving that?

Add the parameters lines and columns in your countNum function. In these parameters you will pass the value of row and column you found your number. Then in main function you will have to declare 2 integer variables which will hold the values of row and column you want. Check this out:

int countNum(int a[][10], int size, int find, int& lines, int& columns)
{
int count = 0;

for(int row = 0; row < size; row++)
{
for (int col = 0; col < 10; col++)
{
if (a[row][col]== find;
count++;
 lines = row;
 columns = col; //at this point you pass the desired values in variables lines 
                //and columns 
}
 
}
return count;
}

Then in main function:

int lin, cols;
 .....
 cout<< count << " was found "<< countNum(ar, 10, count, lin, cols)<< " times at row " << lin << " and column " << cols << endl;

I think that'll work...:icon_wink:

=( I tried it to no avail

=( I tried it to no avail

Ok now i get it..you want to return all locations that the number exists..am i right?

Ok now i get it..you want to return all locations that the number exists..am i right?

I think that's what you meant..I made a 2d array inside the function findLocations and stored there all pairs of rows and columns you need. Then i printed the array of the locations. Test it and leave your feedback:

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

using namespace std;

void fillArray(int ar[][10], int size);
void printArray(int ar[][10], int size);
void findLocations(int ar[][10], int size, int find, int locations[][2]);
int countNum(int ar[][10], int size, int find);

int main()
{
srand((unsigned int) time(0));
int ar[10][10];
int count, times;
fillArray(ar, 10);
printArray(ar,10);

cout<<" Input a number " <<endl;
cin >> count;
times = countNum(ar, 10, count);
cout<< count << " was found "<< times << " times "<<endl;

int locations[times][2];
 findLocations(ar, 10, count, locations);
cout << count <<" was found at following locations: " << endl;
cout << "row:" << "\t" << "column:" << "\t" << endl;
  for (int i = 0; i < times; i++) {
   for (int j = 0; j < 2; j++)
    cout << locations[i][j] << "\t";
    cout << endl;
  }
cout << endl;
 return 0;
}

void fillArray(int ar[][10], int size)//fills the array with random numbers in 10 rows and colums
{
for(int row = 0; row < size; row++)
for (int col = 0; col < 10; col++)
ar[row][col] = rand() % 10;
}

void printArray(int ar[][10], int size)//prints the array neatly
{
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++)
if (a[row][col]== find)
count++;
}
int i = 0;

return count;
}

void findLocations(int ar[][10], int size, int find, int locations[][2]) {
 int i = 0;
for(int row = 0; row < size; row++)
 {
 for (int col = 0; col < 10; col++)
  if (ar[row][col] == find) {
  locations[i][0] = row;
  locations[i][1] = col;
  i++;
}
}
}

It built with 3 errors, looking over it right now to try and find errors

1>c:\users\XXXXXX\documents\visual studio 2008\projects\discussion 8b\two dimensional arrays\main.cpp(25) : error C2057: expected constant expression
1>c:\users\XXXXXXX\documents\visual studio 2008\projects\discussion 8b\two dimensional arrays\main.cpp(25) : error C2466: cannot allocate an array of constant size 0
1>c:\users\XXXXXXX\documents\visual studio 2008\projects\discussion 8b\two dimensional arrays\main.cpp(25) : error C2133: 'locations' : unknown size
1>Two Dimensional Arrays - 3 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

1>c:\users\XXXXXX\documents\visual studio 2008\projects\discussion 8b\two dimensional arrays\main.cpp(25) : error C2057: expected constant expression
1>c:\users\XXXXXXX\documents\visual studio 2008\projects\discussion 8b\two dimensional arrays\main.cpp(25) : error C2466: cannot allocate an array of constant size 0
1>c:\users\XXXXXXX\documents\visual studio 2008\projects\discussion 8b\two dimensional arrays\main.cpp(25) : error C2133: 'locations' : unknown size
1>Two Dimensional Arrays - 3 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Did you copy exactly the code i wrote and found errors? It works for me fine though..I ran it on Code::Blocks.

Yeah copied exactly... will try one more time.. brb

ok copied it, I changed "times" in the array to "2" and it compiled and ran, gave correct answer, however there is a "runtime error". What does "times" represent? Also I appreciate you being patience with me

ok copied it, I changed "times" in the array to "2" and it compiled and ran, gave correct answer, however there is a "runtime error". What does "times" represent? Also I appreciate you being patience with me

Variable times represents how many times the specific number exists..i'll post a version with a dynamic array in order to see what visual studio is going to say..

ok copied it, I changed "times" in the array to "2" and it compiled and ran, gave correct answer, however there is a "runtime error". What does "times" represent? Also I appreciate you being patience with me

I think that eventually this piece of code will work in either API. It's a version with a dynamic array:

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

using namespace std;

typedef int* IntP;

void fillArray(int ar[][10], int size);
void printArray(int ar[][10], int size);
void findLocations(int ar[][10], int size, int find, IntP *locations);
int countNum(int ar[][10], int size, int find);

int main()
{
srand((unsigned int) time(0));
int ar[10][10];
int count, times;
fillArray(ar, 10);
printArray(ar,10);

cout<<" Input a number " <<endl;
cin >> count;
times = countNum(ar, 10, count);
cout<< count << " was found "<< times << " times "<<endl;

IntP *locations = new IntP[times];

 for (int i = 0; i < times; i++)
  locations[i] = new int[2];

 findLocations(ar, 10, count, locations);
cout << count <<" was found at following locations: " << endl;
cout << "row:" << "\t" << "column:" << "\t" << endl;
  for (int i = 0; i < times; i++) {
   for (int j = 0; j < 2; j++)
    cout << locations[i][j] << "\t";
    cout << endl;
  }
 for (int i = 0; i < times; i++)
   delete [] locations[i];
   delete locations;
cout << endl;
 return 0;
}

void fillArray(int ar[][10], int size)//fills the array with random numbers in 10 rows and colums
{
for(int row = 0; row < size; row++)
for (int col = 0; col < 10; col++)
ar[row][col] = rand() % 10;
}

void printArray(int ar[][10], int size)//prints the array neatly
{
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++)
if (a[row][col]== find)
count++;
}
int i = 0;

return count;
}

void findLocations(int ar[][10], int size, int find, IntP *locations) {
 int i = 0;
for(int row = 0; row < size; row++)
 {
 for (int col = 0; col < 10; col++)
  if (ar[row][col] == find) {
  locations[i][0] = row;
  locations[i][1] = col;
  i++;
}
}
}

Leave your feedback..Cheers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.