+ 2
Write a c++ function min() that has two integer parameters first,and second and it returns the smallest of the two.
Please help me to solve this!!! I
13 Respuestas
+ 4
1) You have to create a function "min" and not main
2) You have to use parentesis in construct like if as:
if(condition){
statements
} else {
other statements
}
3) in c++ you have to declare variable types in functions parameters also like
int min(int first, int second)....
Sincerally i see much very basic errors then i suggest to read your learning resource
+ 4
#include<iostream>
using namespace std ;
int min(int first , int second )
{
return (a<b ? a:b ) ;
}
int main()
{
int a, b ;
int s_no ; // small_number
cout << "Enter two number " ;
cin >> a>> b ;
s_no = min(a,b) ;
cout << "Small number = " << s_no ;
return 0 ;
}
+ 3
Where is your try? Post it so we can discuss on it...
+ 3
int min(int first, int second)
{
return first<second ? first : second;
}
+ 3
int min(int a, int b){
if(a<b){
return a;
}
else{
return b;
}
}
+ 2
int main(first, second){
if first>second;
return second;
else
return first
}
+ 2
Farnaz.A `cin >> a,b >> endl` ? This is so wrong. There's no comma & endl in std::cin. Plus, don't take input from functions, but rather do Min(int a, int b){...}
Then after main:
int x, y;
std::cin >> x >> y;
Min(x,y);
And, you can make ternary return statement, e.g `return a > b ? a : b;`. Check Martin Taylor 's code about them.
+ 1
This example is good to tell people why they should avoid <bits/stdc++.h> & using namespace std;
Well because bits will call the <algorithm> library & namespace will call `std::min` thus resulting in an error.
+ 1
same as Haider Ali Waris
int min(int first, int second) {
return (first<second ? first:second);
}
I think best the above code☺
+ 1
https://code.sololearn.com/chOMyCsIKmBc/?ref=app
Check this code it's the whole code as per your requirement
+ 1
Check this out:
https://www.programiz.com/cpp-programming/ternary-operator
0
Thanks Isabella 😍
0
int min(int a, int b) {return a > b ? b : a;}
// Hope this helps