Hello,
Write a program that reads a sequence of numbers (at least three), The program must find out three smallest number
for example :
if we entered { 0.5 , 5 , 6 , 150 , 0.25 , 8 , 8 , 1 ,3 }
then the program should outputs { 0,25 , 0,5 , 1 }.
i wrote the following code but it does not work for all number , when i enter :
How many number in your sequence : 7
Please enter first integer :0.5
Please enter next integer :
1
Please enter next integer :8
Please enter next integer 8
Please enter next integer 8
Please enter next integer 4
Please enter next integer 0.25
The Minimum Smallest 0
The Mid Number is 1
The Max Smallest 4
how can i solve this problem ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
#include <iostream>
using namespace std ;
double Min( double , double , double );//function to calculate minimum number
double Mid( double , double , double );//function to calculate second smallest number
double Max( double , double , double );//function to calculate third smallest number
int main() {
double num1 , num2 , num3 ;
int seq;
double th3 , sec , frst ;
cout << "How many number in your sequence : " ; // here we enter how long our sequence is
cin >> seq;
cout << "Please enter first integer :" ;// first we enter three number for have first three smallest numbers
cin >> num1;
cout << "Please enter next integer :" ;
cin >> num2;
cout << "Please enter next integer :" ;
cin >> num3;
th3 = Max( num1 , num2 , num3 );
sec = Mid( num1 , num2 , num3 );
frst = Min( num1 , num2 , num3 );
// here we wanna enter further number depending on how long our sequnce is
for ( int i = 4 ; i <= seq ; i++){
int New ;
cout << "Please enter next integer " ;
cin >> New ;
if ( New < frst ){
frst = New ;
}
else if ( New > frst && New < sec ){
sec = New ;
}
else if ( New > sec && New < th3 ){
th3 = New ;
}
}
cout << "The Minimum Smallest " << frst << endl ;
cout << "The Mid Number is " << sec << endl ;
cout << "The Max Smallest " << th3 << endl ;
return 0;
}
double Min( double x , double y , double z){
double Min = x;
if(y < Min){
Min = y;}
if (z < Min){
Min = z;}
return Min;
}
double Max(double x , double y , double z){
double Max = x;
if( y > Max){
Max = y ;}
if( z > Max ){
Max = z;}
return Max;
}
double Mid(double x , double y , double z){
double Mid;
if ( y > x && y < z ){
Mid = y;
}
else if ( y < x && y > z ){
Mid = y;
}
else if ( x > y && x < z ){
Mid = x;}
else if ( x < y && x > z ){
Mid = x ;}
else if ( z > y && z < x ){
Mid = z;
}
else if ( z < y && z > x ){
Mid = z ;
}
return Mid ;
}
|