Answered step by step
Verified Expert Solution
Question
1 Approved Answer
code talked about in C: /* * Program to test sort function: * Generate a random sequence of N integers, sort them, and check *
code talked about in C:
/* * Program to test sort function: * Generate a random sequence of N integers, sort them, and check * that the sort succeeded. * This version prompts for N and a seed for generating the random * sequence but hardcodes a maximum for N. */ #include#include /* has EXIT_SUCCESS, EXIT_FAILURE */ #define MAX_COUNT 100 /* function declarations -- see definitions (at end) for more comments */ void fill_with_random(int a[], int size); void sort(int a[], int size); void print(int a[], int size); int find_out_of_order(int a[], int size); /* main program */ int main(void) { int data[MAX_COUNT]; int count; int seed; /* prompt for how many to sort, seed for rand() */ printf("how many to sort? "); if (scanf("%d", &count) != 1) { printf("value must be integer "); return EXIT_FAILURE; } if ((count MAX_COUNT)) { printf("value must be between 1 and %d ", MAX_COUNT); return EXIT_FAILURE; } printf("seed for generating random sequence? "); if (scanf("%d", &seed) != 1) { printf("value must be integer "); return EXIT_FAILURE; } if ((seed a[i+1]) { return i; } } return -1; } /* get index of smallest element of data[start .. num_items-1] */ int index_of_smallest(int data[], int num_items, int start) { int smallest = start; for (int i = start+1; i 1. (10 points) For this problem your mission is to further revise the sort program from Homework 5 so that rather than generating random data it reads the values to sort from a file and writes the sorted values to another file. The completed program should take two command-line arguments giving the names of the input and output files. (It should not prompt the user for anything.) The program should print appropriate error messages if not enough arguments are supplied, if it cannot open the input and output files, or if the input file contains anything but a sequence of integers. Since we have not yet talked about how to make arrays larger at runtime, just write the program with a fixed-size array for holding input, and have the program print an error message if the number of input values exceeds the size of the array. It's up to you whether you keep the part of the existing program that checks whether the sort succeeds (I say" might as well"); if you do, just have it print to standard output as before. Hints: o Sample program whilesum-fromfile.c illustrates reading a sequence of integers from an input file. Notice that the while loop to read integers stops when fscans detects either an error or the end of the file.The if after the loop uses feof to find out which of these two things happened feof returns a nonzero value ("true") when the previous attempt to read something detected end of file, zero ("false") otherwise (ie., an error). Be advised that ferror is useful only for detecting I/O errors and is not set if fscanf can read input from the stream but can't convert it to
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started