Question: C programming. I need someones assistance to help me to get this to work in the C language. I need this program to read from
C programming. I need someones assistance to help me to get this to work in the C language. I need this program to read from a .txt file something like [ BMW 330i 3.0 569000 Porsche GT3 3.8 23040 ]. I need the program to order the floats from low to high and a function from high to low as well as the same for the int's ( I have already created these functions try not to change them if you don't need to). I also need this file to be read into a array which the memory is allocated for dynamically and must call a function that opens a file of unknown size and stores the data in a array of struct***. Please read completely. I will give a Like :)
#include
struct data { char color[40]; float engine; int n; char make[40]; };
void IntL2H( int a[], int n) { int i, j, t; for( i = n - 2; i >= 0; i--) { for(j = 0; j <= i; j++) { if(a[j] > a[j + 1]) { t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } } } void IntH2L( int a[], int n) { int i, j, t; for( i = n - 2; i >= 0; i--) { for( j = 0; j <= i; j++) { if(a[j] < a[j +1]) { t = a[j]; a[j] = a[j + 1]; a[j +1] = t; } } } } void FloatL2H(float a[], int n) { int i, j; float t; for(i = n - 2; i >= 0; i--) { for(j = 0; j <= i; j++) { if(a[j] > a[j + 1]) { t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } } } void FloatH2L(float a[], int n) { int i, j; float t; for(i = n - 2; i >= 0; i--) { for(j = 0; j <= i; j++) { if(a[j] , a[j + 1]) { t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } } } void readData(struct data *D, int n) { FILE *fp; int i = 0, counter = 0, len;
fp = fopen("hw2.txt", "r"); if (fp == NULL) { printf(" Error unable to open file"); exit(0); } //find array size fseek(fp, 0, SEEK_END); size_t size = ftell(fp); fseek(fp, 0, SEEK_SET); //allocate mem char *array = (char*)malloc(size*sizeof(char) +1); //store into array fread(array, 1, size, fp); array[size] = ' '; //get elements of array while(!feof(fp)) { scanf(fp, "%s %d %d %s", color, make, engine, n); } }
int main () { int input;
while (input != 5) { printf("1. Sort the data by the float vlaue& print high to low " "2. Sort the data by the float value & print low to high " "3. Sort the data by the int value & print high to low " "4. Sort the data by the int value & print low to hugh " "5. Exit "); printf("Enter a number "); scanf("%d", &input);} while (input != 5);
//Call functions here
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
