Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. // comp128_lab11.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. using namespace std ;
  7. #define MAX_HI 5
  8. #define MAX_WIDE 5
  9. void fill_array(char c_array[][MAX_HI]) ;
  10. void print_array(const char c_array[][MAX_HI]) ;
  11. void search_array(const char c_array[][MAX_HI], char target) ;
  12.  
  13. int _tmain(int argc, _TCHAR* argv[])
  14. {
  15. char char_array[MAX_WIDE][MAX_HI] ;
  16. fill_array(char_array) ;
  17. print_array(char_array) ;
  18. search_array(char_array, 'd');
  19.  
  20.  
  21. return 0;
  22. }
  23.  
  24. /*1. Reimplement your read_array() to build a 5x5 matrix. using MAX_HI 5 and MAX_WIDE 5 #defines.
  25. You do not need references there this time, just fill in the 25 characters 5 at a time (row at a time) using a nested loop.
  26.  
  27. 2. Write a print_array with MAX_HI and MAX_WIDE and pass array as const. Refer to text to create a 5 by 5 array of chars. You might practice by just getting a nested for loop to work (like selection sort),
  28.  
  29. 3. Write search_array to do a search printing row and column of All MATCHES ( Just do a cout in the loop if a match). Do not stop on first match. Do some serious testing for matches at 0,0 and 5,5 and middle.
  30. You are using a nested for loop for this.
  31.  
  32. 4. Revise above to keep track of the number of matches and print "Not found" after loop if zero.
  33.  
  34. 5. Optional: Create a 2D array to keep track of multiple matches (pass to search_array).*/
  35.  
  36. void print_array(const char c_array[][MAX_HI])
  37. {
  38. for(int i=0 ; i<MAX_WIDE ; i++)
  39. {
  40. for (int j=0 ; j<MAX_HI ; j++)
  41. { cout << c_array[i][j];}
  42. cout << endl << endl;
  43. }
  44. }
  45.  
  46. void fill_array(char c_array[][MAX_HI])
  47. {
  48. char avar/* = 'a'*/ ;
  49. for(int i=0; i<MAX_WIDE; i++)
  50. {
  51. for(int j=0 ; j<MAX_HI ; j++)
  52. {
  53. cout << "Enter a char for: " << i << " " << j << endl ;
  54. cin >> avar ;
  55. c_array[i][j] = avar ;
  56. }
  57. }
  58. cout << endl ;
  59. }
  60.  
  61. void search_array(const char c_array[][MAX_HI], char target)
  62. {
  63. int match=0 ;
  64. for(int i=0 ; i<MAX_WIDE ; i++)
  65. {
  66. for(int j=0; j<MAX_HI ; j++)
  67. {
  68. if(c_array[i][j] == target)
  69. {
  70. cout << "Found " << target << " at: " << i << " " << j << endl ;
  71. match++ ;
  72. }
  73. }
  74. }
  75. if(match>0)
  76. {
  77. cout << "found " << match << " matches. " << endl ;
  78. }
  79. else
  80. cout << "not found. " << endl ;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement