Search Array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 22
Reputation: FtKShadow is an unknown quantity at this point 
Solved Threads: 0
FtKShadow FtKShadow is offline Offline
Newbie Poster

Search Array

 
0
  #1
Nov 11th, 2008
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.
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <ctime>
  5. using namespace std;
  6.  
  7. void fillarray(int a[][9], int row);
  8. void show(int a[][9], int row);
  9. int howMany(int a[][9], int num);
  10.  
  11.  
  12. int main()
  13. {
  14. int array[10][9];
  15. int num, occ;
  16.  
  17. fillarray(array,10);
  18. show(array,10);
  19.  
  20. cout << "Enter the number to search for: ";
  21. cin >> num;
  22.  
  23. occ = howMany(array, num);
  24. cout << "Number of occurrences; " << occ << endl;
  25.  
  26. return 0;
  27.  
  28.  
  29. if ( num > 0 )
  30. {
  31. cout << "Number of occurrences: " << occ << endl;
  32. }
  33. else {
  34. cout << "Not found " << endl;
  35. }
  36.  
  37. }
  38.  
  39. void fillarray(int a[][9], int row)
  40. {
  41. srand(time(0));
  42. for(int row = 0; row < 10; row++)
  43. {
  44. for(int col = 0; col < 9; col++)
  45. {
  46. a[row][col] = (rand() % 10) + 1;
  47. }
  48. }
  49. }
  50.  
  51.  
  52. void show(int a[][9], int row)
  53. {
  54. for(int row = 0; row < 10; row++)
  55. {
  56. for(int col = 0; col < 9; col++)
  57. {
  58. cout << a[row][col] << "\t";
  59. }
  60. }
  61. }
  62.  
  63.  
  64. int howMany(int a[][9], int num)
  65. {
  66. int count = 0;
  67.  
  68. for(int r = 0; r < 10; r++)
  69. {
  70. for(int c = 0; c < 9; c++)
  71. {
  72. if(a[r][c] == num)
  73. {
  74. a[9][9] = c;
  75. count++;
  76.  
  77.  
  78. cout << "row: " << r << "\t"<< " col: " << c << " contains the number " << num << endl;
  79.  
  80. }
  81. }
  82. }
  83. return count;
  84. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,673
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Search Array

 
0
  #2
Nov 12th, 2008
The error is caused by your writing a value past the end of the array's allocation. Line 74 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
  1. void fillarray(int a[][9], int row)
  2. {
  3. srand((unsigned)time(0));
  4. int r;
  5. int c;
  6. for( r = 0; r < row; r++)
  7. {
  8. for( c = 0; c < 9; c++)
  9. {
  10. a[r][c] = (rand() % 10) + 1;
  11. }
  12. }
  13. }

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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC