Any help in this question is more appreciated write c program: the program shoul
ID: 3713183 • Letter: A
Question
Any help in this question is more appreciated
write c program: the program should use five functions one to read int numbers from file using malloc where the first number in the file is the size of the list. the second function sort these numers and print the sorted number, the size of the list, the smaller number and the last number. the third function is to find the the first Quartile. the fourth function is to find the the second Quartile ?and the last function is to find the third Quartile. the program should use pointer and array.
example file.txt
6
9
7
1
8
3
4
Explanation / Answer
PLEASE REFER BELOW CODE
#include<stdlib.h>
#include<stdio.h>
int *read_file(char *name, int *arr_size)
{
FILE *fp; //file pointer
int i,temp;
int *arr;
fp = fopen(name, "r"); //opening file in read mode
fscanf(fp, "%d", &temp); //reding first element from file i.e. size of array
*arr_size = temp;
arr = (int *)malloc(temp * sizeof(int)); //allocating array of size temp
//reding remaining elements from array
for (i = 0; i < temp; i++)
{
fscanf(fp, "%d", &arr[i]);
}
//closing file
fclose(fp);
return arr;//return array
}
// A function to implement bubble sort
void sort_arr(int arr[], int n)
{
int i, j,temp;
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
//printing array
printf("Printing Array ..... ");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
printf(" Size of the array = %d ", n);
printf(" Smaller Number = %d ", arr[0]);
printf(" Last Number = %d ", arr[n-1]);
}
float first_quartile(int *arr, int arr_size)
{
int half_arr;
//checking array size as odd or even
//if size is even
if(!(arr_size % 2))
{
half_arr = arr_size/2; //divding array into lower half i.e. first 25%
if(half_arr % 2) //if first 25% is odd then return middle element as median
return arr[half_arr/2];
else //if lower 25% is even then return mean of central two elements as median
return (arr[half_arr/2] + arr[(half_arr/2) - 1])/2.0;
}
else
{
half_arr = arr_size/2; //if array half is odd then return half element
return arr[half_arr/2];
}
}
float second_quartile(int *arr, int arr_size)
{
if(arr_size % 2) //if array half size is odd
return arr[arr_size/2];
else //if array half size is even
return (arr[arr_size/2] + arr[(arr_size/2) - 1])/2.0;
}
float third_quartile(int *arr, int arr_size)
{
int half_arr;
half_arr = arr_size/2;
if((arr_size - half_arr) % 2) //checking last 25% size as odd
return arr[((arr_size - half_arr)/ 2) + half_arr];
else //last 25% as even
return (arr[((arr_size - half_arr)/ 2) + half_arr] + arr[((arr_size - half_arr)/ 2) + half_arr - 1])/2.0;
}
int main()
{
int *arr; //array pointer
int arr_size; //size of the array
float first_quart,second_quart,third_quart; //for all quartiles
char name_file[100] = "file.txt"; //file name in char array
arr = read_file(name_file,&arr_size); //function to read file and store in array arr
sort_arr(arr,arr_size); //function to sort array
first_quart = first_quartile(arr,arr_size); //function to calculate first Quartile
second_quart = second_quartile(arr,arr_size);//function to calculate second Quartile
third_quart = third_quartile(arr,arr_size); //function to calculate third Quartile
//display result
printf(" First Quartile = %f Second Quartile = %f Third Quartile = %f ", first_quart,second_quart,third_quart);
return 0;
}
PLEASE REFER BELOW OUTPUT
Printing Array .....
1 3 4 7 8 9
Size of the array = 6
Smaller Number = 1
Last Number = 9
First Quartile = 3.000000
Second Quartile = 5.500000
Third Quartile = 8.000000
Process returned 0 (0x0) execution time : 0.039 s
Press any key to continue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.