Help!!!

R

reepakmajhi.com

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)
 
G

Giff

Sam said:
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.
 
O

osmium

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

reepakmajhi.com

osmium said:
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");
}
 
S

sean_in_raleigh

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
 
L

LR

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
 
L

Lionel B

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
 
O

osmium

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");
}
 
L

Lionel B

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.


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?
 
J

James Kanze

Ok, that doesn't compile on my system.

  #include <iostream>
iostream.h possibly doesn't (and probably shouldn't) exist.

It definitely should exist, for reasons of backward
compatibility. Many implementations, however, provide a
version of it which isn't backward compatible. And you may need
special compiler flags to get at it.

What is certain is that someone learning C++ today shouldn't be
using it.
      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.
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
?

Better yet, what is the definition of an odd number? And is
there an operator which could give the required information. (I
wouldn't use division.)
However you do it, "odd" is surely not the right name for what
you want to print out.

Yes and no. His output looks pretty odd to me.
Not sure why you want this... but if you do you probably have
to "#include <cstdlib>" or "#include stdlib.h" before main()

I've never used it either, but it would seem that some IDE's
don't know how to handle command line programs correctly. (At
least if they're not configured correctly. I've heard that you
can configure them so the behave correctly, but I've never
followed up on it.)
 
L

Lionel B

It definitely should exist, for reasons of backward compatibility. Many
implementations, however, provide a version of it which isn't backward
compatible. And you may need special compiler flags to get at it.

Hm. My compiler (gcc 4.3.2) won't pick it up (with -std=c++98 -pedantic).
In fact it doesn't appear to exist in the source or installation tree for
gcc 4.3.2. What does the standard say?
What is certain is that someone learning C++ today shouldn't be using
it.
Sure.

[...]
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 ?

Better yet, what is the definition of an odd number? And is there an
operator which could give the required information. (I wouldn't use
division.)

I wouldn't either; but I tend to think of division as a more "elementary"
operation than modulus (and perhaps more likely to be familiar to the OP).

[...]
 
J

James Kanze

Hm. My compiler (gcc 4.3.2) won't pick it up (with -std=c++98
-pedantic). In fact it doesn't appear to exist in the source
or installation tree for gcc 4.3.2. What does the standard
say?

It's not a question of the standard. It's a question of
supporting existing code. A compiler would be quite
irresponsible not to provide it, although as I said, it may
require special options to get it. (And I wouldn't expect to
get it if I specifically request 100% standard compatibility.)
Regretfully, too, many compilers only provide a "roughly
compatible" version, which still breaks some existing code.
(This was the case with both g++ and Sun CC before I made my
transition to the standard libraries.)

All of which isn't really that important in this context. As I
said:

It's there (or should be there) to support legacy code, and
shouldn't be used except in legacy code; typically, the vendor
is not actively maintaining it, for example.
Sure.
[...]
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 ?
Better yet, what is the definition of an odd number? And is
there an operator which could give the required information.
(I wouldn't use division.)
I wouldn't either; but I tend to think of division as a more
"elementary" operation than modulus (and perhaps more likely
to be familiar to the OP).

I'd think it hard to understand integral division if you don't
also know modulus. But it's hard for me to remember what it's
like to be a beginner; all too far in the past.
 
L

Lionel B

It's not a question of the standard. It's a question of supporting
existing code. A compiler would be quite irresponsible not to provide
it, although as I said, it may require special options to get it. (And
I wouldn't expect to get it if I specifically request 100% standard
compatibility.)

I guess that makes GCC "irresponsible" then; it seems that they have
indeed removed a screed of backward-compatibility and deprecated headers
in the 4.3 series:

http://gcc.gnu.org/gcc-4.3/porting_to.html

There are also other changes there that are bound to break some legacy
code. I suppose that ultimately there will always be a limit to the extent
that a compiler vendor feels obliged to maintain backward compatibility or
support deprecated features (perhaps this limit is likely to be lower for
a FOSS compiler than a proprietory one...).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,167
Messages
2,570,910
Members
47,453
Latest member
MadelinePh

Latest Threads

Top