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

Hello, need help with converting this code to run smoothly on c++. I keep gettin

ID: 3762631 • Letter: H

Question

Hello, need help with converting this code to run smoothly on c++. I keep getting error messages that I can't seem to find the proper answer to. Everytime I fix one issue 4 others come up. I am using Visual Studio 2013 standard.

for example: How do you change fopen() to something that is allowed in C++?? I keep getting "unsafe..try fopen_s.... _CRT_SECURE_NO_WARNINGS" however when I do the modification it then gives other errors.

some info on the program: it must read a .txt file with 20000 random numbers and sort them into an array using bubble sort which is then presented to the user in a nice list which also prints/saves this sorted array back into another file. Now the program must prompt the user to input a number to search for with a binary search algorithim and then give back whether it was found or not, and in how many compares it took to find it.

please and thank you

#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);
}

Explanation / Answer

#include "stdafx.h" 03 #include // for streams 04 #include // for setw() 05 #include // for files 06 #include // for exit 07 using namespace std;   08 09   const int size=100;   10   int A[size]={0}; 11 12 void read_data() 13 { 14     int x, i=0;     15    char name[20];   16 17                          // ifstream constructor opens file   18     ifstream fin; 19     cout
Dr Jack
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote