1 Replies - 1086 Views - Last Post: 11 November 2008 - 09:00 PM Rate Topic: -----

#1 FtK Shadow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-September 08

Find in Array

Posted 11 November 2008 - 08:47 PM

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.


Quote

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;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Find in Array

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: Find in Array

Posted 11 November 2008 - 09:00 PM

You corrupted the stack by calling an array index out of bounds here:

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][8] = c;      //error was here, second demin. was 9, out of bounds
//changed it to 8 and now its all good to go.
                count++;


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

            }
        }
    }
    return count;
}



Remeber that an array's index is 0-n, n being the number declared with [].

Was that helpful? :)

This post has been edited by KYA: 11 November 2008 - 09:01 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1