Introduction to c++ Write a short program that opens the file numbers.txt. The n
ID: 3762128 • Letter: I
Question
Introduction to c++ Write a short program that opens the file numbers.txt. The nunibers.txt is a text file that contains 20,000 random numbers. Your program should: 1. Read the numbers.txt numbers into an int array 2. Sort the array using the bubble sort or another sort discu55eu in the text (.. Starting-out-with-c-plus--plus-8th-edition-ChaPter-8) 3. Keep track of, and print Out, the number of swaps and compares performed by the sort 4. Print out the sorted array in an efficient eye pleasing manner. For example, don?t just print out every number followed by a new line (printing 20,000 new lines). Print out columns of numbers, giving the user the option to print out a page at a time. 5. Write the sorted array to a file called sortednumbers.txt 6. Ask the user for a number to search for in the array. . After accepting this, the program should search the array using a binary search algorithm. Print out whether or not the number was found and in how many compares. Note.: Your program must have individual functions for the bubble sort, binary search, file input, and file output.Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
void bubbleSort(int Array[], int size)
{
int compares = 0, swaps = 0, temp, i, j;
for(int i = 0; i < size-1; i++)
for(j = 0; j < size-i-1; j++)
{
compares++;
if(Array[j] > Array[j+1])
{
swaps++;
temp = Array[j];
Array[j] = Array[j+1];
Array[j+1] = temp;
}
}
printf("The number of compares involved here is: %i ", compares);
printf("The number of swaps involved here is: %i ", swaps);
}
int binarySearch(int Array[], int size, int key)
{
int low = 0, high = size - 1, mid, compares = 0;
while(low <= high)
{
compares++;
mid = (low + high) / 2;
if(Array[mid] == key)
{
printf("It took a total of %i Comparisions. ", compares);
return mid;
}
else if(key < Array[mid])
high = mid - 1;
else
low = mid + 1;
}
printf("It took a total of %i Comparisions. ", compares);
return -1;
}
void readNumbersFromFile(int Array[], int size)
{
int count = 0;
FILE *fp;
fp = fopen("numbers.txt", "r");
while(!feof(fp) && count < 20000)
{
fscanf(fp, "%i", &Array[count]);
count++;
}
}
void writeNumbersToFile(int Array[], int size)
{
int i;
FILE *fp;
fp = fopen("sortednumbers.txt", "w");
for(i = 0; i < size; i++)
{
fprintf(fp, "%i ", Array[i]);
if(i%20 == 0)
fprintf(fp, " ");
}
}
void printArray(int Array[], int size)
{
int i;
for(i = 0; i < 20000; i++)
{
printf("%4i ", Array[i]);
if(i % 20 == 0)
printf(" ");
if(i % 200 == 0)
{
printf("Press any key to continue.....");
getch();
}
}
}
int main()
{
int Array[20000], pos, key;
readNumbersFromFile(Array, 20000);
bubbleSort(Array, 20000);
printArray(Array, 20000);
writeNumbersToFile(Array, 20000);
printf("Enter the number to search: ");
scanf("%i", &key);
pos = binarySearch(Array, 20000, key);
if(pos == -1)
printf("Element not found in the list... ");
else
printf("Element found at position %i ", pos);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.