hw assignment asks 4 the construction of a 2d array with the generation of random numbers 1-10 which has been done.
next ask the user 4 which numbers occurence in the array he wants to find and its location.
i have come up with this so far but the occurence part does not work right. need some plz.
thanks

#include <iostream>

#include <ctime>

using namespace std;

void fillar(int ar[][9], int numRows);

void printar(const int ar[][9], int numRows);

int howmanyar(int x , const int ar[][9], int numRows);

int main()
{
	int userinput;

	srand(time(0));

int ar[10][9]; 

fillar(ar, 10);

printar(ar, 10);

cout<<"Enter the number whose occurence and location you want to find from the table:"<<endl;

cin>>userinput;

int x = howmanyar(userinput, ar, 10);

cout<<"The number has an occurence of "<<x<<endl;

return 0;
}

void fillar(int ar[][9], int numRows)
{
	for(int row = 0; row < numRows; row++)
   {
	   for(int col = 0; col < 9; col++)
	  {
		  ar[row][col] = (rand() % 10) +1;
	  }
   }
}

void printar(const int ar[][9], int numRows)
{ 
	for(int row = 0; row < numRows; row++)
	{
		for(int col = 0; col < 9; col++)
		{
			cout << ar[row][col] << "\t";
		}
	  cout<<endl;
   }
}

int howmanyar(int x , const int ar[][9], int numRows)
{
	 int a = 0;

    for (int i = 0; i < numRows; i++)
	{
        if(ar[i][i] == x)
			a++;
	}
	return a;
}

Here's your code USING CODE-TAGS and indention. Notice how readable it is?

#include <iostream>

#include <ctime>

using namespace std;

void fillar(int ar[][9], int numRows);
void printar(const int ar[][9], int numRows);
int howmanyar(int x , const int ar[][9], int numRows);

int main()
{
    int userinput;
    srand(time(0));
    int ar[10][9];
    fillar(ar, 10);
    printar(ar, 10);
    cout<<"Enter the number whose occurence and location you want to find from the table:"<<endl;
    cin>>userinput;
    int x = howmanyar(userinput, ar, 10);
    cout<<"The number has an occurence of "<<x<<endl;
    cin.ignore();
    cin.get();
    return 0;
}

void fillar(int ar[][9], int numRows)
{
    for(int row = 0; row < numRows; row++)
    {
        for(int col = 0; col < 9; col++)
        {
            ar[row][col] = (rand() % 10) +1;
        }
    }
}

void printar(const int ar[][9], int numRows)
{
    for(int row = 0; row < numRows; row++)
    {
        for(int col = 0; col < 9; col++)
        {
            cout << ar[row][col] << "\t";
        }
        cout<<endl;
    }
}

int howmanyar(int x , const int ar[][9], int numRows)
{
    int a = 0;

    for (int i = 0; i < numRows; i++)
    {
        if(ar[i][i] == x)
            a++;
    }
    return a;
}

The problem is in your howmanyar() function. This loop:

for (int i = 0; i < numRows; i++)
    {
        if(ar[i][i] == x)
            a++;
    }

Will loop from ar[0][0] to ar[1][1] to arr[2][2], you see? If not: write the loop down on paper and see what it does.

To solve the problem you will need 2 nested loops (as you have used in the printar() function). One loops from 0 to cols, and one loops from 0 to rows.

for (int i = 0; i < numRows; i++)
    {
        for (int j = 0; j < 9 ; j++) // I used '9', but you can give an extra var to the function
        {
            if(ar[i][j] == x)
                a++;
        }
    }

wow..thnx man..that was fast..appreciate it..any ideas on how i can output each location of the value the user wants in the array

Just print out the values of 'i' and 'j' when you found a number, those are your coordinates after all.

Another thing: Do NOT PM me for help. Even if your problem is urgent to you it isn't to me. Pm-ing for help makes me not want to help you anymore.

ok..i kinda knew i had to that..but not too sure how to go about it, as far as having to pass the co-ordinate of each location its generated

How about letting the howmany() function take care of that. The function already looks for the number and counts it if it was found, so it's save to assume that the function knows the coordinates of the number right?

I'll try to be even less subtle with my hints:

for (int i = 0; i < numRows; i++)
    {
        for (int j = 0; j < 9 ; j++) 
        {
            if(ar[i][j] == x)
            {
                 // what would happen if you'd print 'i' and 'j' here??
                a++;
             }
        }
    }

well i tried it but its not workin..and my professor doesnt want couts inside my function..

think this is what u meant hopefully:

int howmanyar(int x, const int ar[][9], int numRows)
{ 
	int a = 0;

	for (int i = 0; i < numRows; i++)
    {
		for (int j = 0; j < 9 ; j++) 
        {
            if(ar[i][j] == x)
               
				cout<<x<<" has row "<<i<<" and column "<<j<<endl;

				a++;
        }

	}
	
	return a;
}

yeah i think the problem is im returning a value so i cant have the cout there

Allmost, you forgot the bracket around the if-statement:

if(ar[i][j] == x)
{
    cout<<x<<" has row "<<i<<" and column "<<j<<endl;
    a++;
}

And please learn how to use code-tags

.and my professor doesnt want couts inside my function..

I don't really see a beginner solution to solve this problem otherwise. You could pass a vector of coordinates by reference and push all the coordinates in it, but I'm pretty sure that's not what the prof meant.

How about asking him what is and isn't allowed?

yeah ill double check this..but this still isnt workin..got the exact same thing

#include <iostream>

#include <ctime>

using namespace std;

void fillar(int ar[][9], int numRows);

void printar(const int ar[][9], int numRows);

int howmanyar(int x , const int ar[][9], int numRows);

int main()
{
	int userinput;

	srand(time(0));

int ar[10][9]; 

fillar(ar, 10);

printar(ar, 10);

cout<<"Enter the number whose occurence and location you want to find from the table:"<<endl;

cin>>userinput;

int x = howmanyar(userinput, ar, 10);

cout<<"The number has an occurence of "<<x<<"and cordinates are as follows :"<<endl;

return 0;
}

void fillar(int ar[][9], int numRows)
{
	for(int row = 0; row < numRows; row++)
   {
	   for(int col = 0; col < 9; col++)
	  {
		  ar[row][col] = (rand() % 10) +1;
	  }
   }
}

void printar(const int ar[][9], int numRows)
{ 
	for(int row = 0; row < numRows; row++)
	{
		for(int col = 0; col < 9; col++)
		{
			cout << ar[row][col] << "\t";
		}
	  cout<<endl;
   }
}

int howmanyar(int x, const int ar[][9], int numRows)
{ 
	int a = 0;

	for (int i = 0; i < numRows; i++)
    {
		for (int j = 0; j < 9 ; j++) 
        {
            if(ar[i][j] == x)
			{
				cout<<x<<" has row "<<i<<" and column "<<j<<endl;
	
				a++;
			}
			
		}

	}
	
	return a;
}

yeah ill double check this..but this still isnt workin..got the exact same thing

Works fine for me. It gives out all the coordinates. Saying: "isn;t working" doesn't help me understand your problem. Be specific.

its not printing out the corodinates..only displays occurances

or nevermind i got it..dont know what was going on...thnx 4 ur help

the code (not changed since your last post)

#include "stdafx.h"
#include <iostream>
#include <stack>
#include <string>

#include <iostream>

#include <ctime>

using namespace std;

void fillar(int ar[][9], int numRows);

void printar(const int ar[][9], int numRows);

int howmanyar(int x , const int ar[][9], int numRows);

int main()
{
    int userinput;
    srand(time(0));
    int ar[10][9]; 
    fillar(ar, 10);
    printar(ar, 10);
    cout<<"Enter the number whose occurence and location you want to find from the table:"<<endl;
    cin>>userinput;
    int x = howmanyar(userinput, ar, 10);
    cout<<"The number has an occurence of "<<x<<"and cordinates are as follows :"<<endl;
    cin.ignore();     
    cin.get();
    return 0;
}

void fillar(int ar[][9], int numRows)
{
    for(int row = 0; row < numRows; row++)
    {
        for(int col = 0; col < 9; col++)
        {
            ar[row][col] = (rand() % 10) +1;
        }
    }
}

void printar(const int ar[][9], int numRows)
{ 
    for(int row = 0; row < numRows; row++)
    {
        for(int col = 0; col < 9; col++)
        {
            cout << ar[row][col] << "\t";
        }
        cout<<endl;
    }
}

int howmanyar(int x, const int ar[][9], int numRows)
{ 
    int a = 0;

    for (int i = 0; i < numRows; i++)
    {
        for (int j = 0; j < 9 ; j++) 
        {
            if(ar[i][j] == x)
            {
                cout<<x<<" has row "<<i<<" and column "<<j<<endl;

                a++;
            }
        }
    }
    return a;
}

Output in attachment.

If you're talking about that the coordinates don't magicly show up after "and cordinates are as follows :", then all I can say is: duh

[edit]

Ok, good to see that we're now on one line :)

Attachments out.JPG 31.44 KB

lol..yeah..saw tht..but just got 1 final question and that is how would i do it if he didnt want the couts there..and without the use of vectors..im only in a level 1 class

Create a separate function for it that's searches for coordinates and prints them.

I would still (for the record) like to point out, that I think your teacher is wrong in this case. It causes a lot of overhead creating/calling a function to display the coordinates. Most of the code that you'd have to create is also in the howmanyar() function...

yeah i knw its pretty much gona be the same code but i still dont really see it..incase i do have to change it i will probably need ur guidance..i spent a gud couple of hours on this..thnx again

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