| View previous topic :: View next topic |
| Author |
Message |
Broken Master Cheater
Reputation: 0
Joined: 12 Sep 2006 Posts: 322
|
Posted: Sun Sep 02, 2007 11:23 am Post subject: C Primer Plus : Programming Exercise |
|
|
Programming Exercises
Reading about C isn't enough. You should try writing one or two simple programs to see whether writing a program goes as smoothly as it looks in this chapter. A few suggestions follow, but you should also try to think up some problems yourself. You'll find answers to selected programming exercises on the publisher's website: www.samspublishing.com.
1: Write a program that uses one printf() call to print your first name and last name on one line, uses a second printf() call to print your first and last names on two separate lines, and uses a pair of printf() calls to print your first and last names on one line. The output should look like this (but using your name):
Anton Bruckner
First print statement
| Code: |
printf("Anton Bruckner\n");
|
Anton
Second print statement
Bruckner
Still the second print statement
| Code: |
printf("Anton \n Brucker");
|
Anton Bruckner
Third and fourth print statements
| Code: |
printf("Anton");
printf("Bruckner \n");
|
2: Write a program to print your name and address.
| Code: |
#include library.h
int main(void)
{
int address;
address = 0000;
printf("Hello, my name is Anton Bruckner\n");
printf("My address is %d, address");
return 0;
}
|
3: Write a program that converts your age in years to days and displays both values. At this point, don't worry about fractional years and leap years.
| Code: |
#include library.h
int main(void)
{
int years, days;
years = x;
days = 365.242199x;
print("I am %d years old and %d days old. \n, years, 365.242199 * years");
return 0;
}
|
4: Write a program that produces the following output:
For he's a jolly good fellow!
For he's a jolly good fellow!
For he's a jolly good fellow!
Which nobody can deny!
| Code: |
#include library.h
int main(void);
{
printf("For he's a jolly good fellow! \n")
printf("For he's a jolly good fellow! \n")
printf("For he's a jolly good fellow! \n")
printf("Which nobody can deny! \n")
return 0;
}
|
Can i know if i did anything wrong?
_________________
|
|
| Back to top |
|
 |
--Pillboi-- Grandmaster Cheater Supreme
Reputation: 0
Joined: 06 Mar 2007 Posts: 1383 Location: I don't understand the question. Is this a 1 to 10 thing?
|
Posted: Sun Sep 02, 2007 11:33 am Post subject: |
|
|
Almost all right. Well done.
To make the second one better, you could use a constant.
In the third one, I don't know why your passing the main function the parameter: void. You don't need to create the integer: days. Even if you were going to say: days*years, there shouldn't be an x at the end of the integer variable value. And to make it even better, you should have user input for how old they are.
And for the fourth one, you need /n/n at the end of each line, rather than just /n.
_________________
Enter darkness, leave the light, Here be nightmare, here be fright...
Earth and Water, Fire and Air. Prepare to meet a creature rare.
Enter now if you dare, Enter now the dragon's lair.
Last edited by --Pillboi-- on Sun Sep 02, 2007 11:42 am; edited 1 time in total |
|
| Back to top |
|
 |
Broken Master Cheater
Reputation: 0
Joined: 12 Sep 2006 Posts: 322
|
Posted: Sun Sep 02, 2007 11:42 am Post subject: |
|
|
| --Pillboi-- wrote: | Almost all right. Well done.
To make the second one better, you could use a constant.
In the third one, I don't know why your passing the main function the parameter: void.
And for the fourth one, you need /n/n at the end of each line, rather than just /n. |
2:O_O? Confused, are you saying that i should change the name and address?
3:Uh, I think the book says it would be better to write a function the traditional way.
4:Why \n\n? Isn't one \n supposed to move the cursor to the next line?
_________________
|
|
| Back to top |
|
 |
--Pillboi-- Grandmaster Cheater Supreme
Reputation: 0
Joined: 06 Mar 2007 Posts: 1383 Location: I don't understand the question. Is this a 1 to 10 thing?
|
Posted: Sun Sep 02, 2007 11:46 am Post subject: |
|
|
Edited last post.
1: If you haven't learned about constants yet, don't worry.
2: It should just be: int main()
3: Yes. But the way you've done it, it would write:
| Code: | For he's a jolly good fellow!
For he's a jolly good fellow!
For he's a jolly good fellow!
Which nobody can deny! |
This is because /n just moves it to the next line, it doesn't move it to the next line, create a blank line, and go down another line.
_________________
Enter darkness, leave the light, Here be nightmare, here be fright...
Earth and Water, Fire and Air. Prepare to meet a creature rare.
Enter now if you dare, Enter now the dragon's lair. |
|
| Back to top |
|
 |
Broken Master Cheater
Reputation: 0
Joined: 12 Sep 2006 Posts: 322
|
Posted: Sun Sep 02, 2007 11:48 am Post subject: |
|
|
| --Pillboi-- wrote: | Edited last post.
1: If you haven't learned about constants yet, don't worry.
2: It should just be: int main()
3: Yes. But the way you've done it, it would write:
| Code: | For he's a jolly good fellow!
For he's a jolly good fellow!
For he's a jolly good fellow!
Which nobody can deny! |
This is because /n just moves it to the next line, it doesn't move it to the next line, create a blank line, and go down another line. |
K, what is the void there for?
_________________
|
|
| Back to top |
|
 |
UnLmtD Grandmaster Cheater
Reputation: 0
Joined: 13 Mar 2007 Posts: 894 Location: Canada
|
Posted: Sun Sep 02, 2007 12:12 pm Post subject: |
|
|
| --Pillboi-- wrote: |
2: It should just be: int main() |
He's learning C. It is advised to use the C99 standard under which only these are acceptable: | Code: | int main (void)
int main (int argc, char *argv[]) |
_________________
|
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Sun Sep 02, 2007 1:30 pm Post subject: |
|
|
| The jolly good fellow exercise probably was supposed to be done with a loop.
|
|
| Back to top |
|
 |
Trow Grandmaster Cheater
Reputation: 2
Joined: 17 Aug 2006 Posts: 957
|
Posted: Sun Sep 02, 2007 4:11 pm Post subject: |
|
|
| appalsap wrote: | | The jolly good fellow exercise probably was supposed to be done with a loop. |
so for(..).
_________________
Get kidnapped often. |
|
| Back to top |
|
 |
Broken Master Cheater
Reputation: 0
Joined: 12 Sep 2006 Posts: 322
|
Posted: Mon Sep 03, 2007 6:05 am Post subject: |
|
|
Whats a loop? >.>
_________________
|
|
| Back to top |
|
 |
Trow Grandmaster Cheater
Reputation: 2
Joined: 17 Aug 2006 Posts: 957
|
Posted: Mon Sep 03, 2007 6:35 am Post subject: |
|
|
loop = what is done over and over again
_________________
Get kidnapped often. |
|
| Back to top |
|
 |
--Pillboi-- Grandmaster Cheater Supreme
Reputation: 0
Joined: 06 Mar 2007 Posts: 1383 Location: I don't understand the question. Is this a 1 to 10 thing?
|
Posted: Mon Sep 03, 2007 9:30 am Post subject: |
|
|
| UnLmtD wrote: | He's learning C. It is advised to use the C99 standard under which only these are acceptable: | Code: | int main (void)
int main (int argc, char *argv[]) |
|
I have no idea what you are talking about. I'm learning C, and I've been taught to only pass parameters that need to be passed. I'm probably missing something really obvious. And how can you only use those two? What if it needs 3 parameters etc?
_________________
Enter darkness, leave the light, Here be nightmare, here be fright...
Earth and Water, Fire and Air. Prepare to meet a creature rare.
Enter now if you dare, Enter now the dragon's lair. |
|
| Back to top |
|
 |
UnLmtD Grandmaster Cheater
Reputation: 0
Joined: 13 Mar 2007 Posts: 894 Location: Canada
|
Posted: Mon Sep 03, 2007 11:10 am Post subject: |
|
|
In the example that he posted, he doesn't need to pass any arguments in his main function. Therefor he used int main(void) and not in main(). Using void within the brackets is the correct way to tell the compiler that the function takes NO arguments.
int main() take an unknown number of arguments
_________________
|
|
| Back to top |
|
 |
--Pillboi-- Grandmaster Cheater Supreme
Reputation: 0
Joined: 06 Mar 2007 Posts: 1383 Location: I don't understand the question. Is this a 1 to 10 thing?
|
Posted: Tue Sep 04, 2007 11:48 am Post subject: |
|
|
I see. Now there's something tutorials don't tell you. As it compiles, does it actually make any difference, in size, run-speed etc. If so, I need to change a few programs, lol. Thanks for the info.
_________________
Enter darkness, leave the light, Here be nightmare, here be fright...
Earth and Water, Fire and Air. Prepare to meet a creature rare.
Enter now if you dare, Enter now the dragon's lair. |
|
| Back to top |
|
 |
UnLmtD Grandmaster Cheater
Reputation: 0
Joined: 13 Mar 2007 Posts: 894 Location: Canada
|
Posted: Tue Sep 04, 2007 1:04 pm Post subject: |
|
|
I don't see why it would be faster, if somehow it is, you wouldn't notice it. It's like returning 0 at the end, it's not required but it's good programming practice to always use a return statement, even if you don't have to.
_________________
|
|
| Back to top |
|
 |
--Pillboi-- Grandmaster Cheater Supreme
Reputation: 0
Joined: 06 Mar 2007 Posts: 1383 Location: I don't understand the question. Is this a 1 to 10 thing?
|
Posted: Wed Sep 05, 2007 10:35 am Post subject: |
|
|
Ok, thanks, that cleared it up.
_________________
Enter darkness, leave the light, Here be nightmare, here be fright...
Earth and Water, Fire and Air. Prepare to meet a creature rare.
Enter now if you dare, Enter now the dragon's lair. |
|
| Back to top |
|
 |
|