[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Help!!!

reepakmajhi.com@gmail.com

11/14/2008 11:27:00 AM

Write a C++ program to input a number. If the number n is odd and
positive,print its square root
otherwise print pow(n,5)
17 Answers

Sam

11/14/2008 12:04:00 PM

0

reepakmajhi.com@gmail.com writes:

> Write a C++ program to input a number. If the number n is odd and
> positive,print its square root
> otherwise print pow(n,5)

Nobody here is going to help you with your homework assignment. Figure it
out by yourself.


anon

11/14/2008 12:32:00 PM

0

Sam wrote:
> reepakmajhi.com@gmail.com writes:
>
>> Write a C++ program to input a number. If the number n is odd and
>> positive,print its square root
>> otherwise print pow(n,5)
>
> Nobody here is going to help you with your homework assignment. Figure
> it out by yourself.
>
>

Thats not true. I can help.
Here you go:
http://www.parashift.com/c++-faq-lite/how-to-post.ht...

Giff

11/14/2008 1:00:00 PM

0

Sam wrote:

> Nobody here is going to help you with your homework assignment. Figure
> it out by yourself.

Especially after he posted the same question yesterday, and actually
received some useful hints.

osmium

11/14/2008 3:47:00 PM

0

<reepakmajhi.com@gmail.com> wrote:

> Write a C++ program to input a number. If the number n is odd and
> positive,print its square root
> otherwise print pow(n,5)

There are five or six sub-problems there. I think you should post some code
to show that you are at least trying and also to show where your problems
are. Unless you do post some code, I predict you will get no further help.


reepakmajhi.com@gmail.com

11/14/2008 3:51:00 PM

0



osmium wrote:
> <reepakmajhi.com@gmail.com> wrote:
>
> > Write a C++ program to input a number. If the number n is odd and
> > positive,print its square root
> > otherwise print pow(n,5)
>
> There are five or six sub-problems there. I think you should post some code
> to show that you are at least trying and also to show where your problems
> are. Unless you do post some code, I predict you will get no further help.

The following i have tried

#include<iostream.h>
#include<math.h>
int main()
{
int number,odd;
float n;
cout<<"enter number";
cin>>number;
odd=(n=number/2)? sqrt(number):pow(number,5);
cout<<odd;
system("pause");
}

sean_in_raleigh

11/14/2008 4:18:00 PM

0

On Nov 14, 10:51 am, "reepakmajhi....@gmail.com"
<reepakmajhi....@gmail.com> wrote:
> The following i have tried
>
> #include<iostream.h>
> #include<math.h>
> int main()
> {
> int number,odd;
> float n;
> cout<<"enter number";
> cin>>number;
> odd=(n=number/2)? sqrt(number):pow(number,5);
> cout<<odd;
> system("pause");
> }

A useful technique is to remove all of those
lines in main() and add them back one at a time
(starting at the top), and compile and run your
program after each new line is added. That way you
can figure out which line is causing you a
problem.

Figuring this stuff out on your own is part
of building up the mental scar tissue you'll
need to work with the horrible software that
programmers have to use.

Sean

LR

11/14/2008 4:36:00 PM

0

reepakmajhi.com@gmail.com wrote:
>
> osmium wrote:
>> <reepakmajhi.com@gmail.com> wrote:
>>
>>> Write a C++ program to input a number. If the number n is odd and
>>> positive,print its square root
>>> otherwise print pow(n,5)
>> There are five or six sub-problems there. I think you should post some code
>> to show that you are at least trying and also to show where your problems
>> are. Unless you do post some code, I predict you will get no further help.
>
> The following i have tried
>
> #include<iostream.h>
> #include<math.h>
> int main()
> {
> int number,odd;
> float n;
> cout<<"enter number";
> cin>>number;
> odd=(n=number/2)? sqrt(number):pow(number,5);
> cout<<odd;
> system("pause");
> }

What specific problems are you seeing with this code when you try to
either compile it or run it?

Have you put in any trace to see what values things are?

What do you expect (n=number/2) to do?

I suggest you try this, although I haven't tested this or even tried to
compile it myself.

int main() {
for(int i=0; i<10; i++) {
const double n = i/2;
const bool test = n ? true : false;
std::cout << i << " " << n << " " << test << std::endl;
}
}

LR


jt

11/14/2008 4:46:00 PM

0

On Fri, 14 Nov 2008 07:51:21 -0800, reepakmajhi.com@gmail.com wrote:

> osmium wrote:
>> <reepakmajhi.com@gmail.com> wrote:
>>
>> > Write a C++ program to input a number. If the number n is odd and
>> > positive,print its square root
>> > otherwise print pow(n,5)
>>
>> There are five or six sub-problems there. I think you should post some
>> code to show that you are at least trying and also to show where your
>> problems are. Unless you do post some code, I predict you will get no
>> further help.
>
> The following i have tried

Ok, that doesn't compile on my system.

> #include<iostream.h>

#include <iostream>

iostream.h possibly doesn't (and probably shouldn't) exist.

> #include<math.h>
>
> int main()
> {
> int number,odd;
> float n;
> cout<<"enter number";

std::cout<<"enter number";

The iostream stuff is all in "namespace std" (read up about namespaces if
you're not familiar with them). Same for cin.

> cin>>number;
> odd=(n=number/2)? sqrt(number):pow(number,5);

Hmmm... think of a simple test for oddness (or for evenness). Hint: what
do you get when you divide an odd "int" value by 2 ?

> cout<<odd;

However you do it, "odd" is surely not the right name for what you want to
print out.

> system("pause");

Not sure why you want this... but if you do you probably have to "#include
<cstdlib>" or "#include stdlib.h" before main()

> }

--
Lionel B

osmium

11/14/2008 5:13:00 PM

0

<reepakmajhi.com@gmail.com> wrote:

> osmium wrote:
>> <reepakmajhi.com@gmail.com> wrote:
>>
>> > Write a C++ program to input a number. If the number n is odd and
>> > positive,print its square root
>> > otherwise print pow(n,5)
>>
>> There are five or six sub-problems there. I think you should post some
>> code
>> to show that you are at least trying and also to show where your problems
>> are. Unless you do post some code, I predict you will get no further
>> help.
>
> The following i have tried
>
> #include<iostream.h>
> #include<math.h>
> int main()
> {
> int number,odd;
> float n;
> cout<<"enter number";
> cin>>number;
> odd=(n=number/2)? sqrt(number):pow(number,5);

You're trying to do too much in one statement. Crawl, walk, and then run is
a good idea. Note that odd is an integer, but that square roots are, in
general, decimal numbers. So odd can't contain a meaningful square root,
can it? But odd is all you print.

The assignment says "... n is odd ...". But in the code you use n for a
float. Oddness and evenness are qualities of integers, not decimals. Don't
use n to mean two things in the same exercise.

> cout<<odd;
> system("pause");
> }

I would write it something like this.
-----
#include <iostream>
#include <cmath>
#include <cstdlib>

int main()
{
using namespace std;
while(1)
{
int n;
cin >> n;
if((n%2) > 0)
cout << sqrt(n) << endl;
else if(n > 0)
cout << pow(n, 5.0) << endl;
else
cout << "n was zero or negative\n";
}
system("pause");
}
------
Some people who have way too much time on their hands will bitch about using
the "using" statement and that the user might type in something like
"apple" and the program will respond badly. To them I say: For God's sake
man, get a life!



jt

11/14/2008 5:23:00 PM

0

On Fri, 14 Nov 2008 11:12:58 -0600, osmium wrote:

> <reepakmajhi.com@gmail.com> wrote:
>
>> osmium wrote:
>>> <reepakmajhi.com@gmail.com> wrote:
>>>
>>> > Write a C++ program to input a number. If the number n is odd and
>>> > positive,print its square root
>>> > otherwise print pow(n,5)
>>>
>>> There are five or six sub-problems there. I think you should post
>>> some code
>>> to show that you are at least trying and also to show where your
>>> problems are. Unless you do post some code, I predict you will get no
>>> further help.
>>
>> The following i have tried
>>
>> #include<iostream.h>
>> #include<math.h>
>> int main()
>> {
>> int number,odd;
>> float n;
>> cout<<"enter number";
>> cin>>number;
>> odd=(n=number/2)? sqrt(number):pow(number,5);
>
> You're trying to do too much in one statement. Crawl, walk, and then
> run is a good idea. Note that odd is an integer, but that square roots
> are, in general, decimal numbers. So odd can't contain a meaningful
> square root, can it? But odd is all you print.
>
> The assignment says "... n is odd ...". But in the code you use n for a
> float. Oddness and evenness are qualities of integers, not decimals.
> Don't use n to mean two things in the same exercise.
>
>> cout<<odd;
>> system("pause");
>> }
>
> I would write it something like this. -----
> #include <iostream>
> #include <cmath>
> #include <cstdlib>
>
> int main()
> {
> using namespace std;
> while(1)
> {
> int n;
> cin >> n;
> if((n%2) > 0)
> cout << sqrt(n) << endl;
> else if(n > 0)
> cout << pow(n, 5.0) << endl;
> else
> cout << "n was zero or negative\n";
> }
> system("pause");
> }
> ------
> Some people who have way too much time on their hands will bitch about
> using the "using" statement and that the user might type in something
> like "apple" and the program will respond badly. To them I say: For
> God's sake man, get a life!

Others may say: For God's sake man, you've just given away the answer!
How's anyone going to learn anything without thinking for themself?

--
Lionel B