Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #define _GNU_SOURCE
- int main()
- {
- int treeCount = 0;
- int buildingCount = 0;
- int pathCount =0;
- int total =0;
- char whichBuilding;
- float pathPercent = 0;
- int i, j;
- char myArray [100][100];
- // open the file to read
- FILE*access;
- access = fopen("buildingMap.txt","r");
- if (access != NULL){
- //initialise array
- //initialise to 0
- for(i=0; i<100; i++){
- for(j=0; j<100; j++){
- myArray[i][j] = 0; //1 gets added to j and loops then when j = 99, 1 gets added to i
- }
- }
- //char lineOfText [100]; //does it work with 99
- /*while (fgets(lineOfText,100,access) != NULL)
- char mapPoint [1];//variable mapPoint to allow single character to be read from the line
- myArray [i][j] = int fgetc(FILE*lineOfText); //gets a character from a line, stored in myArray
- i++; */
- for (i=0; i<100; i++){
- //fgets(lineOfText, 100, access); //reads the whole line of file one line at a time
- for(j=0; j<100; j++){
- total++;
- myArray[i][j] = fgetc(access); //reads each character of lineOfText and puts it into the array and then loops
- if(myArray[i][j] == '#'){ //if the read finds "#" it'll do something
- treeCount++;
- }
- 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'){
- buildingCount++; //use single quotes for a single letter
- }
- else if(myArray[i][j] =='x'){
- pathCount++;
- }
- pathPercent = (float)pathCount/total*100;
- }
- }
- }
- else{
- printf("file not found\n");
- }
- //reads a file 100x100
- printf("the number of trees is %d\n" , treeCount);
- printf("the number of buildings is %d\n", buildingCount);
- printf("the percentage of path on the map is %.2f%%\n", pathPercent);
- printf("please enter the building where you would like to find\n");
- scanf("%c",&whichBuilding);
- for (i=0; i<100; i++){
- for(j=0; j<100; j++){
- if (myArray[i][j] == whichBuilding){
- printf("found at (%d ,%d)\n",j,i);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement