Advertisement
Guest User

Untitled

a guest
May 26th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. //
  2. // Program for homework 10 - Exercise 2 - CCOM3033
  3. //
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10. const int ROWS = 20, COLS = 20;
  11.  
  12. //
  13. // Function printArray(A, rows):
  14. // Given A, a two-dim array of characters, prints it
  15. //
  16.  
  17. void printArray(char A[][COLS], int rows) {
  18. for (int r = 0; r < rows; r++) {
  19. for (int c = 0; c < COLS; c++)
  20. cout << A[r][c] << " ";
  21. cout << endl;
  22. }
  23. cout << endl;
  24. }
  25.  
  26. //
  27. // Function readArrayFromFile(fileName, A, rows):
  28. // Given the name of a file, reads its contents to the provided
  29. // 2-dim array (A). The file must contain 20 strings of 20 letters
  30. // each.
  31. // Returns true if the file was read correctly, otherwise false
  32. //
  33.  
  34. bool readArrayFromFile(string fileName, char A[][COLS], int rows) {
  35. ifstream inFile;
  36. inFile.open(fileName.c_str());
  37.  
  38. if (inFile.fail())return false;
  39.  
  40. // reads the contents of the file onto a two dimensional array
  41. char c;
  42. int ctr = 0;
  43. while(inFile >> c) {
  44. A[ctr/ROWS][ctr%COLS] = c;
  45. ctr++;
  46. }
  47. return true;
  48.  
  49. }
  50.  
  51. //
  52. // This is the function that you should implement.
  53. //
  54. //
  55. // Function uniqueLandUse(A,rows):
  56. // Given A, a two-dim array of chars returns how many different
  57. // chars are in the array.
  58. //
  59.  
  60. int uniqueLandUse(char A[][COLS], int rows) {
  61.  
  62. // your code here!!!
  63.  
  64. return 0;
  65. }
  66.  
  67. //
  68. // The main program simply reads the file in to a 2-dim array and
  69. // then invokes the uniqueLandUse function to compute the number of
  70. // unique land uses.
  71. //
  72.  
  73. int main() {
  74. char A[ROWS][COLS];
  75. string fileName = "input.txt";
  76.  
  77. if ( readArrayFromFile(fileName, A , ROWS) ) {
  78. printArray(A,ROWS);
  79. cout << "The number of unique land uses is: "
  80. << uniqueLandUse(A, ROWS) << endl;
  81. }
  82. else
  83. cout << "Problem reading the file: " << fileName << endl;
  84.  
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement