Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Beginner C Programming. Need assistance filling a 4x4 2 Dimensional array using

ID: 3587027 • Letter: B

Question

Beginner C Programming. Need assistance filling a 4x4 2 Dimensional array using getchar(). Need to accept input from "stdin" a file of data. Cannot open a file directly, but needs to read from standard input. The data within the file consists of a string of 16 characters, followed by a newline character. Everything needs to be read with "getchar", and after the data has been stored in the array, print the 16 characters with "putchar".

For an example, the first file has "SNSSNSNSSNNSSNNS” as the input data.

Am I on the right track?

#include<stdio.h>

char grid[4][4];

int row = 0, col = 0;

int x, y;

char c;

void main() {

for(x = 0; x < 17; x++) {

while ((c = getchar()) != ' ') {

if (!isspace(c)){

grid[row][col++] = c; }

}

row++

}

for(x = 0; x < 17; x++)

{

putchar( grid[x] );

}

   return 0;
}

Explanation / Answer

// Please follow the below approach for this problem.
#include<stdio.h>
char grid[4][4];
int row = 0, col = 0;
int x, y;
char c;
  
void main() {
  
// Read the first Character
c = getchar();

// Validate the Character read by getchar() function ,terminates if user hit enter
while(c != ' ' ){
// Create Row entries
for(row =0 ;row<4;row++)
{
// Create column entries for above row.
for(col=0;col<4 && c != ' ';col++)
{
if(!isspace(c))
{
// Write record in Array , First value will be grid[0][0], second value will be grid[0][1]
grid[row][col] = c;  
}
c = getchar();
}
}
}

// Display the Records
for(x = 0; x < 4; x++)
{
for(y=0;y<4;y++)
{
putchar(grid[x][y]);
  
}
// Put line break after four record to display in 2D Array form
putchar(' ');
  
}
  
}
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote