Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Please do this in C language using data files and arrays) Problem Statement : G

ID: 3711033 • Letter: #

Question

(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:

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

Explanation / Answer

// File Name: NthMinimumElement.c
#include<stdio.h>
#include<stdlib.h>
#define MAX 100

// Function to read file and display information
int ReadFile(int numbers[])
{
// Counter for number of data
int counter = 0;
// File pointer declared
FILE *readF;
// Opens the file for reading and checks whether it can be opened or not
if ((readF = fopen("RandomData.txt", "r")) == NULL)
{
// If unable to open shows error message
printf("Error! in opening file for reading");
// Program exits if the file pointer returns NULL.
exit(1);
}// End of if condition

// Loops till end of file
while(!feof(readF))
{
// Reads data and stores it in array counter index position
fscanf(readF, "%d", &numbers[counter++]);
}// End of while condition
// Closes the file
fclose(readF);
// Returns number of data in the file
return counter;
}// End of function

// Function to validate the rank.
// Returns zero for invalid rank otherwise returns the rank entered by the user
int ReadRank(int len)
{
// To store the rank
int rank;
// Accepts rank
printf(" Enter rank of minimum to find: ");
scanf("%d", &rank);
// Checks if the rank is zero or less than zero or greater than the length return zero
if(rank <= 0 || rank > len)
return 0;
// Otherwise valid rank.
else
// Returns the rank
return rank;
}// End of function

// Function to copy an array
void CopyArray(int ori[], int copy[], int len)
{
// Loop variable
int i;
// Loops till length
for(i = 0; i < len; i++)
// Copies each element from original array to copy array
copy[i] = ori[i];
}// End of function
void SortArray(int numbers[], int pos[], int len)
{
// Loop variables
int i, j;
// Loops till length
for(i = 0; i < len; i++)
// Stores the index position
pos[i] = i;
// Loops till one length
for(i = 0; i < len; i++)
{
// Loops one added to the outer loop index position to length
for(j = i + 1; j < len; j++)
{
// Checks if ith index position of the number is greater than the j th index position of the number array
if(numbers[i] > numbers[j])
{
// Swap the numbers
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;

// Swap the index position
temp = pos[i];
pos[i] = pos[j];
pos[j] = temp;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
}// End of function

// Function to display array
void DisplayArray(int numbers[], int len)
{
// Loop variable
int i;
// Loops till one length
for(i = 0; i < len; i++)
// Displays index position with the value
printf(" %-25d %d", i, numbers[i]);
}// End of function

// Displays the nth minimum element with index position
void FindMinimum(int Copy[], int pos[], int rank, int len)
{
printf(" %d-th minimum element is %d and its index is %d", rank, Copy[rank-1], pos[rank-1]);
}// End of function

// main function definition
int main()
{
// For original array
int Original[MAX];
// For copy of the original array
int Copy[MAX];
// For index position of the array
int pos[MAX];
int x = 0;
int counter;

// Calls the function to read file contents and stores it in original array
// Stores the return value as number of numbers
counter = ReadFile(Original);
printf(" Unsorted array read from file with indices");
// Calls the function to display the original array
DisplayArray(Original, counter);
// Calls the function to create duplicate copy of original array
CopyArray(Original, Copy, counter);
// Calls the function to sort the array
SortArray(Copy, pos, counter);
printf(" ------------------------------------------");
// Display the sorted array with index position
printf(" Sorted array with indices");
for(x = 0; x < counter; x++)
printf(" %-25d %d", pos[x], Copy[x]);
// Loops till valid rank
do
{
// Calls the function to validate the rank
int minPos = ReadRank(counter);
// Checks if the minPos is zero which is the return status of the ReadRank then stop the program
if(minPos == 0)
{
printf(" END !");
exit(1);
}// End of if condition
// Calls the function to Nth minimum element with index
FindMinimum(Copy, pos, minPos, counter);
}while(1);// End of do while loop
return 0;
}// End of main function

Sample Output:

Unsorted array read from file with indices
0 9
1 2
2 11
3 5
4 3
5 15
6 23
7 12
8 1
9 13
------------------------------------------
Sorted array with indices
8 1
1 2
4 3
3 5
0 9
2 11
7 12
9 13
5 15
6 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: 8

8-th minimum element is 13 and its index is 9
Enter rank of minimum to find: 23

END !