Question
In the C language , I need an experienced programmer to implement two more topics to my already functioning program. Please include output terminal ,
In the C language, I need an experienced programmer to implement two more topics to my already functioning program.
Please include output terminal, and compile using gcc, Thank you!
Topics to add to the program(Pick 2):
Structures Linked Lists (Pointers) Recursion Stack/Queue Header Files
I have used the following topics in my program:
Strings 2D Arrays File I/O Command Line Arguments Pass by Reference and sorting.
Here is my program source code:
#include
#include
#include
//function to sort the elements of an 2D array row wise
void sort(int **arr, int n) {
//(1) sorting each row elements
int a;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = (j + 1); k < n; ++k) {
if (arr[i][j] > arr[i][k]) {
a = arr[i][j];
arr[i][j] = arr[i][k];
arr[i][k] = a;
}
}
}
}
}
//functio to read the string(line) from a file
int read_line(FILE *in, char *buffer, size_t max) {
return fgets(buffer, max, in) == buffer;
}
int main(int argc, char *argv[]) {
//(1)checking for the command line argument
//handling command line arguments count
if (argc != 2) {
printf("Specify the file name");
return 0;
}
//(2)handling string here
//(1)And reading the values passed from command line argument
char* file = argv[1];
printf("File names passed from command line arguments is: %s ", file);
//Here fixing the 2D array size, if you want you can changeit
//but at the same time you need add that many columns in the data.txt file
int n = 4;
// (3)Handling 2D arrays
int **array;
array = malloc(n * sizeof(int *));
//checking if the memory is allocated or not
if (array == NULL) {
printf("Out of memory ");
exit(1);
}
//allocating memory for each field in the array
for (int i = 0; i < n; i++) {
array[i] = malloc(n * sizeof(int));
if (array[i] == NULL) {
printf("Out of memory ");
exit(1);
}
}
// Open the file data.txt to read the 2D array data.
//(4)Handling the files concept here
FILE *pFile = fopen(file, "r");
if (pFile == NULL) {
//printing error if there is any issue with file
printf("Error: Unable to open the file");
exit(1);
}
//reading the matrix into 2D array
int x;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
fscanf(pFile, " %d", &x);
array[i][j] = x;
}
}
//printing the 2D array
printf(" 2D Array read from file is: ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", array[i][j]);
}
printf(" ");
}
//(5)passing the 2D array as Pass by Reference
sort(array, n);
//printing the results
printf(" 2D Array after sorting each row in ascending order is: ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", array[i][j]);
}
printf(" ");
}
// Deallocate 2D array
for (int i = 0; i < n; i++) {
free(array[i]);
}
free(array);
array = NULL;
//Reading a string from the text file and printing its length
}
// Close files
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started