Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(Please do this in C language using data files and arrays) Problem Statement : Given a text file RandomData.txt, that contains random integer numbers. You

(Please do this in C language using data files and arrays)

Problem Statement:

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 nth 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 0 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.

Program Requirements:

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 exitwith an appropriate message. If opening succeeds, the code should proceed with the following functions.

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.

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.

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:

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.

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.

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.

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.

You will also need a function to display the array (DisplayArray), as this is needed to check your results.

Your main program should use the functions, above, in order to demonstrate modular programming, in the sense that the main program consists only of modules (function calls) that perform specific tasks, as outlined, above.

SAMPLE RUN:

image text in transcribed

Unsorted array read from file with indices 2 4 5 15 23 12 13 Sorted array with indices 4 12 13 15 23 Enter rank of minimum to find: 2 2-th minimum element is 2 and its index is 1 Enter rank of minimum to find: 6 6-th minimum element is 11 and its index is 2 Enter rank of minimum to find: END! Process exited after 17.12 seconds with return value e Press any key to continue

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases

Authors: Ian Robinson, Jim Webber, Emil Eifrem

1st Edition

1449356265, 978-1449356262

More Books

Students also viewed these Databases questions

Question

What are the benefits of activity-based costing?

Answered: 1 week ago

Question

5. Save raster im?

Answered: 1 week ago

Question

2. What do you believe is at the root of the problem?

Answered: 1 week ago