33 Replies - 1399 Views - Last Post: 01 February 2013 - 02:06 AM
#1
Help with dividing number by 2 homework
Posted 31 January 2013 - 04:08 PM
Call the function half() with an argument of 100, the screen output should be
100
50
25
...
...
1
please suggest meh the correct coding for this
I have tried it with for loop, do-while loop, while loop, if statement still em not getting the answer.
Replies To: Help with dividing number by 2 homework
#2
Re: Help with dividing number by 2 homework
Posted 31 January 2013 - 04:12 PM
And you should revise your post-title; everything posted in this forum should be a question.. and the title is meant to help others find useful information.
This post has been edited by andrewsw: 31 January 2013 - 04:17 PM
#3
Re: Help with dividing number by 2 homework
Posted 31 January 2013 - 04:14 PM
You can find examples of recursive functions all over the place, but don't expect people to do your work for you. That isn't what this community is about. Normally the best way to get help is to post the code that you have tried (so people know you have put forth an effort) in the
#4
Re: Help with dividing number by 2 homework
Posted 31 January 2013 - 04:19 PM
coding..
#include<iostream>
using namespace std;
int half(int n);
int main()
{
int num1,x;
num1=100;
x=(num1/2);
cout<<num1<<endl;
cout<<x<<endl;
system ("pause");
return 0;
}
int half(int n)
{
int x;
if(x>0)
{
cout<<x<<endl;
}
else if(x<=0)
{
cout<<"exit"<<endl;
}
return x;
}
reply meh plzz
Mod edit - Please
#5
Re: Help with dividing number by 2 homework
Posted 31 January 2013 - 04:33 PM
Okay, so you have a function that does something, now you know you need to divide the input number by 2, so lets start by you dividing the number by two and outputting it instead. Once you have that code completed (and posted in
#6
Re: Help with dividing number by 2 homework
Posted 31 January 2013 - 04:36 PM
my out is just coming till 100 nd 50 em not getting rest of the numbers..
#7
Re: Help with dividing number by 2 homework
Posted 31 January 2013 - 04:45 PM
int main()
{
/*Call the function half() with an argument of 100*/
system("pause");
return 0;
}
void half(int n) //made it void because you don't need to return anything.
{
/*The function must print the number it received to the screen*/
/*then the program should divide that number by two to make a new number*/
if(/*the new number is greater than zero*/)
{
/*the function then calls the function half() passing it the new number as its argument*/
}
else //the number is zero or less
{
/*the function exits*/
}
}
#8
Re: Help with dividing number by 2 homework
Posted 31 January 2013 - 04:53 PM
#include<iostream>
using namespace std;
void half(int n);
int main()
{
system ("pause");
return 0;
}
void half(int n)
{
int a,b;
a=100;
b=a/2;
if(a>0)
{
cout<<a<<endl;
}
else if(a<=0)
{
cout<<"exits"<<endl;
}
}
This post has been edited by Skydiver: 31 January 2013 - 08:46 PM
Reason for edit:: Added code tags.
#9
Re: Help with dividing number by 2 homework
Posted 31 January 2013 - 08:46 PM
#10
Re: Help with dividing number by 2 homework
Posted 31 January 2013 - 09:01 PM
I'm trying but, I'm not able to get it ..
#11
Re: Help with dividing number by 2 homework
Posted 31 January 2013 - 09:28 PM
Jim
#12
Re: Help with dividing number by 2 homework
Posted 31 January 2013 - 11:00 PM
calling function is something like this.
void some_function( int something) //function definition
{
cout<<"Hi...I am present in function definition and being called from main()";
}
int main()
{
some_function(3); //Calling function
}
regards,
Raghav
This post has been edited by raghav.naganathan: 31 January 2013 - 11:01 PM
#13
Re: Help with dividing number by 2 homework
Posted 31 January 2013 - 11:29 PM
#14
Re: Help with dividing number by 2 homework
Posted 01 February 2013 - 12:07 AM
#include<iostream>
02 using namespace std;
03 void half(int n);
04 int main()
05 {
06 half(1000); //although this will do nothing with the number 1000, it will help the function to get executed by calling it.
07 system ("pause");
08 return 0;
09 }
10
11 void half(int n)
12 {
13 int a,b;
14 a=100;
15 b=a/2;
16
17 if(a>0)
18 {
19 cout<<a<<endl;
20 }
21 else if(a<=0)
22 {
23 cout<<"exits"<<endl;
24 }
25 }
regards,
Raghav
#15
Re: Help with dividing number by 2 homework
Posted 01 February 2013 - 12:16 AM
but, I want my output to be like this
100
50
25
...
...
1
and for that I guess I have to use loop..
|
|

New Topic/Question
Reply



MultiQuote





|