Question

In: Computer Science

Write a small C++ program with 4 functions (and main(): getNumbers()- what is the return, what...

Write a small C++ program with 4 functions (and main():

  • getNumbers()- what is the return, what are the parameters?
  • findMax()- what is the return, what are the parameters?
  • findMin()-what is the return, what are the parameters?
  • find()- should return the index of the element or a -1 indicating not found

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 }

  1. NOTE: output should be user friendly, eg, for find(), the code should not print a cryptic 3 or -1, the message should explain the results.
  2. No magic numbers.
  3. Use const array parameter where appropriate.

.

Solutions

Expert Solution

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


Related Solutions

In C++ Prototype your functions above "main" and define them below "main"; Write a program that...
In C++ Prototype your functions above "main" and define them below "main"; Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other arrays. It should also keep count...
Write a C++ program which consists of several functions besides the main() function. The main() function,...
Write a C++ program which consists of several functions besides the main() function. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. A value-returning function called Power(int a, int b) that...
Write a program in c++, with at least four functions, including main, which must do the...
Write a program in c++, with at least four functions, including main, which must do the following: Ask user whether they want to encode or decode a message – if no, then terminate Take the input string from the user, store it in dynamic memory (use new) As appropriate, encode or decode the message using Rot13. Output the encoded/decoded message Delete the input string from dynamic memory (use delete) Input will be a string of no more than 25 characters....
Write an IPO diagram and Python program that has two functions, main and determine_grade. main –...
Write an IPO diagram and Python program that has two functions, main and determine_grade. main – Should accept input of five numeric grades from the user USING A LOOP.   It should then calculate the average numeric grade.    The numeric average should be passed to the determine_grade function. determine_grade – should display the letter grade to the user based on the numeric average:        Greater than 90: A 80-89:                 B 70-79:                 C 60-69:              D Below 60:           F Modularity:...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
Write the code in C++. Write a program to implement Employee Directory. The main purpose of...
Write the code in C++. Write a program to implement Employee Directory. The main purpose of the class is to get the data, store it into a file, perform some operations and display data. For the purpose mentioned above, you should write a template class Directory for storing the data empID(template), empFirstName(string), empLastName(string), empContactNumber(string) and empAddress(string) of each Employee in a file EmployeeDirectory.txt. 1. Write a function Add to write the above mentioned contact details of the employee into EmployeeDirectory.txt....
C++ in one program that ends with return 0} at the end f Write a program...
C++ in one program that ends with return 0} at the end f Write a program to calculate the salary paid to its salespersons at Pohanka. The salary is calculated based on a salesperson’s length of employment in years and employment category (full-time employee or part-time employee). The salary calculation rules are as following: 1) If an employee is a part-time employee and worked here for less than 5 years, the salary consists of only the commission amount; 2) If...
Write a C program with call to functions to produce the output given below. // the...
Write a C program with call to functions to produce the output given below. // the requirements are that there should be 5 files; intList.h, intList.c, hw3.h, hw3.c, and main.c. please only C and use Linked List. thank you. For the 5 different files, he wants it this way: 1) main.c This file just consists of the main() function, which only consists of the displayClassInfo() function call, and the runMenuHw3() function call. 2) intList.h This file would have the IntNode...
Language Python with functions and one main function Write a program that converts a color image...
Language Python with functions and one main function Write a program that converts a color image to grayscale. The user supplies the name of a file containing a GIF or PPM image, and the program loads the image and displays the file. At the click of the mouse, the program converts the image to grayscale. The user is then prompted for a file name to store the grayscale image in.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT