Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

QUESTION

///C Programing Project\\\\ GCC Compiler I would like the program to become more real world useful, I am open to your ideas(I was thinking some sort...

///C Programing Project\\ GCC Compiler

I would like the program to become more real world useful, I am open to your ideas(I was thinking some sort of a sports scoreboard program). This question seems to never get answered, so I decided to shorten the question for the tutors.. All I need is someone to give me ideas, and modify the code so it's useful for a real world situation.

Thanks

////Source Code\\

#include <stdio.h>

#include <stdlib.h>

#include<string.h>

//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: %sn", 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 memoryn");

    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 memoryn");

      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 %dn",temp.rows,temp.cols);

  printf("n2D Array read from file is:n");

  for (int i = 0; i < n; i++) {

    for (int j = 0; j < n; j++) {

      printf("%d ", array[i][j]);

    }

    printf("n");

  }

  printf("n");

  printf("Finding the sum of 3rd row using recursionn");

  printf("n");

  int sum = row_sum(array, 2, 4);

  printf("Sum is %dn", sum);

  //(5)passing the 2D array as Pass by Reference

  sort(array, n);

  //printing the results

  printf("n2D Array after sorting each row in ascending order is:n");

  for (int i = 0; i < n; i++) {

    for (int j = 0; j < n; j++) {

      printf("%d ", array[i][j]);

    }

    printf("n");

  }

  for (int i = 0; i < n; i++) {

    free(array[i]);

  }

  free(array);

  array = NULL;

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question