I am writing a C program that reads integersfrom a text file whose name is given
ID: 3612673 • Letter: I
Question
I am writing a C program that reads integersfrom a text file whose name is given as a command-line argument.The program will display the largest, smallest, and median integerfromthe file.I have some partial code written to open the file and read thefile, plus I already have the sorting function ready to peice intothe program. I do need help sorting out the "file name given as acommand line argument"
Also can I just call my sort fuction like normal in main(), callingthe function and sending the name of the char array and thesize?
Code written so far:
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
#define FILE_NAME "numbers.txt"
int main(int argc, char *argv[])
{
char numbers[MAX];
FILE* pFile;
if(argc != 2)
{
printf("usage:%s large_small_median", argv[0]);
}
else
{
pFile= fopen(argv[1], "r");
if(pFile== 0)
{
printf("Erroropening file ");
}
else
{
intx;
while((x = fgetc(pFile)) != EOF)
{
printf("%c",x);
}
}
while(fgets (numbers, MAX, pFile) != NULL )
{
printf("%s", numbers);
}
fclose(pFile);
}
printf(" ");
getchar();
return 0;
}
void quicksort(int a[], int low, int high)
{
int middle;
if(low >= high)
return;
middle = split(a, low,high);
quicksort(a, low,middle-1);
quicksort(a, middle+1,high);
}
int split(int a[], int low, int high)
{
int part_element = a[low];
for(; ;)
{
while(low< high && part_element <= a[high])
high--;
if(low>= high)
break;
a[low++]= a[high];
while(low< high && a[low] <= part_element)
low++;
if(low>= high)
break;
a[high--]= a[low];
}
a[high] = part_element;
return high; } I am writing a C program that reads integersfrom a text file whose name is given as a command-line argument.The program will display the largest, smallest, and median integerfromthe file.
I have some partial code written to open the file and read thefile, plus I already have the sorting function ready to peice intothe program. I do need help sorting out the "file name given as acommand line argument"
Also can I just call my sort fuction like normal in main(), callingthe function and sending the name of the char array and thesize?
Code written so far:
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
#define FILE_NAME "numbers.txt"
int main(int argc, char *argv[])
{
char numbers[MAX];
FILE* pFile;
if(argc != 2)
{
printf("usage:%s large_small_median", argv[0]);
}
else
{
pFile= fopen(argv[1], "r");
if(pFile== 0)
{
printf("Erroropening file ");
}
else
{
intx;
while((x = fgetc(pFile)) != EOF)
{
printf("%c",x);
}
}
while(fgets (numbers, MAX, pFile) != NULL )
{
printf("%s", numbers);
}
fclose(pFile);
}
printf(" ");
getchar();
return 0;
}
void quicksort(int a[], int low, int high)
{
int middle;
if(low >= high)
return;
middle = split(a, low,high);
quicksort(a, low,middle-1);
quicksort(a, middle+1,high);
}
int split(int a[], int low, int high)
{
int part_element = a[low];
for(; ;)
{
while(low< high && part_element <= a[high])
high--;
if(low>= high)
break;
a[low++]= a[high];
while(low< high && a[low] <= part_element)
low++;
if(low>= high)
break;
a[high--]= a[low];
}
a[high] = part_element;
return high; }
Explanation / Answer
please rate - thanks sample run - note the quotes around thefile name-file must be in same folder as program note minor changes in program C:UsersDefault.XPS420Desktopcramster>untitled1"numbers.txt" The unsorted numbers 5 6 3 8 2 The sorted numbers 2 3 5 6 8 C:UsersDefault.XPS420Desktopcramster> code with corrections #include #include #define MAX 1000 //#define FILE_NAME "numbers.txt" void quicksort(int a[], int low, int high); int split(int a[], int low, int high); int main(int argc, char *argv[]) { int a[MAX]; //char numbers[MAX]; FILE* pFile; int low=0, high=0,i; if(argc != 2) { printf("usage: %s large_small_median", argv[0]); } else { pFile = fopen(argv[1], "r"); if(pFile == 0) { printf("Error opening file "); } else { printf("The unsorted numbers "); while( (fscanf(pFile,"%d",&a[high])) >0) { printf("%d ",a[high]); high++; } } low=0; quicksort(a,low,high); /*while (fgets (numbers, MAX, pFile) != NULL ) { printf ("%s", numbers); }*/ printf("The sorted numbers "); for(i=0;i= high) return; middle = split(a, low, high); quicksort(a, low, middle-1); quicksort(a, middle+1, high); } int split(int a[], int low, int high) { int part_element = a[low]; for(; ;) { while(lowRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.