Question: // C Programming GCC compiler // THEIR IS NO IMAGE in this post I am creating a bowling score board program that utilizes a
// C Programming \\ GCC compiler // THEIR IS NO IMAGE in this post
I am creating a bowling score board program that utilizes a number of different programming topics. I am in need of my program to be modifed so it outputs a bowling scoreboard that lists each bowling frame so the output is more visually appealing.
Program Source code:
#include
#include
#include
//function to sort the elements of an 2D array row wise
struct array_dim{
int rows;
int cols;
};
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 row_sum(int **A, int row, int N)
{
if(N <=0)
return 0;
return row_sum(A, row, N-1) + A[row][N-1];
}
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
struct array_dim temp = {.rows = n, .cols = n};
printf("Array dimensions are, No of rows are %d, No of columns are %d ",temp.rows,temp.cols);
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(" ");
}
printf(" ");
printf("Finding the sum of 3rd row using recursion ");
printf(" ");
int sum = row_sum(array, 2, 4);
printf("Sum is %d ", sum);
//(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(" ");
}
for (int i = 0; i < n; i++) {
free(array[i]);
}
free(array);
array = NULL;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
