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 ? Purpose: ? structs, argc / argv Description:

ID: 3902478 • Letter: H

Question

How would I create this C program?

Purpose:

?

structs, argc / argv

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

Below is your code. Please do rate this answer positive, If i was able to help you. Let me know if you have any issues in comments

#include<stdlib.h>

#include<stdio.h>

#include<math.h>

//definition of required structure

struct data{

int x;

int y;

};

//declaration of all required function

//definition will be below main function

struct data* read_data(int size);

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

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

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

//main function

int main(int argc,char **argv){

if(argc > 0) {

int size = atoi(argv[1]);

struct data *ptr1 = read_data(size);

divide_numbers(ptr1,size);

struct data *ptr2 = sort(ptr1,size);

print_data(ptr2,size);

return 0;

} else {

printf("You have not specified size as argument. ");

}

}

//all required function definition

//this function will dynamically allocate memory for structure array.

//as well as take input or we can say initialize each element of structure

//array.

struct data* read_data(int size){

int i;

struct data *pointer;

pointer = new data[size];

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

printf("Enter a data set ");

scanf("%d",&pointer[i].x);

scanf("%d",&pointer[i].y);

}

return pointer;

}

//in this function since our point variable i.e x and y are integer

//so performing division on integer will led to the truncation of decimal

//value.

//due to this reason we have to typecast the result of division

void divide_numbers(struct data *array, int size){

float k;

int i;

printf("Data sets divided ");

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

printf("%d/%d = %f ",array[i].x,array[i].y,(float)array[i].x/array[i].y); //type casting integer to float

}

}

//Sorting array

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

{

struct data temp;

int i, j;

float distance1, distance2;

//Iterating over outer array

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

{

//Iterating over remaining array

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

{

//Calculating distance

distance1 = sqrt( (array[i].x * array[i].x) + (array[i].y * array[i].y) );

distance2 = sqrt( (array[j].x * array[j].x) + (array[j].y * array[j].y) );

  

//comparing distances

if(distance1 < distance2)

{

//Swapping elements

temp = array[i];

array[i] = array[j];

array[j] = temp;

}

}

}

//Return array

return array;  

}

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

int i;

printf("Data sets sorted ");

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

printf("%d %d ",array[i].x,array[i].y);

}

}

Output

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