Please write the code in C++.
Question:
Please write the code in C++.
void InputNumbers(float *numbers, int count); void DisplayAll(float *numbers, int count); void DetermineTopFive(float *numbers, float *topFive, int count); void DetermineBottomfive(float *numbers, float *BottomFive, int count); void DisplayTopFive(float *topFive); void DisplayBottomFive(float *bottomFive); void InputNumbers(float *numbers, int count) { //write codes here } void DisplayAll(float *numbers, int count); { //write codes here } void DetermineTopFive(float *numbers, float *topFive, int count); { //write codes here } void DetermineBottomfive(float *numbers, float *BottomFive, int count); { //write codes here } void DisplayTopFive(float *topFive); { //write codes here } void DisplayBottomFive(float *bottomFive); { //write codes here }
Summary:
In this question, we need to write the code where they have asked to write the code in c++.
Explanation:
To write the code in c++ we must know about the pointers.
Pointers:
A pointer is a variable that contains the memory location of another variable or an array element. To declare a pointer, we use the ‘*’ symbol. Pointer provides access to a variable by using the address of that variable. The general syntax for declaring pointer is
datatype *var_name;
The rules for pointer operations are:
1) A pointer variable can be assigned the address of another variable.
2) It can be assigned the value of another pointer variable.
3) It can be initialized with a NULL value.
4) Prefix or postfix, increment and decrement operators can be applied on the pointer variable.
5) An integer value can be added or subracted from a pointer variable.
6) Two pointer variables can be compared using relational operators.
7) It cannot be multipied by a constant.
8) It cannot be added to another pointer variable.
Applications of pointers:
1) To pass information back and forth between a function and its reference point.
2) Enable the programmers to return multiple data items from a function via function arguments.
3) Provide an alternate way to access individual elements of the array.
4) To pass arrays and strings as function arguments.
In this code, we use call by reference approach to pass the float array. And then perform operations on it. In incall by reference method, the actual variable is passed. Any changes made also reflect on the actual variable.
- Initially, to determine the top five elements in an array, we sort them in descending order.
- And then print the top five elements.
- Similarly, to determine the bottom five elements in an array, we sort them in ascending order.
- Then print the top five elements.
- After the array address is passed, we copy the values of the array in another array.
- After that perform the sorting operations, so as not to affect the actual sequence of the data.
Source code:
#include<iostream> using namespace std; //function prototypes void InputNumbers(float *numbers, int count); void DisplayALL(float *numbers, int count); void DetermineTopFive(float *numbers, float *topfive, int count); void DetermineBottomFive(float *numbers, float *bottomfive, int count); void DisplayTopFive(float *topfive); void DisplayBottomFive(float *bottomfive); //main function int main() { int c=0; cout<<"\nEnter number of elements: "; cin>>c; float arr[c],toparr[c],botarr[c]; //function calls InputNumbers(arr,c); DisplayALL(arr,c); DetermineTopFive(arr,toparr,c); DetermineBottomFive(arr,botarr,c); DisplayTopFive(toparr); DisplayBottomFive(botarr); return 0; } //end of main //function definitions void InputNumbers(float *numbers, int count) { cout<<"\nEnter "<<count<<" values: \n"; for(int i=0; i<count; i++) cin>>numbers[i]; } void DisplayALL(float *numbers, int count) { cout<<"\nEntered numbers are: "; for(int i=0; i<count; i++) cout<<numbers[i]<<","; } void DetermineTopFive(float *numbers, float *topfive, int count) { //copying elements from numbers to topfive for(int i=0; i<count; i++) topfive[i]=numbers[i]; //now, we sort the array topfive in descending order int temp; for(int i=0; i<count; i++) for(int j=i+1; j<count; j++) { if(topfive[i]<topfive[j]) { temp=topfive[i]; topfive[i]=topfive[j]; topfive[j]=temp; } } } void DetermineBottomFive(float *numbers, float *bottomfive, int count) { //copying elements from numbers to bottomfive for(int i=0; i<count; i++) bottomfive[i]=numbers[i]; //now, we sort the array topfive in descending order int temp; for(int i=0; i<count; i++) for(int j=i+1; j<count; j++) { if(bottomfive[i]>bottomfive[j]) { temp=bottomfive[i]; bottomfive[i]=bottomfive[j]; bottomfive[j]=temp; } } } void DisplayTopFive(float *topfive) { cout<<"\nTop five elements are: "; for(int i=0; i<5; i++) cout<<topfive[i]<<","; } void DisplayBottomFive(float *bottomfive) { cout<<"\nBottom five elements are: "; for(int i=0; i<5; i++) cout<<bottomfive[i]<<","; }
Output:
Also, read Write a C++ program that prompts the user to enter 1000 integer values.