Please in C++ Write a program that accepts the users name (at least 15 character
ID: 3920036 • Letter: P
Question
Please in C++
Write a program that accepts the users name (at least 15 characters), then accepts 5 integer and 5 float values. After all the data has been entered, the program must then display the user’s name and the median values for both the integers and the floats.
Project Requirements:
Must have a “programmed by …” inline function, that works.
Use a template function to get integer and float data.
No global variables, pass information in.
Use an overloaded function to display the name, integer and float arrays
Hints:
The median is the middle number in a sorted list. if the list has an even number of elements then it is the average of the two numbers straddling the middle. Remember, the median is not the average of a list of numbers
Overloaded functions have the same name but different parameters
Also remember that character arrays always end with “” the null character, so don’t forget a space for that character….however, strings don’t have to worry about that.
Watch your limits, if you over-run your array it will crash your computer.
If you want to, you can pass pointers and use them in your swap function.
You can also use a class if you want.
Explanation / Answer
#include<iostream>
#include <algorithm>
using namespace std;
//template function
template <class T>
void read_input(T a[],int n)
{
int i=0;
while(i<n)
{
cin>>a[i];//read numbers...
i++;
}
}
//method to display values...
template <class T>
void Display(T a[],int n)
{
int i=0;
while(i<n)
{
cout<<a[i]<<" ";
i++;
}
cout<<" ";
}
//method to find median of floats
void median(float a[],int n)
{
sort(a,a+n);///method sort numbers
//finding median
if(n%2==0)//if n is even
{
int m = n/2;
float median = (a[m-1]+a[m])/2;
cout<<median<<endl;
}
else
{
int m = n/2;
cout<<a[m]<<endl;
}
}
//overloading
//method to find median of integers
void median(int a[],int n)
{
sort(a,a+n);///method sort numbers
//finding median
if(n%2==0)//if n is even
{
int m = n/2;
float median = (a[m-1]+a[m])/2;
cout<<median<<endl;
}
else
{
int m = n/2;
cout<<a[m]<<endl;
}
}
int main()
{
string u;
int a[5];
float b[5];
//reading user name
cout<<"Enter user name:";
cin>>u;
//calling function to read ints and floats
cout<<"Enter 5 integers:";
read_input(a,5);
cout<<"Enter 5 floats:";
read_input(b,5);
cout<<"User name:"<<u<<endl;
cout<<"Integers:";
Display(a,5);
cout<<"Floats:";
Display(b,5);
//finding median////
cout<<"Median of Integers:";
median(a,5);
cout<<"Median of floats:";
median(b,5);
return 0;
}
output:
Enter user name:surya
Enter 5 integers:2 3 1 4 5
Enter 5 floats:1.1 3.2 2.3 5.1 4
User name:surya
Integers:2 3 1 4 5
Floats:1.1 3.2 2.3 5.1 4
Median of Integers:3
Median of floats:3.2
Process exited normally.
Press any key to continue . . .