Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // comp128_lab11.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std ;
- #define MAX_HI 5
- #define MAX_WIDE 5
- void fill_array(char c_array[][MAX_HI]) ;
- void print_array(const char c_array[][MAX_HI]) ;
- void search_array(const char c_array[][MAX_HI], char target) ;
- int _tmain(int argc, _TCHAR* argv[])
- {
- char char_array[MAX_WIDE][MAX_HI] ;
- fill_array(char_array) ;
- print_array(char_array) ;
- search_array(char_array, 'd');
- return 0;
- }
- /*1. Reimplement your read_array() to build a 5x5 matrix. using MAX_HI 5 and MAX_WIDE 5 #defines.
- 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.
- 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),
- 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.
- You are using a nested for loop for this.
- 4. Revise above to keep track of the number of matches and print "Not found" after loop if zero.
- 5. Optional: Create a 2D array to keep track of multiple matches (pass to search_array).*/
- void print_array(const char c_array[][MAX_HI])
- {
- for(int i=0 ; i<MAX_WIDE ; i++)
- {
- for (int j=0 ; j<MAX_HI ; j++)
- { cout << c_array[i][j];}
- cout << endl << endl;
- }
- }
- void fill_array(char c_array[][MAX_HI])
- {
- char avar/* = 'a'*/ ;
- for(int i=0; i<MAX_WIDE; i++)
- {
- for(int j=0 ; j<MAX_HI ; j++)
- {
- cout << "Enter a char for: " << i << " " << j << endl ;
- cin >> avar ;
- c_array[i][j] = avar ;
- }
- }
- cout << endl ;
- }
- void search_array(const char c_array[][MAX_HI], char target)
- {
- int match=0 ;
- for(int i=0 ; i<MAX_WIDE ; i++)
- {
- for(int j=0; j<MAX_HI ; j++)
- {
- if(c_array[i][j] == target)
- {
- cout << "Found " << target << " at: " << i << " " << j << endl ;
- match++ ;
- }
- }
- }
- if(match>0)
- {
- cout << "found " << match << " matches. " << endl ;
- }
- else
- cout << "not found. " << endl ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement