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

5.CS110 – Programming In C (Practice #5) Topics Covered · Two-Dimensional Array

ID: 3589565 • Letter: 5

Question

5.CS110 – Programming In C (Practice #5)

Topics Covered

·      Two-Dimensional Array

·      End of File

·      For, While, If, Else, statements

1) For this assignment, the user will read in a text file that will contain the following:

            Textfile.txt

ABBA

QQQQQQQQQQ

QQQQQQQQQQ

XXXXXXXXXXXXX

XXXXXXXXXXXXX

2) When reading the “Textfile.txt” the program should read content until end of file, which will put the first four characters into a two-dimensional table that will be printed out at the end and will put the remaing charachters after into a seperate array that will also be printed at the end of the program. Use get/put char when handling end of file. Be sure to use seperate methods for each function:

Main Method will call functions:

-dataCollected

-printTable

-printExtraData

Data Collected will contain the data that was read from textfile.txt, still printing the original 4 charachters read and Extra Data collected. However, the first 4 charachters that will be placed into a 2D array and the remaining data will be stored in a seperate array that will be sent to a function called printExtraData.

Print Table will take in the array that was collected from "dataCollected" when reading the first 4 charachters from the file put it into a two-dimensional table and will be printed out.

Print Extra Data will take in the remaining data that wasn't stored into the two-dimensional array and will do a simple print statment showing the remaing content.

CS110terrinal:$ ./Practice5

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

char **dataCollected(char **tb,int *row,int *col,char *arr,int *size);

void printTable(char **tb,int r,int c);

void printExtraData(char *arr,int size);

void allocateMemoryforTale(char **tb, int *row, int *col,int charLength);

void fill2dArray(char *s,char **t, int r, int col);

void fillArray(char *src, char *dst, int len);

int main()

{

char **tb =NULL;

char arr[100];

int r, c, l;

//I generalised this function to take chars more than 4 and it creats table according to the number of chars

tb = dataCollected(tb, &r, &c, arr, &l);

printf("This is the tale created from the %d icharaters collectd from file read: ",r*c);

printTable(tb, r, c);

//read extra data

printf("This is the data collected after the 4 chars: ");

printExtraData(arr, l);

}

char **dataCollected(char **tb, int *row, int *col, char *arr, int *size)

{

FILE *fp;

char ch;

char str[100];

int i =0,first=1,j;

fp = fopen("textfile.txt", "r");

//chk if input file testfile is open

if (!fp)

{

printf("not able to open input file testfile.txt ");

return NULL;

}

while (fscanf(fp, "%c", &ch) != EOF)

{

if (ch == ' ' && first)

{

str[i] = '';

first = 0;

if (i % 2 == 0)

{

*row = (i) / 2;

*col = 2;

}

else

{

*row = (i + 1) / 2;

*col = 2;

}

//now allocate memory for 2-d array

tb = (char**)malloc((*row) * sizeof(char *));

for (j = 0; j < *row; j++)

tb[j] = (char*)malloc(*col*sizeof(char));

//allocateMemoryforTale(tb, row, col, i);

//fill 2-d array

fill2dArray(str,tb, *row, *col);

i = 0;

str[i] = '';

}

else

{

//place each chr read from fscanf into char array until /n is reached, then save this as string

str[i++] = ch;

}

}

str[i] = '';

*size = i;

fillArray(str, arr, i);

return tb;

}

void fillArray(char *src,char *dst, int len)

{

int i;

for (i = 0; i < strlen(src); i++)

{

dst[i] = src[i];

}

}

void fill2dArray(char *s,char **t, int r, int c)

{

int i = 0, j, k;

for (j = 0; j < r; j++)

{

for (k = 0; k < c; k++)

{

t[j][k] = s[i++];

}

}

}

void allocateMemoryforTale(char **tb, int *row, int *col,int charLength)

{

int j;

if (charLength % 2 == 0)

{

*row = (charLength) / 2;

*col = 2;

}

else

{

*row = (charLength + 1) / 2;

*col = 2;

}

//now allocate memory for 2-d array

tb = (char**)malloc((*row) * sizeof(char *));

for (j = 0; j < *row; j++)

tb[j] = (char*)malloc(*col*sizeof(char));

}

void printTable(char **tb, int r, int c)

{

int i, j;

for (i = 0; i < r; i++)

{

for (j = 0; j < c; j++)

{

printf("%c ", tb[i][j]);

}

printf(" ");

}

}

void printExtraData(char *arr, int size)

{

int i;

printf(" ");

for (i = 0; i < size; i++)

printf("%c ", arr[i]);

printf(" ");

}

-------------------------------------------------------------------------------------------

//output

This is the tale created from the 4 icharaters collectd from file read:

A B

B A

This is the data collected after the 4 chars:

Q Q Q Q Q Q Q Q

Q Q Q Q Q Q Q Q

X X X X X X X X X X X X

X X X X X X X X X X X X

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