Question: Need help in c++, I have the code but having issues displaying the output, please and thank you Program 3: Write a program with several
Need help in c++, I have the code but having issues displaying the output, please and thank you
Program 3:
Write a program with several functions that performs the following tasks. :
Create a function that reads the following 5 float numbers from the file data.txt into array Called Arr1.
12.0 15.0 29.3 25.0 93.2
Copy array Arr1 into array Arr2 in reverse order
Print array Arr1.
Print array Arr2.
Find the number of elements in array Arr1 that are >= 80 and <=100.
Find the number of the elements in array Arr1 in which their contents are divisible by 5
Find the index of the elements in array Arr1 in which their contents are divisible by 3.
Find mean (average) in array Arr1.
Find the maximum number in array Arr1.
Ask the user to input a key. Then search for the key in array Arr1 and inform the user about the existence (true / false) of the key in array
My code below
#include
#include
#include
#include
using namespace std;
//function prototypes
void readArray(int A[], int size);
void copyArray(int A[], int B[], int size);
void printArray(int arr[], int size);
int findCountInRange(int arr[], int size);
int findCountDivisibleByFive(int arr[], int size);
void printIndicesDivisibleByFive(int arr[], int size);
float findAverage(int arr[], int size);
int findMaximum(int arr[], int size);
bool searchKey(int key, int arr[], int size);
int main()
{
const int MAX_SIZE = 5;//going to be reading 5 numbers
int A[MAX_SIZE];
int B[MAX_SIZE];
int key;
readArray(A, MAX_SIZE);
copyArray(A, B, MAX_SIZE);
cout << "Array Arr1: ";
printArray(A, MAX_SIZE);
cout << "Array Arr2: ";
printArray(B, MAX_SIZE);
cout << "The number of elements in array Arr1 that are >= 80 and <=100: " << findCountInRange(A, MAX_SIZE) << endl;
cout << "The number of the elements in array Arr1 in which their contents are divisible by 5: " << findCountDivisibleByFive(A, MAX_SIZE) << endl;
cout << "The index of the elements in array Arr1 in which their contents are divisible by 3: " << endl;
printIndicesDivisibleByFive(A, MAX_SIZE);
cout << "Mean (average) in array Arr1: " << findAverage(A, MAX_SIZE) << endl;
cout << "The minimum number in array Arr1: " << findMaximum(A, MAX_SIZE) << endl;
cout << "Enter a key: ";
cin >> key;
if(searchKey(key, A, MAX_SIZE))
cout << key << " is found in array Arr1." << endl;
else
cout << key << " is found in array Arr1." << endl;
cout << endl;
system("pause");
return 0;
}
// readArray function implementaion
void readArray(int A[], int size)
{
string filename = "data.txt";//reading the input file
ifstream infile;
infile.open(filename);
if(!infile)
{
cout << filename << " is not found!" << endl;
system("pause");
exit(1);
}
int n;
int count = 0;
infile >> n;
while(infile && count < size)
{
A[count] = n;
count++;
infile >> n;
}
}
// copyArray function implementaion
void copyArray(int A[], int B[], int size)
{
int j = 0;
for(int i = size - 1; i >= 0; i--)
{
B[j] = A[i];
j++;
}
}
// printArray function implementaion
void printArray(int arr[], int size)
{
for(int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
// findCountInRange function implementaion
int findCountInRange(int arr[], int size)
{
int count = 0;
for(int i = 0; i < size; i++)
{
if(arr[i] >= 80 && arr[i] <= 100)
count++;
}
return count;
}
// findCountDivisibleByFive function implementaion
int findCountDivisibleByFive(int arr[], int size)
{
int count = 0;
for(int i = 0; i < size; i++)
{
if(arr[i] % 5 == 0)
count++;
}
return count;
}
// printIndicesDivisibleByFive function implementaion
void printIndicesDivisibleByFive(int arr[], int size)
{
for(int i = 0; i < size; i++)
{
if(arr[i] % 3 == 0)
cout << i << " ";
}
cout << endl;
}
// findAverage function implementaion
float findAverage(int arr[], int size)
{
float sum = 0;
for(int i = 0; i < size; i++)
sum += arr[i];
return sum / size;
}
// findMaximum function implementaion
int findMaximum(int arr[], int size)
{
int max = arr[0];
for(int i = 1; i < size; i++)
{
if(arr[i] > max)
max = arr[i];
}
return max;
}
// searchKey function implementaion
bool searchKey(int key, int arr[], int size)
{
for(int i = 0; i < size; i++)
{
if(arr[i] == key)
return true;
}
return false;
}
Your program should include several functions.
A function for filling up the information from file into an array (part a should call this function)
A function that does the copying of one array into another in reverse order. Arrays must have the same size (part b should call this function)
Printing any array with any size (part c and d should call this function)
finding and returning the number of elements between 80 and 100 in any array with any size (part e calls this function)
finding and returning the number of elements in an array that are divisible by 5 (part f calls this function)
printing the index of elements in an array that are divisible by 3 (part g calls this function)
finding and returning the mean (average) of elements in any array with any size (part h calls this function)
finding and returning the minimum number in any array of any size (part i calls this function)
searching for a key in any array of any size and returning true/false as result (part j calls this function)
Note: If your function is supposed to return a value, then do not print anything inside the function. It would be the job of the main program to print the result on the screen. However, if the function is supposed to print information (like the print function), then you can use cout statement inside the function to print the related information.
Also, any parameter that is not supposed to be changed inside a function must be declared as constant parameter.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
