+ 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

27th Mar 2021, 5:30 PM
GAFAR IBRAHIM OLASUNKANMI
GAFAR IBRAHIM OLASUNKANMI - avatar
15 Respostas
+ 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
27th Mar 2021, 5:44 PM
KrOW
KrOW - avatar
+ 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 ; }
28th Mar 2021, 8:39 PM
Haider Ali Waris
Haider Ali Waris - avatar
+ 3
Where is your try? Post it so we can discuss on it...
27th Mar 2021, 5:34 PM
KrOW
KrOW - avatar
+ 3
int min(int first, int second) { return first<second ? first : second; }
27th Mar 2021, 5:39 PM
Kashyap Kumar
Kashyap Kumar - avatar
+ 3
int min(int a, int b){ if(a<b){ return a; } else{ return b; } }
28th Mar 2021, 10:30 AM
Parth Shendge
+ 2
int main(first, second){ if first>second; return second; else return first }
27th Mar 2021, 5:37 PM
GAFAR IBRAHIM OLASUNKANMI
GAFAR IBRAHIM OLASUNKANMI - avatar
+ 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.
28th Mar 2021, 9:33 AM
Maher Al Dayekh
Maher Al Dayekh - avatar
+ 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.
27th Mar 2021, 5:51 PM
Maher Al Dayekh
Maher Al Dayekh - avatar
+ 1
same as Haider Ali Waris int min(int first, int second) { return (first<second ? first:second); } I think best the above codeâ˜ș
29th Mar 2021, 12:27 PM
Isabella
Isabella - avatar
+ 1
https://code.sololearn.com/chOMyCsIKmBc/?ref=app Check this code it's the whole code as per your requirement
29th Mar 2021, 2:17 PM
Anjali Shaw
Anjali Shaw - avatar
12th Apr 2021, 4:36 PM
R.P.
R.P. - avatar
0
Thanks Isabella 😍
29th Mar 2021, 2:21 PM
Haider Ali Waris
Haider Ali Waris - avatar
0
int min(int a, int b) {return a > b ? b : a;} // Hope this helps
29th Mar 2021, 4:51 PM
Calvin Thomas
Calvin Thomas - avatar