Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Program for homework 10 - Exercise 2 - CCOM3033
- //
- #include <iostream>
- #include <fstream>
- using namespace std;
- const int ROWS = 20, COLS = 20;
- //
- // Function printArray(A, rows):
- // Given A, a two-dim array of characters, prints it
- //
- void printArray(char A[][COLS], int rows) {
- for (int r = 0; r < rows; r++) {
- for (int c = 0; c < COLS; c++)
- cout << A[r][c] << " ";
- cout << endl;
- }
- cout << endl;
- }
- //
- // Function readArrayFromFile(fileName, A, rows):
- // Given the name of a file, reads its contents to the provided
- // 2-dim array (A). The file must contain 20 strings of 20 letters
- // each.
- // Returns true if the file was read correctly, otherwise false
- //
- bool readArrayFromFile(string fileName, char A[][COLS], int rows) {
- ifstream inFile;
- inFile.open(fileName.c_str());
- if (inFile.fail())return false;
- // reads the contents of the file onto a two dimensional array
- char c;
- int ctr = 0;
- while(inFile >> c) {
- A[ctr/ROWS][ctr%COLS] = c;
- ctr++;
- }
- return true;
- }
- //
- // This is the function that you should implement.
- //
- //
- // Function uniqueLandUse(A,rows):
- // Given A, a two-dim array of chars returns how many different
- // chars are in the array.
- //
- int uniqueLandUse(char A[][COLS], int rows) {
- // your code here!!!
- return 0;
- }
- //
- // The main program simply reads the file in to a 2-dim array and
- // then invokes the uniqueLandUse function to compute the number of
- // unique land uses.
- //
- int main() {
- char A[ROWS][COLS];
- string fileName = "input.txt";
- if ( readArrayFromFile(fileName, A , ROWS) ) {
- printArray(A,ROWS);
- cout << "The number of unique land uses is: "
- << uniqueLandUse(A, ROWS) << endl;
- }
- else
- cout << "Problem reading the file: " << fileName << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement