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

How would I create this C program ? Description: You are to implement a program

ID: 3903881 • Letter: H

Question

How would I create this C program?

Description:

You are to implement a program that reads a list of numbers, puts them in an array of structs and performs some operations on them. The program will take the number of

structures to generate as a command line argument. The x and y values will be gathered from prompted input. Include the following struct definition at the top of your program:

struct data {

int x;

int y;

};

struct data* read_data(int size);

Parameter: size – The number of integer couples to be entered

Return: A pointer to an array where the structure data is stored

This function will dynamically create an array to hold the structs and read the user input to load the x and y data.

void divide_numbers(struct data *first);

Parameter: first – A data structure

Return: The result of x divided by y

struct data* sort(struct data* array, int size);

Parameter: array – An array of data

Parameter: size – The size of the array

Return: A pointer to the sorted array

The structures should be sorted based on their x value.

void print_data(struct data* array, int size);

Parameter: array – An array of structures

Prints out all of the points in the array, example output shown below

int main(int argc char **argv)

main should use read_data function to read in the proper number of structures. It should then call divide_numbers function on each struct, sort the array with sort function, and print it out.

Example input data

$ ./a.out 5

Enter a data set

7 1

Enter a data set

1 2

Enter a data set

9 2

Enter a data set

0 1

Enter a data set

9 6

Data sets divided

7 / 1 = 7.0000000

1 / 2 = 0.5000000

9 / 2 = 4.5000000

0 / 1 = 0.0000000

9 / 6 = 0.6666666

Data sets sorted

0 1

1 2

7 1

9 2

9 6

Explanation / Answer


Given below is the code for the question. Make sure to pass a command line argument i.e no. of datasets e.g. ./a.out 5
Please do rate the answer if it was helpful. Thank you


#include <stdio.h>
#include <stdlib.h>
struct data {
int x;
int y;
};

struct data* read_data(int size);
void divide_numbers(struct data *first);
struct data* sort(struct data* array, int size);
void print_data(struct data* array, int size);

int main(int argc, char **argv)
{
int size;
struct data* array;
int i;

if(argc != 2) //check if command line arg is passed
{
printf("Usage %s <number> ", argv[0]);
return 1;
}

size = atoi(argv[1]);
array = read_data(size);
printf("Data sets divided ");
for(i = 0; i < size; i++)
divide_numbers(&array[i]);

sort(array, size);
printf(" Data sets sorted ");
print_data(array, size);

}


struct data* read_data(int size)
{
struct data* array = malloc(sizeof(struct data) * size);
int i;
for(i = 0 ; i < size; i++)
{
printf("Enter a data set ");
scanf("%d %d", &array[i].x , &array[i].y);
}

return array;
}
void divide_numbers(struct data *first)
{
double ans = ((double)first->x) / first->y;

printf("%d / %d = %lf ", first->x, first->y, ans);
}
struct data* sort(struct data* array, int size)
{
int i, j, minIdx;
int compare;

for(i = 0 ; i < size; i++)
{
minIdx = i;
for(j = i + 1; j < size; j++)
{
compare = 0;
if(array[j].x < array[minIdx].x)
compare = -1;
else if(array[j].x == array[minIdx].x)
{
if(array[j].y < array[minIdx].y)
compare = -1;
}

if(compare < 0)
minIdx = j;
}

if(minIdx != i)
{
struct data temp = array[i];
array[i] = array[minIdx];
array[minIdx] = temp;
}
}
return array;
}
void print_data(struct data* array, int size)
{
int i;

for(i = 0; i < size; i++)
printf("%d %d ", array[i].x, array[i].y);

printf(" ");
}

output
====
$ ./a.out
Usage ./a.out <number>

$ ./a.out 5
Enter a data set
7 1
Enter a data set
1 2
Enter a data set
9 2
Enter a data set
0 1
Enter a data set
9 6
Data sets divided
7 / 1 = 7.000000
1 / 2 = 0.500000
9 / 2 = 4.500000
0 / 1 = 0.000000
9 / 6 = 1.500000


Data sets sorted
0 1
1 2
7 1
9 2
9 6

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