Hello, I'm am really new to C++; I've only been doing it for a couple of months. However, in my class, I have no idea what I've been doing wrong...I'm posting my program and the assignment because I desperately need the insight. I'm not sure if I am in the wrong or if I'm being singled out for some strange reason. I do know that I am tired of dealing with a rude instructor....
So, here is my code:

#include <iostream>
#include <iomanip>
#include <ctime>

using namespace std;
int arg[10][9];
int howMany( int num);
int randNum();
int numValue(int row, int col);

int howMany(int num)
{
    int counter = 0;

    for(int row = 0; row < 10; row++)
        for(int col = 0; col < 9; col++)
        {
            if(arg[row][col] == num)
            {
                counter++;
            }
        }

        return counter;
}

int randNum()
{
    int nValue = 0;
    int row;
    int col;
    srand((unsigned)time(0));
    for (row = 0; row < 10; row++)
        for (col = 0; col < 9; col++)
        { 
            arg[row][col] = ((rand() % 10)+ 1);
        }

        nValue = arg[row][col];

    return nValue;
}

int numValue(int row, int col)
{
    int value = arg[row][col];


    return value;
}

int main()
{
    int row = 0;
    int col = 0;
    int value = 0;
    int counter = 0;
    int num = 0;
    int nValue = 0;

    for(int row = 0; row < 10; row++)
        for(int col = 0; col < 9; col++)
        {
            randNum();
            cout << arg[row][col] << "\t";
        }
        cout << endl;

    cout << "Enter a number for the computer to search for: " << endl;
    cin >> num;
    cout << "Your number occurs " << howMany( num) << " times." << endl;

    cout << "Enter a row and a column to search for: " << endl;
    cin >> row >> col;
    col += row;
    cout << "Your value is: " << numValue(row,col) << endl;

    return 0;
}

Create a function called howMany that will search a two dimensional array for the number of occurences a particular number is found within the array. Your function should return the number of occurences the number appears withing the array and the coordinates 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 mechanism to return the coordinates that each one is found at. 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.

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

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 demonstrate the functionality of your program. It would be nice if you used some kind of a loop and a simple menu scheme.

---------------------------------------------------------------
Now, my instructor gave me permission to use however many arguments I wanted, and he wanted me to cout and cin only in main. I really would appreciate some insight in this matter. Can anyone tell me what I've done wrong?

Recommended Answers

All 2 Replies

First, completely read and understand the assignment. The 2D array must be a parameter passed to the various functions. Declaring it in global space is not fulfilling the assignment and is, in general, not a good thing to do in programming classes, or real life programming.

The array must be declared inside main( ). Your functions must take that, and other information as parameters. In particular, I recommend you pass the number of rows to the functions, so they may be used with arrays of any number of rows. (They will be constrained to column dimension of 9.)

Generally, your functions are set up to do the jobs specified.

randnum( ) need only be called once in main( ), not inside the loop. The function is looping through the array, filling all elements, so you don't need to call it 90 times! What is the purpose of the one value you're returning from randnum( )? It's role is just to fill the array, not give any results back.

If you're confused about passing arrays to functions, here's a quick sample

int main( )
{
     int data[3][4] = { 1,2,3,4,  5,6,7,8,  9,10, 11, 12 };
     int total;

     total = sum( data, 3 );
    cout << total << endl;
    return 0;
}

int sum( int d[][4], int nrows )
{
    int i, j;
    int result = 0;
    for( i = 0; i < nrows; i++ )  //rows
        for( j = 0; j < 4; j++ )    //columns
            result += d[i][j];

     return result;
}

Please use the code tags around your code in future postings, like:

[code]

your code goes here

[/code]

Thank you so much. This is something I can truly use! I really appreciate your help.

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.