beginner c questions

M

matt

I new to programming and have started with c. I am stuck on 2 questions
and cannot move forward, any help would be greatly appreciated.

Question 1: Express each number as a floating-point constant using both
regular decimal notation and exponential notation.
a) 1,234
b) 1,234.5
c) 0.1234
d) 1.234 x 10 to the second power

Question 2: Express each number as an integer constant, an octal
constant, and a hexadecimal constant.
a) 1,234
b) 2
c) 8
d) 16
e) 1,024

Thanks in advance.
 
M

Mike Wahler

matt said:
I new to programming and have started with c. I am stuck on 2 questions
and cannot move forward, any help would be greatly appreciated.

Question 1: Express each number as a floating-point constant using both
regular decimal notation and exponential notation.
a) 1,234
b) 1,234.5
c) 0.1234
d) 1.234 x 10 to the second power

Question 2: Express each number as an integer constant, an octal
constant, and a hexadecimal constant.
a) 1,234
b) 2
c) 8
d) 16
e) 1,024

This looks too much like a verbatim homework assignment,
that I seriously doubt anyone will simply provide the
answers on a silver platter.

HOWEVER:

If you post your best attempts at your answers (better,
including your reasoning behind each), many of us will
jump in with corrections, hints, and guidance.

You do have a textbook, right?

-Mike
 
M

matt

Not a text book, just one from amazon supposed to be for beginners. I
am not attending a class, this is not homework. I just need a push in
the right direction, something to get started with and work backwards
from that point.
 
K

Keith Thompson

matt said:
I new to programming and have started with c. I am stuck on 2 questions
and cannot move forward, any help would be greatly appreciated.

Question 1: Express each number as a floating-point constant using both
regular decimal notation and exponential notation.
a) 1,234
b) 1,234.5
c) 0.1234
d) 1.234 x 10 to the second power

Question 2: Express each number as an integer constant, an octal
constant, and a hexadecimal constant.
a) 1,234
b) 2
c) 8
d) 16
e) 1,024

These are easy questions; what difficulty are you having with them?

Your C textbook should have a section on the various forms of numeric
literals, including explanations of what they all mean.

To get you started, here are some examples of each form:

Floating-point constant in regular decimal notation:
345.6
Floating-point constant in exponential notation:
3.456e2 (same value)

Integer constant (presumably this means decimal):
678
Octal constant:
01246 (same value)
Hexadecimal constant:
0x2a6 (same value)

We're glad to help out, but we're not just going to give you the
answers.
 
M

Mike Wahler

matt said:
Not a text book, just one from amazon supposed to be for beginners.

If you tell us which book and author, we can help assess
whether it's a quality book. Be warned, there are far
more absoutely incorrect books on C than there are correct
ones.

In either case, what does your books say about expressing
constant values? Every textbook I've seen (good or bad)
that has such exercises precedes them with material necessary
to complete them.

I
am not attending a class, this is not homework.

OK I'll just have to take your word for that. But
I'm still not going to provide the answers. (Nor
any help until I see evidence that you've actually
tried to work out solutions yourself).
I just need a push in
the right direction,

OK here's a 'push': Look up 'constant', or 'numeric
constant' in your book. I suspect those or similar
terms appear in its index and/or table of contents.
Also look for other terms which appear in the exercise
such as 'decimal', 'octal', 'floating point' etc.
something to get started with and work backwards
from that point.

Virtually every (especially beginner) textbook I've ever
read was the most useful when starting at the beginning
and working *forward*. Start on page 1.

-Mike
 
M

Martin Ambuhl

matt said:
I new to programming and have started with c. I am stuck on 2 questions
and cannot move forward, any help would be greatly appreciated.

Question 1: Express each number as a floating-point constant using both
regular decimal notation and exponential notation.
a) 1,234
b) 1,234.5
c) 0.1234
d) 1.234 x 10 to the second power

Question 2: Express each number as an integer constant, an octal
constant, and a hexadecimal constant.
a) 1,234
b) 2
c) 8
d) 16
e) 1,024

#include <stdio.h>

inline void Q1(double x)
{
printf("fixed: %f; exponential: %e\n", x, x);
}
inline void Q2(unsigned x)
{
printf("decimal: %d; octal: %#o; hex: %#x\n", x, x, x);
}
int main(void)
{
Q1(1234);
Q1(1234.5);
Q1(0.1234);
Q1(1.234e2);
Q2(1234);
Q2(2);
Q2(8);
Q2(16);
Q2(1024);
return 0;
}

fixed: 1234.000000; exponential: 1.234000e+03
fixed: 1234.500000; exponential: 1.234500e+03
fixed: 0.123400; exponential: 1.234000e-01
fixed: 123.400000; exponential: 1.234000e+02
decimal: 1234; octal: 02322; hex: 0x4d2
decimal: 2; octal: 02; hex: 0x2
decimal: 8; octal: 010; hex: 0x8
decimal: 16; octal: 020; hex: 0x10
decimal: 1024; octal: 02000; hex: 0x400
Thanks in advance.

Save your bill collector cant for your job as a bill collector. Bite me.
 
M

matt

So for the first question:

a)1.234 x 10 to the 3rd
b)1.2345 x 10 to the 3rd
c)?
d)123.4

As far as octal and hexadecimal im definitly lost, is there a formula?
 
M

Mike Wahler

matt said:
So for the first question:

a)1.234 x 10 to the 3rd
b)1.2345 x 10 to the 3rd
c)?
d)123.4

As far as octal and hexadecimal im definitly lost, is there a formula?

No. The exercise is about syntax.

Read your book. You need to express these constants
in C, not English. Read your book. It's in there.
Really. (Unless it's a very very poor book). Which
book is it, btw?

-Mike
 
M

matt

If I cant even express it in english, no way I can do it in any other
language. Maybe I wasnt clear on what I was asking in the first place
or maybe comp.lang.c is the wrong place to ask such questions.
 
M

Mike Wahler

matt said:
C and UNIX: Tools for Software Design
Martin L. Barrett
Clifford H. Wagner

Ah, as I thought, not an appropriate book for a beginner.
See this:
http://www.accu.informika.ru/accu/bookreviews/public/reviews/c/c000246.htm

Then peruse:
http://www.accu.org/bookreviews/public/reviews/0sb/beginner_s_c.htm

Based upon your first message, look for books that are
recommended for people with no programming experience.
(although many, including myself, wouldn't necessarily
recommend C as a first language).

Also I'm sure others will offer their suggestions and
opinions about beginner C books. Watch for further
messages.

-Mike
 
M

Mike Wahler

matt said:
If I cant even express it in english, no way I can do it in any other
language.

The exercise (as I understand it) asked for C syntax.

If you're having trouble with arithmetical concepts, perhaps you should
start with that before programming.
Maybe I wasnt clear on what I was asking in the first place

I think it was clear. You posted questions from your book.
I would be astounded if the knowledge needed to answer them
were not in the same chapter as they are. However, as I
note in my other post, I believe that book is not a good
one for the beginner.
or maybe comp.lang.c is the wrong place to ask such questions.

Not at all. Any level questions are welcome. But
'gimme the answer' posts are not.

-Mike
 
K

Keith Thompson

matt said:
Not a text book, just one from amazon supposed to be for beginners. I
am not attending a class, this is not homework. I just need a push in
the right direction, something to get started with and work backwards
from that point.

The first thing you need to know is how to post properly. You need to
provide some context (quoted text and attributions) in each followup,
so each article can be read on its own. Don't assume we can easily
see the article to which you're replying.

This followup is an example of what it should look like.

Google Groups, unfortunately, makes this gratuitously difficult.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
M

matt

If I cant even express it in english, no way I can do it in any other
The exercise (as I understand it) asked for C syntax.

I just read the chapter again and if they are not asking for it in
english I need a new book. There is no reference anywhere in this
chapter for converting a decimal to exponential, octal or hex. The only
thing I get from this chapter is printing formatted text to the screen.
In which case I would need to print strings printf(""); in english(...I
think).

If you're having trouble with arithmetical concepts, perhaps you should
start with that before programming.

I have limited experence with binary and no experence with octal or hex
but have googled and think ive have a grip.
I think it was clear. You posted questions from your book.
I would be astounded if the knowledge needed to answer them
were not in the same chapter as they are. However, as I
note in my other post, I believe that book is not a good
one for the beginner.

Could you recommend a book?
Not at all. Any level questions are welcome. But
'gimme the answer' posts are not.
Please dont give me the answers, anyone.
 
M

Mike Wahler

matt said:
I just read the chapter again and if they are not asking for it in
english I need a new book. There is no reference anywhere in this
chapter for converting a decimal to exponential, octal or hex. The only
thing I get from this chapter is printing formatted text to the screen.
In which case I would need to print strings printf(""); in english(...I
think).



I have limited experence with binary and no experence with octal or hex
but have googled and think ive have a grip.

Good. Make sure you do understand at least binary and hex,
octal isn't used quite so much (but should come easily once
you understand the general concepts of number bases). Good
exercises to solidify that knowledge is to practice converting
among those number bases (2 (binary), 8 (octal), 10 (decimal),
and 16 (hexadecimal).)

Could you recommend a book?

You should read the reviews at www.accu.org for which I gave
the link in another message in this thread. Surprisingly to
me, the book I'd recommend isn't listed there: Kernighan
and Ritchie, "The C Programming Language" Second Edition.
It's the one I learned with, oh so many eons ago. :)
Also don't feel you should restrict yourself to a single
book. Often seeing the perspectives of different authors
about the same subject can help greatly.


-Mike
 
M

matt

You should read the reviews at www.accu.org for which I gave
the link in another message in this thread. Surprisingly to
me, the book I'd recommend isn't listed there: Kernighan
and Ritchie, "The C Programming Language" Second Edition.
It's the one I learned with, oh so many eons ago. :)
Also don't feel you should restrict yourself to a single
book. Often seeing the perspectives of different authors
about the same subject can help greatly.


-Mike

I actually have Kernighan and Ritchie, "The C Programming Language"
Second Edition. I started with it but I felt it was asking me to write
c without teaching it first. As an example in the first chapter it asks
you to print EOF, it doesnt even explain how anywhere so how can I move
forward. I purchased the book I have now from me girlfriends college
figuring since they sell it in the school it would be helpful. I really
dont know where to go next, I do have php programming experence but am
not finding it helpful with these questions at all.
 
M

Mike Wahler

matt said:
I actually have Kernighan and Ritchie, "The C Programming Language"
Second Edition.

Good. Keep it.
I started with it but I felt it was asking me to write
c without teaching it first.

It 'teaches as it goes'. But I must admit it is
quite concise. But all the info is there.
As an example in the first chapter it asks
you to print EOF, it doesnt even explain how anywhere

You need to 'put the pieces together'. EOF is explained on
page 16.

How to print an integer is explained on page 11 (also see
page 13).

(page numbers might not be exact if your book doesn't have
same printing date as mine, but should be close).

Were you successful with the "Hello world" program?
so how can I move
forward.

Go slowly. Read and reread, carefully. Experiment.

Anyway, here's how to print the value of EOF:

#include <stdio.h>

int main()
{
printf("%d\n", EOF);
return 0;
}
I purchased the book I have now from me girlfriends college
figuring since they sell it in the school it would be helpful.

You should probably hang on to it. It could be useful later.
I really
dont know where to go next, I do have php programming experence but am
not finding it helpful with these questions at all.

Forget about PHP when learning C. It's enough different from C that it will
probably only cause confusion.

-Mike
 
K

Keith Thompson

matt said:
I actually have Kernighan and Ritchie, "The C Programming Language"
Second Edition. I started with it but I felt it was asking me to write
c without teaching it first. As an example in the first chapter it asks
you to print EOF, it doesnt even explain how anywhere so how can I move
forward. I purchased the book I have now from me girlfriends college
figuring since they sell it in the school it would be helpful. I really
dont know where to go next, I do have php programming experence but am
not finding it helpful with these questions at all.

Matt, thanks for quoting the previous article, but please don't delete
the attribution line (the one that indicates who wrote what).

That's exercise 1-7, "Write a program to print the value of EOF",
page 17. On page 16, it says "EOF is an integer defined in <stdio.h>".
The book shows you how to print integer values on page 11.
 
S

Sandeep

matt said:
I new to programming and have started with c. I am stuck on 2 questions
and cannot move forward, any help would be greatly appreciated.

Question 1: Express each number as a floating-point constant using both
regular decimal notation and exponential notation.
a) 1,234
b) 1,234.5
c) 0.1234
d) 1.234 x 10 to the second power

Question 2: Express each number as an integer constant, an octal
constant, and a hexadecimal constant.
a) 1,234
b) 2
c) 8
d) 16
e) 1,024

Note that these are not just "programming" questions. These are more
related to the concepts of computer science and how numbers are
expressed. So , if you are only looking into programming books for the
answer, I would suggest open some Computer System
Organization/Architecture Book. These questions would be answered
better there. Apart from knowing "how" you will also know "why" in each
of the examples above.

Before coding for a machine, it would be nice if you are familiar with
"language" it understands :)
 
O

osmium

matt said:
As far as octal and hexadecimal im definitly lost, is there a formula?

Note that octal and hexadecimal are really just a convenient shorthand way
of representing binary numbers. It is easy to mentally convert binary
from/to octal or from/to hex, less easy to do that for decimal.

If someone gives you a number and says it is decimal 235, he really means
2*10^2 + 3*10^1 + 5* 10^0.

If he says it is octal, he means 2*8^2 + 3*8^1 + 5*8^0. It is *not* two
hundred and thirty-five! It is 235.

Similarly for hex: 2*16^2 ....

In hexadecimal we run out of ways of expressing the base digits so we use
0,1,...9ABCDEF to represent what we are talking about.

This may help, but I rather doubt it. Do some address pruning and look for
something better on this site (Wikipedia)

http://en.wikipedia.org/wiki/Bases_and_number_systems
 

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
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top