Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #define _GNU_SOURCE
  5. int main()
  6. {
  7. int treeCount = 0;
  8. int buildingCount = 0;
  9. int pathCount =0;
  10. int total =0;
  11. char whichBuilding;
  12. float pathPercent = 0;
  13. int i, j;
  14. char myArray [100][100];
  15. // open the file to read
  16. FILE*access;
  17. access = fopen("buildingMap.txt","r");
  18.  
  19. if (access != NULL){
  20. //initialise array
  21.  
  22. //initialise to 0
  23.  
  24. for(i=0; i<100; i++){
  25. for(j=0; j<100; j++){
  26. myArray[i][j] = 0; //1 gets added to j and loops then when j = 99, 1 gets added to i
  27. }
  28. }
  29. //char lineOfText [100]; //does it work with 99
  30.  
  31. /*while (fgets(lineOfText,100,access) != NULL)
  32. char mapPoint [1];//variable mapPoint to allow single character to be read from the line
  33. myArray [i][j] = int fgetc(FILE*lineOfText); //gets a character from a line, stored in myArray
  34. i++; */
  35. for (i=0; i<100; i++){
  36. //fgets(lineOfText, 100, access); //reads the whole line of file one line at a time
  37. for(j=0; j<100; j++){
  38. total++;
  39. myArray[i][j] = fgetc(access); //reads each character of lineOfText and puts it into the array and then loops
  40. if(myArray[i][j] == '#'){ //if the read finds "#" it'll do something
  41. treeCount++;
  42.  
  43. }
  44. else if(myArray[i][j] =='I' || myArray[i][j] =='L' || myArray[i][j] =='E' || myArray[i][j] =='F' || myArray[i][j] =='C' || myArray[i][j] =='P' || myArray[i][j] =='M' || myArray[i][j] =='B' || myArray[i][j] =='H' || myArray[i][j] == 's'|| myArray[i][j] == 'h' || myArray[i][j] =='o'){
  45.  
  46. buildingCount++; //use single quotes for a single letter
  47. }
  48. else if(myArray[i][j] =='x'){
  49. pathCount++;
  50.  
  51. }
  52. pathPercent = (float)pathCount/total*100;
  53.  
  54.  
  55. }
  56.  
  57.  
  58. }
  59. }
  60. else{
  61. printf("file not found\n");
  62. }
  63. //reads a file 100x100
  64. printf("the number of trees is %d\n" , treeCount);
  65. printf("the number of buildings is %d\n", buildingCount);
  66. printf("the percentage of path on the map is %.2f%%\n", pathPercent);
  67. printf("please enter the building where you would like to find\n");
  68. scanf("%c",&whichBuilding);
  69. for (i=0; i<100; i++){
  70. for(j=0; j<100; j++){
  71. if (myArray[i][j] == whichBuilding){
  72. printf("found at (%d ,%d)\n",j,i);
  73. }
  74.  
  75.  
  76. }
  77.  
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement