| | |
Need insight in assignment
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 11
Reputation:
Solved Threads: 0
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?
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
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
Please use the code tags around your code in future postings, like:
[code]
your code goes here
[/code]
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)
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]
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- Assignment Issues (C++)
- visual c++ computer help (C++)
- simple 'if statement' issue (C#)
- translate c into java (Java)
- date (C++)
- Floating point multiplication, precision issues (C++)
- program to simulate a soft drink machine. PLease help!!! (C++)
- Program, help (Java)
- Colour picker, MouseDown(Event, int x, int y) Depreciation Help! (Java)
- Pls Help JAVA Program Headaches Not Giving me the right output for user input (Java)
Other Threads in the C++ Forum
- Previous Thread: Cast double to const char*
- Next Thread: function to check leap year
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






