Advertisement
Guest User

Da code

a guest
Oct 25th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void FillArray(int a[][9], int size);
  6. void PrintArray(int a[][9], int size);
  7. int FindNumber(int a[][9], int search);
  8.  
  9. int main()
  10. {
  11. int num;
  12. int theOdds[10][9];
  13.  
  14. cout << "Input an odd number to match. " << endl;
  15. cin >> num;
  16.  
  17. FillArray(theOdds, 9);
  18. FindNumber(theOdds, num);
  19.  
  20.  
  21. PrintArray(theOdds, 9);
  22.  
  23.  
  24. system("pause");
  25.  
  26. }
  27. void FillArray(int a[][9], int size)
  28. {
  29. for (int row = 0; row < 10; row++)
  30. {
  31. for (int col = 0; col < 9; col++)
  32. {
  33. // a[row][col] = rand() % 99 + 1;
  34. a[row][col] = (2 * rand() + 1) % 100;
  35. }
  36. }
  37.  
  38. }
  39. void PrintArray(int a[][9], int size)
  40. {
  41. for (int row = 0; row < 10; row++)
  42. {
  43. for (int col = 0; col < 9; col++)
  44. {
  45. cout << a[row][col] << "\t";
  46. }
  47. cout << endl;
  48. }
  49.  
  50. }
  51. int FindNumber(int a[][9], int search)
  52. {
  53. int count = 0;
  54.  
  55.  
  56. for (int row = 0; row < 10; row++)
  57. {
  58. for (int col = 0; col < 9; col++)
  59. {
  60. if (a[row][col] == search)
  61. count++;
  62. if (a[row][col] == search)
  63. {
  64.  
  65. }
  66. }
  67. }
  68. return count;
  69.  
  70.  
  71.  
  72. }
  73. int ReadArrayAt(int a[][9], int )
  74. {
  75.  
  76.  
  77.  
  78. }
  79.  
  80.  
  81. CSIS 113A Lab 16 2D Arrays
  82.  
  83. Create a program that declares a 2 dimensional array and fills it with random numbers. You will need to create functions that: fill the 2D array, find a number and count the number of instances of the number in the 2d array and load the coordinates of the matches in an array or arrays. You will need functions to print the array 2D array and the list of coordinates of matches.
  84. In the main function create an array called theOdds. The array should hold ten rows with nine columns.
  85. Create a void function call FillArray that will fill a 2D array with variable rows and nine columns with random odd numbers from 1 to 99. Don’t assume the function will only be used to fill 10x9 tables.
  86. Create a void function call PrintArray that will print a 2D array with any number of rows and nine columns. Don’t assume the function will only be used to print 10x9 tables.
  87. Create a value producing function called FindNumber that will search a two dimensional array of various rows and nine columns for the number of times a particular number is found within the array. The function should return the number of times the number appears within the array *and* the coordinates, row and column, where each match is located. Don’t assume the function will only search 10x9 tables.
  88. Your FindNumber function should take as arguments the number to search for, and the two dimensional array to search. You are going also to need something to fill with the the row and column coordinates of found matches. Use either one or two arrays to hold the coordinates, your choice.
  89. Next, create another function called ReadArrayAt that will have as arguments a set of row and column coordinates, and a 2D array of variable rows and nine columns to read. This function should just use the row and column coordinates arguments to read the 2D array passed in at the coordinates and return the value at those row and column coordinates. As the array to be read should not be changed by this function, the array argument should be a constant.
  90. Finally write some kind of driver code in main to demonstrate the functionality of your program and functions. Program flow should go as follows:
  91.  
  92. Declare theOdds array
  93. Call FillArray on theOdds
  94. Call FindNumber on theOdds with an odd number between 1 and 99; get the number of matches, and load the coordinates of *every* match into either one array or two arrays
  95. Use the return value of FindNumber, number of matches, in a for loop with the array(s) of row and column coordinates to callReadArrayAt on the coordinates of every match to verify your coordinates are correct. Output the row and column information and value returned from ReadArrayAt to verify you coordinates are correct. You may choose to put this code in a function calledPrintCoordinates if you wish.
  96. Call the PrintArray function with theOdds to verify you found all the matches in theRandoms.
  97.  
  98. Using any of the following will drop your grade for this assignment by 70%
  99.  
  100. global variables
  101. cout in any function other than main, PrintArray and PrintCoordinates
  102. cin in any function other than main
  103. goto, break (other than in a switch), or continue statements
  104.  
  105.  
  106. Sample input/output when 97 is entered; obviously your numbers will vary:
  107.  
  108. Input an odd number 1-99 to match: 97
  109.  
  110. 97 is at row 2 col 4
  111.  
  112. 97 is at row 8 col 4
  113.  
  114.  
  115.  
  116. 69 39 17 35 89 41 71 7 69
  117.  
  118. 3 91 63 97 85 99 11 35 43
  119.  
  120. 91 27 37 23 35 91 17 61 65
  121.  
  122. 1 29 51 31 81 77 49 33 15
  123.  
  124. 51 7 47 29 19 3 99 93 93
  125.  
  126. 15 43 43 35 83 95 1 77 75
  127.  
  128. 39 87 71 87 17 33 49 35 77
  129.  
  130. 67 79 61 97 63 13 57 37 41
  131.  
  132. 15 57 57 27 47 11 19 47 11
  133.  
  134. 21 1 85 49 23 93 1 63 79
  135.  
  136. Press any key to continue . . .
  137.  
  138.  
  139. Be aware you may not always get a match so you program should still work and say “No matches found for “ the input number. I cheated and entered a 101 so you can see this case:
  140.  
  141. Input an odd number 1-99 to match: 101
  142.  
  143.  
  144.  
  145. No matches found for 101!
  146.  
  147.  
  148.  
  149.  
  150. 69 39 17 35 89 41 71 7 69
  151.  
  152. 3 91 63 97 85 99 11 35 43
  153.  
  154. 91 27 37 23 35 91 17 61 65
  155.  
  156. 1 29 51 31 81 77 49 33 15
  157.  
  158. 51 7 47 29 19 3 99 93 93
  159.  
  160. 15 43 43 35 83 95 1 77 75
  161.  
  162. 39 87 71 87 17 33 49 35 77
  163.  
  164. 67 79 61 97 63 13 57 37 41
  165.  
  166. 15 57 57 27 47 11 19 47 11
  167.  
  168. 21 1 85 49 23 93 1 63 79
  169.  
  170. Press any key to continue . . .
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement