Please provide beginner level C++ code based on the given instructions ** Cannot
ID: 3737119 • Letter: P
Question
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!
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 << "Programmed by Lori Tetzner" << endl << endl;
// Call the loadArray function
cout << "The array after it is loaded: " << endl;
// Call the function printArray
// Call the function totalOfAllNumbers
cout << "The total of the numbers in the array is: " << total << endl;
// Call the function averageOfArray;
cout << "The average of the numbers in the array is: " << average << endl;
cout << "Enter the number you want to count multiples of: ";
cin >> multiplesOf;
// Call the function countMultiplesOf;
cout << "There are " << numMultiples << " multiples of "
<< multiplesOf << " in the array" << endl << endl;
cout << "The array before sorting " << endl;
// Call the function printArray
// Call the function sortArray
cout << "The array after sorting " << endl;
// Call the function printArray
// Call the function findMedianOfNumbers
cout << "The median of the array is " << median << endl;
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:
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 methodExplanation / Answer
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.
/***********************/
#include <iostream>
#include <stdio.h>
using namespace std;
const int MAXSIZE = 150;
void loadArray(int* nums,int& numElements){
int data=0;
int read_count=1;
char fname[20];
cout<<"Enter the file name: ";
cin>>fname;
FILE* ifile;
ifile=fopen(fname,"r"); //open input file
if(ifile==NULL){
cout<<"Invalid file name"<<endl;
return;
}
while(read_count>0 && numElements<MAXSIZE)//loop till end of file read
{
read_count=fscanf(ifile,"%d", &data); //read data from inputfile
nums[numElements++]=data;
}
}
void printArray(int* nums,int& numElements){
for(int i=0;i<numElements;i++)
{
cout<<nums[i]<<" ";
}
cout<<endl;
}
double averageArray(int total,int numofelement){
return (double)total/(double)numofelement;
}
void sortArray(int* nums,int size){
for(int i=0;i<size;i++){
for(int j=i+1;j<size;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<size;i++){
if(nums[i]%multof==0)
count++;
}
return count;
}
int totalOfNumbers(int* nums,int numElements){
int sum=0;
for(int i=0;i<numElements;i++)
{
sum=sum+nums[i];
}
return sum;
}
int findMedian(int* nums,int& numElements){
return nums[numElements/2];
}
int main()
{
int nums[MAXSIZE]={0};
int numElements=0;
int total=0;
double average=0.0;
int multiplesOf;
int numMultiples;
double median;
// Change the statement below to print your name
cout << "Programmed by Lori Tetzner" << endl << endl;
// Call the loadArray function
loadArray(nums,numElements);
cout << "The array after it is loaded: " << endl;
// Call the function printArray
printArray(nums,numElements);
// Call the function totalOfAllNumbers
total=totalOfNumbers(nums,numElements);
cout << "The total of the numbers in the array is: " << total << endl;
// Call the function averageOfArray;
average=averageArray(total,numElements);
cout << "The average of the numbers in the array is: " << average << endl;
cout << "Enter the number you want to count multiples of: ";
cin >> multiplesOf;
// Call the function countMultiplesOf;
numMultiples=countMultiplesOf(nums,numElements,multiplesOf);
cout << "There are " << numMultiples << " multiples of "
<< multiplesOf << " in the array" << endl << endl;
cout << "The array before sorting " << endl;
// Call the function printArray
printArray(nums,numElements);
// Call the function sortArray
sortArray(nums,numElements);
cout << "The array after sorting " << endl;
// Call the function printArray
printArray(nums,numElements);
// Call the function findMedianOfNumbers
median=findMedian(nums,numElements);
cout << "The median of the array is " << median << endl;
return 0;
}
/**********************/output
Programmed by Lori Tetzner
Enter the file name: randomB.txt
The array after it is loaded:
-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 -85
The total of the numbers in the array is: -195
The average of the numbers in the array is: -1.85714
Enter the number you want to count multiples of: 3
There are 32 multiples of 3 in the array
The array before sorting
-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 -85
The array after sorting
-97 -95 -92 -91 -90 -88 -87 -86 -85 -85 -79 -74 -72 -71 -71 -64 -61 -59 -52 -51 -51 -50 -49 -48 -47 -43 -42 -42 -41 -41 -38 -37 -36 -34 -34 -34 -34 -33 -33 -32 -29 -29 -28 -27 -23 -22 -20 -18 -13 -9 -7 -4 -2 -1 2 3 5 5 6 7 7 9 11 11 16 16 16 17 19 20 21 23 24 31 36 41 42 43 46 49 51 54 55 57 60 64 65 66 66 70 71 72 73 73 76 77 82 85 85 87 90 92 93 96 100
The median of the array is -2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.