| | |
Search Array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 22
Reputation:
Solved Threads: 0
I cant get this code to work it searches and finds the answer, but it gets an error once you search it. Heres the assignment.
•
•
•
•
a.) 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.
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include <cstdlib> #include <ctime> using namespace std; void fillarray(int a[][9], int row); void show(int a[][9], int row); int howMany(int a[][9], int num); int main() { int array[10][9]; int num, occ; fillarray(array,10); show(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 fillarray(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 show(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[9][9] = c; count++; cout << "row: " << r << "\t"<< " col: " << c << " contains the number " << num << endl; } } } return count; }
The error is caused by your writing a value past the end of the array's allocation. Line 74
Also, in functions fillarray( ) and show( ) you pass the number of rows as a parameter, then declare a variable of the same name and use hard coded limit on number of rows. Use the parameter, it makes your function more capable, it could handle an array of any number of rows, like
Read the compiler's warnings - it should have told you about the unused function parameters. Also, looks at the code that occurs after the return statement in main( ) - it never, ever will be executed.
a[9][9] = c; is the culprit. Remember, that for an array sized 10 x 9, the maximum index (the lower right corner) is [9][8]. What is the purpose of this line?Also, in functions fillarray( ) and show( ) you pass the number of rows as a parameter, then declare a variable of the same name and use hard coded limit on number of rows. Use the parameter, it makes your function more capable, it could handle an array of any number of rows, like
C++ Syntax (Toggle Plain Text)
void fillarray(int a[][9], int row) { srand((unsigned)time(0)); int r; int c; for( r = 0; r < row; r++) { for( c = 0; c < 9; c++) { a[r][c] = (rand() % 10) + 1; } } }
Read the compiler's warnings - it should have told you about the unused function parameters. Also, looks at the code that occurs after the return statement in main( ) - it never, ever will be executed.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
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
- Array (C++)
- C++ 6. Search Benchmarks. (C++)
- Binary Search of an array of three strings (C)
- Sorting a 2d array of strings in C++?? (C++)
- Problem with string search of an array (C++)
- How do I create a program using an Array ? (C++)
Other Threads in the C++ Forum
- Previous Thread: Windows Popup menu position problem
- Next Thread: Graph help please
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






