Using Visual Studio Professional 2013 C++. Write a program with three user funct
ID: 3812579 • Letter: U
Question
Using Visual Studio Professional 2013 C++.
Write a program with three user functions. The main function will just define and initialize the variables and call the user functions. All the work will be done by the user functions. In the main function, define a double array of 47 elements. Initialize the array to -99.99. Call Function #1. Call Function #4. Prompt the user to enter a number between 0.0 and 10.0 and a delta value (~ 0.2). Read the values. Call Function #2. In the main function, print the search results. Call Function #3. Call Function #4. Helps to check search results.
Function #1: This function will be passed the array and its size and return nothing. Fill the array with random numbers between 0.0 and 10.0 (inclusive).
Function #2: This function will be passed the array, its size, the value to search for and the delta value. The function will return the number of values in the array within the specified range – value ± delta value (inclusive).
Function #3: This function will be passed the array and its size and return nothing. Use bubble sort to sort the array.
Function #4: This function will be passed the array and its size and return nothing. Print the array 8 elements across.
Explanation / Answer
Answer
#include <iostream>
#include <cstdlib> /* srand, rand */
#include <iomanip> // std::setprecision
using namespace std;
void fun1(double arr[],int size)
{
double x1=0.0,x2=10.0;
const long max_rand = 1000000L;
for(int i=0;i<size;i++)
{
arr[i]=x1+(x2-x1)*(random()%max_rand)/max_rand;
}
}
int fun2(double arr[],int size,double val,double delVal)
{
int count=0;
for(int i=1;i<=size;i++)
{
if((arr[i-1]>=(val-delVal))&&(arr[i-1]<=(val+delVal)))
count++;
}
return count;
}
void fun3(double arr[],int size)
{
for(int i=1;i<=size;i++)
{
for(int j=1;j<=size;j++)
{
if(arr[i-1]<arr[j-1])
{
double temp=arr[i-1];
arr[i-1]=arr[j-1];
arr[j-1]=temp;
}
}
}
}
void fun4(double arr[],int size)
{
cout<<setprecision(2);
for(int i=1;i<=size;i++)
{
cout<<arr[i-1]<<" ";
if(i%8==0)
cout<<endl;
}
}
int main()
{
double arr[47],val,delVal;
srand(time(NULL));
for(int i=0;i<47;i++)
{
arr[i]=-99.99;
}
cout<<" ------FUNCTION 1 CALL-------- ";
fun1(arr,47);
cout<<" ------FUNCTION 4 CALL-------- ";
fun4(arr,47);
cout<<" Enter a number between 0.0 and 10.0 : ";
cin>>val;
cout<<" Enter delta value (~ 0.2) : ";
cin>>delVal;
cout<<" ------FUNCTION 2 CALL-------- ";
int count=fun2(arr,47,val,delVal);
cout<<" number of values in the array within the specified range – value ± delta value (inclusive) : "<<count;
cout<<" ------FUNCTION 3 CALL-------- ";
fun3(arr,47);
cout<<" ------FUNCTION 4 CALL-------- ";
fun4(arr,47);
cout<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.