In: Computer Science
Write a small C++ program with 4 functions (and main():
The main function will call those methods and print the results of each.
1 // declare
necessary variables
2 // declare array
3 double numbers[SIZE];
4 // Function prototypes
5
6 int main() {
7 // call getNumbers()with appropriate parameters passed
8 // print results of getNumbers()
9 // call and print results of findMax()
10 // call and print results of findMin()
11 // call and print results of find() when
element is found
12 // call and print results of find() when
element is not found
13 return 0;
14 }
.
program in c++
#include <iostream>
using namespace std;
void getNumbers(int size);
void findMax(const double arr[], int size);
void findMin(const double arr[], int size);
void find(const double arr[], double num, int size);
const int SIZE = 100;
int i,size;
double numbers[SIZE]; //making a double type array with size
100
void getNumbers(int size) //fn to input numbers into the
array
{
cout<<"Enter the numbers :";
for(i=0;i<size;i++)
cin>>numbers[i];
}
void findMax(const double arr[], int size) //fn to find largest
number in the array
{
double max=arr[0]; //initially considering first number as the
largest
for(i=0;i<size;i++)
{
if(arr[i]>=max) /* if the traversed number greater than max then
max is stored with the current number */
max=arr[i];
}
cout<<"The largest number in the array is "<<max;
}
void findMin(const double arr[], int size) //fn to find smallest
number in the array
{
double min=arr[0]; //initially considering first number as the
smallest
for(i=0;i<size;i++)
{
if(arr[i]<=min) /* if the traversed number less than min then
min is stored with the current number */
min=arr[i];
}
cout<<"\nThe smallest number in the array is
"<<min;
}
void find(const double arr[], double num, int size) /* fn to find
the position of the number .position starts from 1 to n
as the user enter the input array */
{
int position,flag=0; //position varible to store position of
element if found
for(i=0;i<size;i++)
{
if(arr[i]==num)
{
position=i; /* if found then that array index is stored in position
and change the flag value to 1 */
flag=1;
break;
}
}
if(flag==1)
cout<<"\nThe number " <<num<<" found at position
"<<position+1;
else
cout<<"\n The number "<<num<<" not found in the
array"; /* else condition here represents flag=0,that is number not
found in array */
}
int main()
{
double num;
cout<<"Enter the size of the array:";
cin>>size;
getNumbers(size);
findMax(numbers,size);
findMin(numbers,size);
cout<<"\nEnter the number to be checked:";
cin>>num;
find(numbers,num,size);
return 0;
}
output
Enter the size of the array:5
Enter the numbers :2.3 1 0.5 6 4
The largest number in the array is 6
The smallest number in the array is 0.5
Enter the number to be checked:1
The number 1 found at position 2
Enter the size of the array:5
Enter the numbers :2.3 1 0.5 6 4
The largest number in the array is 6
The smallest number in the array is 0.5
Enter the number to be checked:8
The number 8 not found in the array
nb
if you find this useful, please rate positive ,thankyou