Jump to content

2D arrays will be the life of me, SO close to finished. Dont understand drivers!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
Well im so close to being finished with this project. I THINKKKK..... it doesnt run, but i cant find any errors with it.

this is the majority of the assignment

Quote

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.

i think what i have for code is write, but can someone help me with this driver? did i do it right?
rep for help
thanks everyone




#include <iostream> 

#include <iomanip> 

#include <cstdlib> 

#include <ctime>

using namespace std;


void fill(int a[][9], int row);

void print(int a[][9], int row);

int howMany(int a[][9], int num);


//===========================================================


int main()

{

    int array[10][9];    

    int num = 0, occ;


        fill(array,9);

        print(array,9);

    

        cout << "Enter the number to search for: ";

        cin >> occ;

        occ = howMany(array, num);


        while(num = true)

        {    

            cout << "Number of occurrences; " << occ << endl;

        }

            cout << "Not found " << endl;


   return 0;

}


//===========================================================


void fill(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 print(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[10][10] = r;   //i dont really know what this line is for? lol

        //     a[9][9] = c;      

         count++;



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


         }

      }

    }

    return count;

}


OK changes: i got the code to run, but it doesnt run right, it just outputs number of occurances 0 a million times.

UGH SO CLOSE

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
While (num = true) assigns the value 1 to num, throwing an infinite loop. There's also no way to break out of your loop, even if you used while (num == true).
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
so what do you suggest i do? :( is my code ruined

#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
If I understand, rather than a loop:
while(num = true)
you need a conditional:
if(num > 0)


#5
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Let's look at what was most recently posted.
   cout << "Enter the number to search for: ";

   cin >> [COLOR="Magenta"]occ[/COLOR];

   [COLOR="Magenta"]occ[/COLOR] = howMany(array, num);
This says prompt and read an integer value into occ. Then immediately discard what the user entered and instead assign to occ the result of running the howMany function with the array and a value of 0 as parameters. Probably not what you intend to mean.

Let's try re-"wording" this a bit...
   cout << "Enter the number to search for: ";

   cin >> [COLOR="Blue"]num[/COLOR];

   occ = howMany(array, [COLOR="Blue"]num[/COLOR]);
Prompt and take user input. Feed the user-entered value to the function along with the array and assign the result to occ. Maybe that makes more sense.

As mentioned, the while loop makes no sense. You're doing an if-else type of thing: if you have more than zero, show the number; if you have zero, say none found.
   if ( num > 0 )

   {

      cout << "Number of occurrences; " << occ << endl;

   }

   else

   {

      cout << "Not found " << endl;

   }
But really, if you have 1, 2, 10, or 0 occurrences, the same output statement makes sense.

Incidentally, array[10][9] has 10 "rows" and 9 "columns". So let's simplify things a bit.
int main()

{

   int array[[COLOR="Blue"]10[/COLOR]][9];    

   int num, occ;


   fill(array,[COLOR="Blue"]10[/COLOR]);

   print(array,[COLOR="Blue"]10[/COLOR]);


   cout << "Enter the number to search for: ";

   cin  >> [COLOR="Red"]num[/COLOR];


   [COLOR="Purple"]occ[/COLOR] = howMany(array, [COLOR="Red"]num[/COLOR]);

   cout << "Number of occurrences; " << [COLOR="Purple"]occ[/COLOR] << endl;


   return 0;

}
This says:
  • Declare an array and a variable to receive the input.
  • Fill the array, then print it (I'd add some newlines in the output function so it looks better.)
  • Prompt for and take input.
  • Display the results of finding the number of occurrences of the user-entered value.


#6
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
hmmmm alright, what you said is making sense, now i need to figure out how to paste all that and get it to work in my code! i'll check in with my progress soon
thank you!

#7
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
OK so, it runs, and looks perfect, but after it tallies everything up i get this messsage





#include <iostream> 

#include <iomanip> 

#include <cstdlib> 

#include <ctime>

using namespace std;


void fill(int a[][9], int row);

void print(int a[][9], int row);

int howMany(int a[][9], int num);


//===========================================================


int main()

{

	int array[10][9];    

	int num, occ;


	fill(array,10);

	print(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 fill(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 print(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[10][10] = r;   //i dont really know what this line is for 

				a[9][9] = c;      

				count++;



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


			}

		}

	}

	return count;

}









Posted Image

why is there a zero row and column? how can i change it to start at row/column 1??

what do you think that error means?
thanks so far everyone

#8
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
This code attempts to assign outside of array bounds.
a[10][10] = r;   //i dont really know what this line is for 
Apparently, that code is causing a crash. :P

#9
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
stupid me, i meant to take that out.... :doh:

do you know how i can fix that row and column of 0? to start at 1?

#10
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
If i is 0, and you want to print 1, try printing i + 1.