Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need to submit within 1 hour Please Help!! below is code to sort a text file and print. I cant make it work, and I

Need to submit within 1 hour Please Help!!

below is code to sort a text file and print. I cant make it work, and I need to konw how to create the actual text file. Also, after the code, i have included all the instructions/output and an answer from an expert whose code for the sorting fuctions I have already used. Also, what are the steps to create a text file to run on this? (randomB,txt) THANK YOU

const int MAXSIZE = 150;

#include

#include

using namespace std;

const int MAXSIZE = 150;

void loadArray(int* nums, int& numElements) {

int data = 0;

int read_count = 1;

char fname[20];

cout

cin >> fname;

FILE* ifile;

ifile = fopen(fname, "r"); //open input file

if (ifile == NULL) {

cout

return;

}

while (read_count>0 && numElements

{

read_count = fscanf(ifile, "%d", &data); //read data from inputfile

nums[numElements++] = data;

}

}

void printArray(int* nums, int& numElements) {

for (int i = 0; i

{

cout

}

cout

}

double averageArray(int total, int numofelement) {

return (double)total / (double)numofelement;

}

void sortArray(int* nums, int size) {

for (int i = 0; i

for (int j = i + 1; j

if (nums[i]>nums[j]) {

int temp = nums[i];

nums[i] = nums[j];

nums[j] = temp;

}

}

}

}

int countMultiplesOf(int* nums, int size, int multof) {

int count = 0;

for (int i = 0; i

if (nums[i] % multof == 0)

count++;

}

return count;

}

int totalOfNumbers(int* nums, int numElements) {

int sum = 0;

for (int i = 0; i

{

sum = sum + nums[i];

}

return sum;

}

int findMedian(int* nums, int& numElements) {

return nums[numElements / 2];

}

int main()

{

int nums[MAXSIZE];;

int numElements;

int total;

double average;

int multiplesOf;

int numMultiples;

double median;

// Change the statement below to print your name

cout

// Call the loadArray function

cout

// Call the function printArray

// Call the function totalOfAllNumbers

cout

// Call the function averageOfArray;

cout

cout

cin >> multiplesOf;

// Call the function countMultiplesOf;

cout

cout

// Call the function printArray

// Call the function sortArray

cout

// Call the function printArray

// Call the function findMedianOfNumbers

cout

return 0;

}

Please provide beginner level C++ code based on the given instructions **Cannot use .sort, must write a sorting algoriithm. I have a sample of the algorithm in my notes which I can post if it would help. The assignment instructions, the main class, and two text files are included below. THANK YOU!! If there's anything else I can provide or change to help out, let me know in the comments!

image text in transcribed

MAIN:

const int MAXSIZE = 150;

int main()

{

int nums[MAXSIZE];;

int numElements;

int total;

double average;

int multiplesOf;

int numMultiples;

double median;

// Change the statement below to print your name

cout

// Call the loadArray function

cout

// Call the function printArray

// Call the function totalOfAllNumbers

cout

// Call the function averageOfArray;

cout

cout

cin >> multiplesOf;

// Call the function countMultiplesOf;

cout

cout

// Call the function printArray

// Call the function sortArray

cout

// Call the function printArray

// Call the function findMedianOfNumbers

cout

return 0;

}

2 TEXT FILES:

randomA.txt

63 -59 -54 14 51 5 88 23 -87 -77 68 -30 -8 -76 -57 30 85

randomB.txt

-42 -92 82 -38 55 20 -61 2 77 -13 -22 -51 19 85 -97 -41 -88 9 -74 -18 7 43 51 36 96 -71 -52 17 -91 -34 -49 -33 31 -41 -72 -23 5 -71 87 46 93 -32 57 66 -34 -7 3 16 -2 -27 11 5 16 -59 -42 7 -28 41 -87 -90 70 66 6 -33 73 -64 -86 42 54 -4 24 -48 73 11 72 71 -9 49 -95 -79 -29 -37 92 -43 64 23 -34 -36 16 85 -51 -47 65 100 90 21 -50 -20 -29 76 -1 -34 60 -85

SAMPLE OUTPUTS:

image text in transcribed

image text in transcribed

Show transcribed image text

Expert Answer

image text in transcribedTanmay answered this

Save this code as arrayTest.cpp, compile and run. Create an input file randomB(as per example).txt and keep in the same directory with the source code.

main - a main function has been given. You are to only add the function calls. The comments will tell you where the function calls should be done. Do NOT change main. The following functions should be implemented loadArray This function has an array and a variable to count the number of elements in the array passed into it. The function asks the user for the file name, opens the file with the filename and reads the numbers into the array. It also keeps track of how many numbers are in the array. It makes sure the file opens correctly and if it doesn't, it exits the program. This method should also check to make sure that more than the MAXSIZE values are not put into the array printArray this function is passed an array and the number of values in the array. It then prints average Array this function is passed an array and the number of values in the array. It returns sortArray sorts the numbers in the array. It is passed the array and the number of values in the countMultiplesOf this function is passed an array, the number of values in the array, and the the values in the array, eight per line as shown in the output the average of the numbers in the array array number that the function is counting the multiples of. For example, if a five is passed in, this function will count all the multiples of five in the array totalOfNumbers this function is passed an array and the number of values in the array. It returns the total of the numbers in the array findMedian this function is passed an array and the number of values in the array. It returns the s in the array. If there are an odd number of elements in the array, the median of the number median is the middle number (ex: the median of 1, 2, 3, 4,5 is 3). If there is an even number of elements in the array, the median is the average of the middle 2 numbers. (ex: the median of 1 2, 3, 4 is 2.5) There is NO loop in this method

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

Step: 3

blur-text-image

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

Database And Expert Systems Applications 31st International Conference Dexa 2020 Bratislava Slovakia September 14 17 2020 Proceedings Part 1 Lncs 12391

Authors: Sven Hartmann ,Josef Kung ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

303059002X, 978-3030590024

More Books

Students also viewed these Databases questions