943,549 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 544
  • C++ RSS
Nov 12th, 2008
0

Need insight in assignment

Expand Post »
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?
Last edited by kitty7; Nov 12th, 2008 at 2:15 am. Reason: silly typos
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kitty7 is offline Offline
11 posts
since Nov 2008
Nov 12th, 2008
0

Re: Need insight in assignment

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
C++ Syntax (Toggle Plain Text)
  1. int main( )
  2. {
  3. int data[3][4] = { 1,2,3,4, 5,6,7,8, 9,10, 11, 12 };
  4. int total;
  5.  
  6. total = sum( data, 3 );
  7. cout << total << endl;
  8. return 0;
  9. }
  10.  
  11. int sum( int d[][4], int nrows )
  12. {
  13. int i, j;
  14. int result = 0;
  15. for( i = 0; i < nrows; i++ ) //rows
  16. for( j = 0; j < 4; j++ ) //columns
  17. result += d[i][j];
  18.  
  19. return result;
  20. }

Please use the code tags around your code in future postings, like:
[code]
your code goes here
[/code]
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Nov 12th, 2008
0

Re: Need insight in assignment

Thank you so much. This is something I can truly use! I really appreciate your help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kitty7 is offline Offline
11 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Cast double to const char*
Next Thread in C++ Forum Timeline: function to check leap year





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC