Help with dividing number by 2 homework

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 2191 Views - Last Post: 01 February 2013 - 02:06 AM Rate Topic: -----

#1 Annush  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 31-January 13

Help with dividing number by 2 homework

Posted 31 January 2013 - 04:08 PM

Make a function called half() that takes an integer argument. 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. If the number is zero or less than the function exits

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with dividing number by 2 homework

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3485
  • View blog
  • Posts: 11,891
  • Joined: 12-December 12

Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 04:12 PM

You should show your code. If not, it essentially means that you are just asking us to do this for you.

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

Was This Post Helpful? 0
  • +
  • -

#3 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,149
  • Joined: 07-September 06

Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 04:14 PM

It is most likely supposed to be a recursive function. Which I am assuming you have gone over in class one or two days this week.

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 :code: tags.
Was This Post Helpful? 0
  • +
  • -

#4 Annush  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 31-January 13

Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 04:19 PM

here is the coding..

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 :code:
Was This Post Helpful? 0
  • +
  • -

#5 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,149
  • Joined: 07-September 06

Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 04:33 PM

Updated title.

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 :code: ) we can move on to making the function do something more meaningful.
Was This Post Helpful? 0
  • +
  • -

#6 Annush  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 31-January 13

Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 04:36 PM

at the above I had divided it by 2 but, still em not getting the answer why??
my out is just coming till 100 nd 50 em not getting rest of the numbers..
Was This Post Helpful? 0
  • +
  • -

#7 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 771
  • Joined: 27-June 09

Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 04:45 PM

The assignment spells out step by step what you need to code. All you really have to do is translate each step into code.

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*/
	}
}

Was This Post Helpful? 1
  • +
  • -

#8 Annush  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 31-January 13

Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 04:53 PM

I did it this way but, now I'm not geting anything in my output

#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.

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,125
  • Joined: 05-May 12

Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 08:46 PM

It usually helps if you call your function at least once.
Was This Post Helpful? 0
  • +
  • -

#10 Annush  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 31-January 13

Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 09:01 PM

so, how should I call the function..?
I'm trying but, I'm not able to get it ..
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,571
  • Joined: 25-December 09

Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 09:28 PM

I suggest you start by reading the function tutorials contained in my signature. They should help you better understand functions. This link may also be of some use:Functions I.

Jim
Was This Post Helpful? 0
  • +
  • -

#12 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 11:00 PM

Well, you need to call your function in the main

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

Was This Post Helpful? 1
  • +
  • -

#13 Annush  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 31-January 13

Re: Help with dividing number by 2 homework

Posted 31 January 2013 - 11:29 PM

I'm seriously not getting the answer :S
Was This Post Helpful? 0
  • +
  • -

#14 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Help with dividing number by 2 homework

Posted 01 February 2013 - 12:07 AM

Well, if I would do the same in your program, it would be something like this.

#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
Was This Post Helpful? 1
  • +
  • -

#15 Annush  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 31-January 13

Re: Help with dividing number by 2 homework

Posted 01 February 2013 - 12:16 AM

by this I'm only getting my output 100

but, I want my output to be like this

100
50
25
...
...
1

and for that I guess I have to use loop..
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3