Need help

10. Write a function called smaller, with two integer parameters,that returns the smaller of the two values passed to it. For example, if the call is smaller(3,7) the function would return 3. If the numbers are equal, return any.

Please help.
What is your exact problem with this assignment? What are you having trouble with?


Or if you are just lazy: http://en.cppreference.com/w/cpp/algorithm/min
Thanks for your quick response Nico. Actually I'm a self-motivated beginner. We were not taught how to use std::min

I can write a larger function but not for a smaller which is why I need help with this, please.
This is what I tried to put together;

int compareTwo(int x, int y)
{
return smaller (x, y)
}

Any help?
There is a button next to the textfield where you type with "<>". If you click that you will get "code-tags" that enable you to post your source code in a more readable way. Please try to use them whenever you post some code.

I hope you are aware that you did not try to implement a function called smaller, you implemented a function called compareTwo that calls a (undefined) function called smaller.
Also, there is no difference between writing a smaller or a larger function. At least there should not be. A function is just a sequence of commands that you want the program to execute which you expect you will use more often. The number of commands in the sequence doesn't matter (except for the source-code-readers comfort).

It is important to realize that a if a function returns, the rest of the code will not be executed anymore. For example, here line 4 will never be executed, because the function call returns in line 3.
1
2
3
4
5
int fourtytwo()
{
      return 42;
      std::cout << " this will never be printed in the console because this line can not ever be executed." ;
}


Do you know if-statements?
If not, read this: http://www.cprogramming.com/tutorial/lesson2.html
Your smaller function should take two arguments (for example x and y), then you should use an if-statement to check if the first is smaller that the other. If this if-statement is true, you return the first value.
If the if-statement is not true, you return the second value.

I don't understand why you want to call a function called smaller in your function compareTwo, since the compareTwo function does nothing else you could easily put your if-statement in there instead of the call to another function.
1
2
3
4
int compareTwo(int x, int y)
{
      return (x < y) ? x : y; // return the result of this shorthand if-statement.
}


or, to prevent the shorthand from making it difficult to understand:
1
2
3
4
5
int compareTwo(int x, int y)
{
      if (x < y) return x;
      else return y;
}
Last edited on
That is awesome, Nico. Thank you very much :) . Yes, we have been taught if ...else and do while functions.
So is it safe to say that this is similar to


1
2
3
4
5
6
7
Int smaller (int x, int y )
{ 
	If (x >= y)
	Return x;
	else return y;
}
[code]
[/code] ?

My next question is this: 13. Write a function called even that when called returns true if the parameter is a n even integer or 0, and false otherwise. Remember that returning is NOT printing. Make sure the return type is of the proper data type.
Please help.

You should be careful with your capitals and check your math. Your current function should be called "larger".

As for your next question, your smaller function returned an integer because you declare a function as:
ReturnType functionName(parameters)
so
int functionName(parameters)
indicates that the function will return an integer. Now your next function should return something else then an integer, what would be a suitable type?


https://www.tutorialspoint.com/cplusplus/cpp_variable_types.htm
Last edited on
Hi Nico, I was typing in ms word that's why it changed to capitals.


So per your question, my next function should return bool so it can return true or false. Thanks for the link. :)

Should the program look like this? Please help.

#include <iostream>
using namespace std;


1
2
3
4
5
6
7
8
bool even (x, y)
{ 
if  (x %2 = 0)
return x

 
else return y
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;

int smaller(int,int);

int main()
{
 int num=0,num1=0,store=0;
 cin>>num>>num1;
 store=smaller(num,num1);
 cout<<endl;
 cout<<store;
    
}

int smaller(int a,int b)
{
 if(a<b)
    return a;
 else
    return b;
    
}
Smaller:
1
2
3
4
int smaller(int x = 0, int y = 0) // return 0 when no arguments provided
{
    return x < y ? x : y;
}

Even:
1
2
3
4
bool even(int x = 1) // return false when no arguments provided
{
    return x % 2 == 0 ? true : false;
}
Last edited on
Hello,
Should the program look like this?
1
2
3
4
5
6
bool even (x, y)
{ 
     if  (x %2 = 0)
         return x
     else return y
}


Not exactly, boost lexical cast has already provided a proper implementation, but allow me to point at the issues here.

You try to create a function with arguments x and y, but you don't tell the program what kind of variables x and y are. Your compiler will object to that, giving you an error message. You should really read those and try to solve them yourself before you post your code here.

Now we know that you are working with integers and booleans. You can not divide a boolean by 2, so x has to be an integer. a function that should check if a value is even or not does not need a second parameter, so we can just remove the y argument.

Subsequently you try to compare two things using the equals sign, this happens to a lot of people and you have to be very careful with it. If you use a single equals sign you are assigning a value, so in your case you are telling the program that from that moment onwards, the result of x%2 is 0 (regardless of what x was). If you want to compare two things you need to use double equals signs.

After you check if x can be divided equally by 2 your function should return a boolean as you mentioned in you reply. We have just determined that x must be an integer, s you need to return something else as x. In this case you can use the boolean constants "true" and " false" like boost lexical cast did in his example.

This gives you:
1
2
3
4
5
6
bool even (int x)
{ 
     if  (x %2 == 0)
         return true;
     else return false;
}


But you could be a bit more direct, because your if-statement already uses a boolean expression. You could just return the result of that boolean expression.
This gives you:
1
2
3
4
bool even (int x)
{ 
     return  (x %2 == 0);
}

Last edited on
I'm a self-motivated beginner
that's great and good luck!

May I also take this opportunity to introduce you then to the wonderful world of bitwise operators: http://www.cprogramming.com/tutorial/bitwise_operators.html
The bitwise and operator (&) can also be used to check if a number is even or odd:
1
2
3
4
void evenORodd (int x)
{
     cout <<((x & 1)?  "Odd" : "Even" )<< '\n';
}
Topic archived. No new replies allowed.