Question

In: Computer Science

The programming language has to be C The user is going to provide you with a...

The programming language has to be C

The user is going to provide you with a map of rivers and grassland. Each cell on the map will be either grassland or river. Your job is to decorate this map with forks, 4 way forks, and river bends when a river bends. You will process the user's map and modify the river cells if they depict a river bend.

You will use enums to model the river, its bends, and forks. You will pattern match to replace parts of the river with bends and forks. Out of bounds regions will be considered grasslands for pattern matching simplicity.

You will use enums in this program.

You will print the integers with 1 space padding and rows will be terminated by new lines:

10 10 10
 1  2  3
 4  5  6
 7  8  9
10 10 10

Input and Output

The tiles that we use in the map are:

  • 0 Grassland
  • 1 River
  • 2 NorthWestRiverBend
  • 3 SouthWestRiverBend
  • 4 NorthEastRiverBend
  • 5 SouthEastRiverBend
  • 6 NorthEastSouthFork
  • 7 NorthWestSouthFork
  • 8 WestNorthEastFork
  • 9 WestSouthEastFork
  • 10 FourWayFork

The user will input maps usually of 0 and 1 but they can include other tiles as well. Typically it will 0 and 1.

The user will input a map of

P2
6 7
10
 0  0  0  0  0  0 
 0  1  1  1  1  0
 0  1  0  0  1  0 
 0  1  1  1  1  1
 0  1  0  0  1  0
 0  1  1  1  1  0
 0  0  0  0  1  0

Where 0 is grassland and 1 is river

The program will look for corners and replace them with the RiverBend pieces.

P2
6 7
10
 0  0  0  0  0  0 
 0  5  1  1  3  0 
 0  1  0  0  1  0 
 0  6  1  1 10  1 
 0  1  0  0  1  0 
 0  4  1  1  7  0 
 0  0  0  0  1  0 

The header format is described below. $WIDTH is the number of cells wide and $HEIGHT is the number of cells tall.

P2
$WIDTH $HEIGHT
10

The individual cells are whitespace seperated for input. Ignore whitespace and newlines for the cells.

The output format is strict, each row ends in a newline and there is 1 space padding for all integers cells printed.

Please review the q1a-test?-input.txt files for more examples

Patterns

  • Examples of No change
 0  0  0         0  0  0
 0  0  0    ->   0  0  0
 0  0  0         0  0  0
 
 0  0  0         0  0  0
 0  0  0    ->   0  0  0
 0  0  1         0  0  1
  
 0  0  0         0  0  0
 0  0  0    ->   0  0  0
 1  1  1         1  1  1
 

These examples are in cross form, this means that the corners can be anything (river or grassland). This is only 1 cell being changed, you are expected to apply these patterns to all cells.

  • 2 NorthWestRiverBend
    1               1   
 1  1  0    ->   1  2  0
    0               0   

For example:

 0  1  0         0  1  0
 1  1  0    ->   1  2  0
 0  0  0         0  0  0


  • 3 SouthWestRiverBend
    0               0   
 1  1  0    ->   1  3  0
    1               1

For example:

 1  0  1         1  0  1
 1  1  0    ->   1  3  0
 1  1  1         1  1  1


  • 4 NorthEastRiverBend
    1               1   
 0  1  1    ->   0  4  1
    0               0  
  • 5 SouthEastRiverBend
    0               0  
 0  1  1    ->   0  5  1
    1               1  
  • 6 NorthEastSouthFork
    1               1   
 0  1  1    ->   0  6  1
    1               1  
  • 7 NorthWestSouthFork
    1               1   
 1  1  0    ->   1  7  0
    1               1   
  • 8 WestNorthEastFork
    1               1   
 1  1  1    ->   1  8  1
    0               0   
  • 9 WestSouthEastFork
    0               0  
 1  1  1    ->   1  9  1
    1               1  
  • 10 FourWayFork
    1               1  
 1  1  1    ->   1 10  1
    1               1  

More details

Maximum supported dimension in width or height is 16000

The format you input and output is called plain PGM. PGM is portable grey map format so you can use some image programs to view it.

Invalid input (including unexpected EOF) should be aborted immediately with the message:

Invalid input!

Hints

  • Initialize your memory when you malloc it
  • Remember to check the bounds of the array.
  • You should make/extract a cursor so you can pattern match.
  • You should consider writing unit tests for the patterns
  • You should consider creating new arrays dynamically where need be.
  • Return appropriate values from the functions to avoid segmentation faults.
  • If you find any task repetitive, make it a function.
  • You should read from 1 2D array and write to another.
  • You can visualize your output better if you pipe your program through utfdraw.sh (you need GNU sed)

Solutions

Expert Solution

Screenshot of the code

Below is the c code for the same. The code reads input from the file input.txt and writes output to console as well as file output.txt . Enum has been used to represent grassland,river,river-bends & forks.

#include<stdio.h>

#include<stdlib.h>

#define MAX_DIMENSION 16000// defining maximum dimension

//enum for tiles

enum tiles{Grassland,River,NorthWestRiverBend,SouthWestRiverBend,

NorthEastRiverBend,SouthEastRiverBend,

NorthEastSouthFork,NorthWestSouthFork,

WestNorthEastFork,WestSouthEastFork,FourWayFork};

//function for decorating map

void decorate_map(int **map,int width, int height){

//checking patterns in map

for(int i=0;i<height;i++){

for(int j=0;j<width;j++){

if(map[i][j]==0)

continue;

if(i>0&&j>0&&i<height-1&&j<height-1&&map[i-1][j]==1&&map[i+1][j]==1&&map[i][j-1]==1&&map[i][j+1]==1)

map[i][j]=FourWayFork;

else if(j>0&&i<height-1&&j<height-1&&map[i+1][j]==1&&map[i][j-1]==1&&map[i][j+1]==1)

map[i][j]=WestSouthEastFork;

else if(i>0&&j>0&&j<height-1&&map[i-1][j]==1&&map[i][j-1]==1&&map[i][j+1]==1)

map[i][j]=WestNorthEastFork;

else if(i>0&&j>0&&i<height-1&&map[i-1][j]==1&&map[i+1][j]==1&&map[i][j-1]==1)

map[i][j]=NorthWestSouthFork;

else if(i>0&&i<height-1&&j<height-1&&map[i-1][j]==1&&map[i+1][j]==1&&map[i][j+1]==1)

map[i][j]=NorthEastSouthFork;

else if(i<height-1&&j<height-1&&map[i+1][j]==1&&map[i][j+1]==1)

map[i][j]=SouthEastRiverBend;

else if(i>0&&j<height-1&&map[i-1][j]==1&&map[i][j+1]==1)

map[i][j]=NorthEastRiverBend;

else if(j>0&&i<height-1&&map[i+1][j]==1&&map[i][j-1]==1)

map[i][j]=SouthWestRiverBend;

else if(i>0&&j>0&&map[i-1][j]==1&&map[i][j-1]==1)

map[i][j]=NorthWestRiverBend;

}

}

}

int main(){

FILE *fp;

fp=fopen("input.txt","r");//input file

FILE *fp2;

fp2=fopen("output.txt","w");//output file

char P2[3];

if(!(fgets(P2,3,fp)))//inputting first line

printf("Invalid input!");

printf("%s\n",P2);

fprintf(fp2,"%s\n",P2);

int width,height;

if(!(fscanf(fp,"%d",&width)&&fscanf(fp,"%d",&height)))//inputting width and height

printf("Invalid input!");

printf("%d %d\n",width,height);

fprintf(fp2,"%d %d\n",width,height);

int _10_;

if(!(fscanf(fp,"%d",&_10_)))

printf("Invalid input!");

printf("%d\n",_10_);

fprintf(fp2,"%d\n",_10_);

//declaring map of max dimensions

int ** map;

map=(int **)malloc(sizeof(int*)*MAX_DIMENSION);

for(int i=0;i<MAX_DIMENSION;i++)

map[i]=(int *)malloc(sizeof(int)*MAX_DIMENSION);

//inputting map

for(int i=0;i<height;i++)

for(int j=0;j<width;j++)

if(!(fscanf(fp,"%d",&map[i][j])))

printf("Invalid input!");

//calling function to decorate map

decorate_map(map,width,height);

//outputting map

for(int i=0;i<height;i++){

for(int j=0;j<width;j++){

printf("%d ",map[i][j]);

fprintf(fp2,"%d ",map[i][j]);

}

printf("\n");

fprintf(fp2,"\n");

}

//freeing memory

for(int i=0;i<MAX_DIMENSION;i++)

free(map[i]);

free(map);

}


Related Solutions

C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
programming language is c#. Create a method that prompts a user of your console application to...
programming language is c#. Create a method that prompts a user of your console application to input the information for a student: static void GetStudentInfo() { Console.WriteLine("Enter the student's first name: "); string firstName = Console.ReadLine(); Console.WriteLine("Enter the student's last name"); string lastName = Console.ReadLine(); // Code to finish getting the rest of the student data ..... } static void PrintStudentDetails(string first, string last, string birthday) { Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday); } 1. Using the...
Using (C programming language) Create a health monitoring program, that will ask user for their name,...
Using (C programming language) Create a health monitoring program, that will ask user for their name, age, gender, weight, height and other health related questions like blood pressure and etc. Based on the provided information, program will tell user BMI, blood pressure numbers if they fall in healthy range or not and etc. Suggestions can be made as what should be calorie intake per day and the amount of exercise based on user input data. User should be able to...
Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to...
Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to input 2 numbers, a starting number x and ending number y Implements a while loop that counts from x (start) to y(end). Inside the loop, print to the screen the current number Print rather the current number is even or odd If the number is even , multiply by the number by 3 and print the results to the screen. If the number is...
Using c# programming language Write a program that mimics a lottery game. Have the user enter...
Using c# programming language Write a program that mimics a lottery game. Have the user enter 3 distinct numbers between 1 and 10 and match them with 3 distinct, randomly generated numbers between 1 and 10. If all the numbers match, then the user will earn $10, if 2 matches are recorded then the user will win $3, else the user will lose $5. Keep tab of the user earnings for, let say 5 rounds. The user will start with...
Using C# programming language, Write a program that sort three numbers entered by the user using...
Using C# programming language, Write a program that sort three numbers entered by the user using only if and nested if statements. If for instance the user entered 5, 2, and 7; the program should display 2,5,7.
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
You are using ONLY Programming Language C for this: In this program you will calculate the...
You are using ONLY Programming Language C for this: In this program you will calculate the average of x students’ grades (grades will be stored in an array). Here are some guidelines to follow to help you out: 1. In your program, be sure to ask the user for the number of students that are in the class. The number will help in declaring your array. 2. Use the function to scan the grades of the array. To say another...
The Programming Language is C++ Objective: The purpose of this project is to expose you to:...
The Programming Language is C++ Objective: The purpose of this project is to expose you to: One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions. Problem Specification: Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three quizzes for each student during the term. Each student is identified by a four-digit student...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT