Question

In: Computer Science

(Written in C)The input below shows a grid dimension (first line), the following 2 are the...

(Written in C)The input below shows a grid dimension (first line), the following 2 are the positions of the start cell and end cell, and the rest is the positions of cells blocked. Keep in mind that the input is an txt file to be read through stdin (you can not type input multiple times). How do I read the input using scanf . Also, it is not allow to store input into array (because we can not assume the size of the grid with limited number).

10x10
[0,0]
[9,9]
9 blocks

The expected output is like this:

grid has 10 rows and 10 columns.
grid has 9 block(s).
initial cell in the grid is [0,0].
goal cell in the grid is [9,9].

Help !! (thumbs up)

Solutions

Expert Solution

I have created a program .

In the program below , replace the path to the file with the file you have .

(Notice that the 2nd and 4th lines are swapped . But the result is same , i hope .)

If it helped , please consider liking .

#include<stdlib.h>
#include<stdio.h>
int main()
{
char c,filename[]="/home/meliodas/Documents/CPP/Hackerrank/file.txt"; // replace the file path here
char rows[2] ,columns[2],blocks[2],initial[10],goal[10];
FILE *fptr;
fptr=fopen(filename,"r"); // opening file
if (fptr==NULL)
{
printf("Cannot open ");
exit(0);
}
// Read contents from file
c = fgetc(fptr);
printf("grid has ");
while (c!='x')
{
printf("%c",c);
c = fgetc(fptr);
}
printf(" rows");


c = fgetc(fptr);
printf("\ngrid has ");
while (c!='\n')
{
printf("%c",c);
c = fgetc(fptr);
}
printf(" columns");

c = fgetc(fptr);


printf("\ninitial cell in the grid is ");
while (c!='\n')
{
printf("%c",c);
c = fgetc(fptr);
}

c = fgetc(fptr);


printf("\ngoal cell in the grid is ");
while (c!='\n')
{
printf("%c",c);
c = fgetc(fptr);
}
c = fgetc(fptr);


printf("\ngrid has " );
while (c!=' ')
{
printf("%c",c);
c = fgetc(fptr);
}
printf(" block(s)");

fclose(fptr);
return 0;
}


Related Solutions

In 2 to 3 paragraphs describe the C program written below (What the program is supposed...
In 2 to 3 paragraphs describe the C program written below (What the program is supposed to do). State the requirements for the program (How does the program meet the requirements) and discuss the logical process your program uses to meet those requirements (The process steps to meet the requirements). #include "stdio.h" int main(void) { //initialize array int arr[100];   //initialize variables   int i=0, j=0, n=0;    //infinite loop which will stop when user enters -1   while(n != -1) {   ...
Design and implement a C++ program read in a whole line of characters as the input...
Design and implement a C++ program read in a whole line of characters as the input string; count and display how many times how frequently (among the letters) each (case insensitive) letter appears in the above mentioned input string; Sample program execution: An example of executing such a program is shown below. Note that the user input is in italic font. Please enter a line of characters: This is a really long line of characters! There are 41 characters in...
Write C++ programs to perform the following tasks. In the program descriptions below, example input and...
Write C++ programs to perform the following tasks. In the program descriptions below, example input and output is provided. NOTE: You don’t need arrays to solve any of these problems. You should NOT use arrays to solve any of these problems. • stat.cpp: Let the user input a one or more integers, space separated, on a single line (as seen below), then work out and display the sum, average, sum of squares and population variance of the numbers. Remember, you...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Java. Given an input file with each line representing a record of data and the first...
Java. Given an input file with each line representing a record of data and the first token (word) being the key that the file is sorted on, we want to load it and output the line number and record for any duplicate keys we encounter. Remember we are assuming the file is sorted by the key and we want to output to the screen the records (and line numbers) with duplicate keys. We are given a text file and have...
using C language Create a bitwise calculator that ask user for input two input, first int,...
using C language Create a bitwise calculator that ask user for input two input, first int, bitwise operation, second int (i.e 5 & 9) only those bitwise operation are allowed: & ~ ^ | << >>. If user uses wrong operators stop program and ask again. Convert the first int and second int into 8 BITS binary (00000000) and use bitwise operator given by user to either AND, OR, XOR, etc (ie 1001 & 1111)
Write a function in C++ that reads the line separates it with comma. Input: hello how...
Write a function in C++ that reads the line separates it with comma. Input: hello how are you. hello world hello_world I am there for you! Output: hello, how, are and you. hello and world hello_world I, am, there, for and you! just add a comma and (and) before the last word. CODE IN C++ ONLY.
c++ problem: Use an STL stack to reverse a line of input characters that are read...
c++ problem: Use an STL stack to reverse a line of input characters that are read into a string. Your stack should contain the characters of the user's string. Use getline() for input – it needs to be part of your C++ tool inventory. A note on getline: Suppose I am doing the following - This program reverses a string using the STL stack Enter your string of less than 80 characters followed by an ENTER a string input Enter...
Write a program that reads two strings from an input file (The first line is X,...
Write a program that reads two strings from an input file (The first line is X, the second line is Y), compute the longest common subsequence length AND the resulting string. You will need to write 2 methods 1) return LCS length in iterative function // return the length of LCS. L is the 2D matrix, X, Y are the input strings, m=|X|, n=|Y| int lcs_it(int **C, string X, string Y, int m, int n ) 2) return LCS resulting...
The Table below shows the production capacity per unit of input for two countries. E.g. if...
The Table below shows the production capacity per unit of input for two countries. E.g. if Country B spends 1 input on magnets, it will produce 50 magnets. country B country C magnets 50 5 pies 20 10             (5A) Imagine that both countries are initially not trading and have 10 inputs each. They split their inputs evenly in the production of both goods. Describe what the total gains from trade potentially are if the countries change their production...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT