0

I cant get this code to work it searches and finds the answer, but it gets an error once you search it. Heres the assignment.

a.) Create a function called howMany that will search a two dimensional array for the the number of occurences a particular number is found within the array. Your function should return the number of occurences the number appears within the array and the cooridnates that each one is found.

Your function at the very least should take as arguments the number to search for, and the two dimensional array. You are going to need some mechanisim to return the coordinates. You could use a string, an array, or a 2D array here but I will leave that up to you. This means your function needs at least 3 arguments!

b.) Create another function that will take as arguments integer values that represent row, column coordinates. return the value at that location

c.) Write a function that will fill the array with random numbers from 1 - 10. Initially, your array should be 10 rows by 9 columns

Finally write some kind of driver to demostrate the functionality of your program. It would be nice if you used some kind of a loop and a simple menu scheme.

#include <iostream> 
#include <iomanip> 
#include <cstdlib> 
#include <ctime>
using namespace std;

void fillarray(int a[][9], int row);
void show(int a[][9], int row);
int howMany(int a[][9], int num);


int main()
{
	int array[10][9];    
	int num, occ;

	fillarray(array,10);
	show(array,10);

	cout << "Enter the number to search for: ";
	cin  >> num;

	occ = howMany(array, num);
	cout << "Number of occurrences; " << occ << endl;

	return 0;


	if ( num > 0 )
	{
		cout << "Number of occurrences: " << occ << endl;
	}
	else {
		cout << "Not found " << endl;
	}

}

void fillarray(int a[][9], int row)
{
	srand(time(0));
	for(int row = 0; row < 10; row++)
	{
		for(int col = 0; col < 9; col++)
		{
			a[row][col] = (rand() % 10) + 1;
		}
	}
}


void show(int a[][9], int row)
{
	for(int row = 0; row < 10; row++)
	{
		for(int col = 0; col < 9; col++)
		{
			cout << a[row][col] << "\t";
		}
	}
}


int howMany(int a[][9], int num)
{
	int count = 0;

	for(int r = 0; r < 10; r++)
	{
		for(int c = 0; c < 9; c++)
		{
			if(a[r][c] == num)
			{
				a[9][9] = c;      
				count++;


				cout << "row: " << r << "\t"<< " col: " << c << " contains the  number " << num << endl;

			}
		}
	}
	return count;
}
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by vmanes
0

The error is caused by your writing a value past the end of the array's allocation. Line 74 a[9][9] = c; is the culprit. Remember, that for an array sized 10 x 9, the maximum index (the lower right corner) is [9][8]. What is the purpose of this line?

Also, in functions fillarray( ) and show( ) you pass the number of rows as a parameter, then declare a variable of the same name and use hard coded limit on number of rows. Use the parameter, it makes your function more capable, it could handle an array of any number of rows, like

void fillarray(int a[][9], int row)
{
	srand((unsigned)time(0));
	int r;
	int c;
	for( r = 0; r < row; r++)
	{
		for( c = 0; c < 9; c++)
		{
			a[r][c] = (rand() % 10) + 1;
		}
	}
}

Read the compiler's warnings - it should have told you about the unused function parameters. Also, looks at the code that occurs after the return statement in main( ) - it never, ever will be executed.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.