Help with dividing number by 2 homework

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

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

#16 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:51 AM

thankyou so much everyone .. I got the answer .. thankyou thankyou!! :)
Was This Post Helpful? 0
  • +
  • -

#17 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:53 AM

View PostAnnush, on 01 February 2013 - 12:46 PM, said:

for that I guess I have to use loop..


Exactly...also you will notice that the value n is not being used here.

So, something like this would be fine...

void half(int a)
{
int b;//not required here

while(a>0)
   {
    //print a 
   //do the calculation of a=a/2;
   }
}


regards,
Raghav

This post has been edited by raghav.naganathan: 01 February 2013 - 01:11 AM

Was This Post Helpful? 1
  • +
  • -

#18 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:58 AM

Well, one more thing you notice here, the program will not work effectively for odd numbers...you might want to look into that as well.

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#19 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 - 01:01 AM

aryt.. I got the answer .. thankyuh awl...!!
Was This Post Helpful? 0
  • +
  • -

#20 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 - 01:13 AM

You would notice that you will get the output something like this.

100
50
25
12
6
3
1


but, 25/2=12.5 and not 12, so the effectiveness of the program suffers here.

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#21 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 - 01:16 AM

I want the output like this

100
50
25
12
6
3
1

that's y em taking an int value..
Was This Post Helpful? 0
  • +
  • -

#22 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 - 01:25 AM

Ok...I understand. Can you please post your updated code?

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#23 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 - 01:27 AM

sure...

this is the code which I used..

#include<iostream>
using namespace std;
void half(int n);
int main()
{

half(1000); //although this will do nothing with the number 1000, it will help the function to get executed by calling it.
system ("pause");
return 0;

}

void half(int n)
{
int a,;
a=100;


cout<<a<<endl;

do
{
a=a/2;
cout<<a<<endl;

}
while(a>1);

}
Was This Post Helpful? 0
  • +
  • -

#24 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 - 01:34 AM

Well, here are the corrections that I would like to suggest...also I have removed some lines which were redundant.

#include<iostream>
using namespace std;
void half(int n);//replace as half(int a);
int main()
{
half(1000);
system ("pause");
return 0;
}

void half(int n) //here too...replace as half(int a)
{
//int a,;
//a=100;
//Remove the above lines as they are unnecessary
cout<<a<<endl;
do
  {
  a=a/2;
  cout<<a<<endl;
  }
  while(a>1);
}



regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#25 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 - 01:39 AM

I cant replace half(n) by half (a)
its giving me a error..
Was This Post Helpful? 0
  • +
  • -

#26 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 - 01:41 AM

Did you replace both the half(int n) by half(int a) ?

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#27 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 - 01:44 AM

yes, I did..
Was This Post Helpful? 0
  • +
  • -

#28 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 - 01:50 AM

So,does your updated code look like this?

01	#include<iostream>
02	using namespace std;
03	void half(int a);
04	int main()
05	{
06	half(1000);
07	system ("pause");
08	return 0;
09	}
10	 
11	void half(int a)
12	{
13	//int a,;
14	//a=100;
15	//Remove the above lines as they are unnecessary
16	cout<<a<<endl;
17	do
18	  {
19	  a=a/2;
20	  cout<<a<<endl;
21	  }
22	  while(a>1);
23	}


regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#29 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 - 01:52 AM

yes...
Was This Post Helpful? 0
  • +
  • -

#30 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 - 01:53 AM

Then there should be no problem...it should work perfectly as you wanted...what are the errors you are getting?

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

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