Answered step by step
Verified Expert Solution
Question
1 Approved Answer
write a c program . (note the last photo is the RandomData.txt) Given a text file RandomData.txt, that contains random integer numbers. You are required
write a c program . (note the last photo is the RandomData.txt)
Given a text file "RandomData.txt", that contains random integer numbers. You are required to write a program that does the following 1. Read the content of the file and store the numbers in an integer array 2. Print the index of the nh smallest element of the array Here is an example to illustrate the requirements. If the input file contains the numbers: 9, 2, 11, 5, 3, 15, 23, and 12, which after sorting become: 2, 3, 5, 9, 11, 12, 15, 23, and if the user wants to find the index of the 4th minimum element in the original data, the program should print "o" as a result, since the 0th element (9) in the original array is the 4th minimum element in the sorted array. In other words, element 9 of the array is the 4th minimum element and is at position 0 in the original array (or in the file). To simplify the requirements, let us assume that the number of elements in the file does not exceed 100 elements Your program should mainly consist of functions, and the main function contains function calls only. It should start by reading the file "RandomData.txt", and check for its existence. If the opening of the file fails, such as the file does not exist, the code should exit h an appropriate message. If opening succeeds, the code should proceed with the following functions. 1- Read from file This function (ReadFile) receives as input the file pointer, reads and saves the elements in an integer array and returns this array. The function should also return the number of elements read from the file 2- Read minimum rank from the user This function (ReadRank) reads the rank of the minimum from the user. The function should check for the entered rank to be within limits of the size of the array and return a valid rank value. If the user enters 0 or a negative value, then this will be the value to stop the program, and the function should return 0. Upon return to the main program, the program should print the message "END !", as shown below, and stop the execution 3- Find minimum with given rank: This function (FindMinimum) is given the data array and the rank of the minimum to find as inputs. It should return the index of the minimum with the given rank. On return from the previous function called, the code should display in the main loop: the value of the given index Notes 1- To keep track of the positions of the elements of the array, you need a two dimensional array, with the first column containing the indices and the second column containing the array elements. 2- For the function to find the minimum with given rank, you will need to sort the input array. You may need either the selection sort algorithm from your lectures, or the bubble sort. Also, for that purpose you will need a swap function. 3- The rank of the minimum assumes the array elements indexed from 1 to size of array. Your array is indexed from 0 to size -1 4- Not to alter the initial array, when calling the function to find the minimum with given rank (step 3 above), you need to copy the original array into a temporary one and process the temporary array, thus keeping the original one unchanged. This should be implemented as a function with the two arrays (original and copy) as arguments. 5- You will also need a function to display the array (DisplayArray), as this is needed to check your resultsStep 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