🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C++ Workshop - Week 1 (Ch. 1 & 2) - Quizzes and Extra Credit

Started by
27 comments, last by Dbproguy 16 years, 1 month ago

Quizzes and Extra Credit assignments for Week 1.

Post your answers to the Extra Credit assignment and Quizzes from Week 1 here rather than in the workshop thread, so as not to spoil things for the others.

Invisal's Extra Exercises

It is 5th day for C++ Workshop and I want to give some Extra Exercises for testing their understand and improve their programming skill. 1. Write a program that print your name on the screen. 2. Track the errors on this code then fix the errors and guess what is this code do without using the compiler. There are 13 errors in this code

#include <iostream>

int main()
{
   cout >> "There are many mistakes in this code." >> std::endl
   cout >> "Can you help me fix all the bugs in this code" >> end >> endl;
   cout >> "Thank You!" >> std::endl;
}
3. Write a program that display something like this:

*******
 *****
*******
 *****
4. Write a program that ask the age of the user and display it on the screen. For example:

How old are you? : 21
You are 21 years old
5. Write a program that ask user to insert 2 numbers and then display the summary, subtraction and multiplication between those two numbers. For example:

Please insert your first number: 5
Please insert your seond number: 2
The summary is: 7
The subtraction is: 3
The multiplication is : 10
Enjoy!

JWalsh's Quizz

Listed below are a set of quiz questions to help you test your knowledge and understanding of the material contained in chapters 1 and 2. Each chapter builds upon the knowledge obtained in previous chapters, so it can be dangerous and confusing to advance to later chapters without a complete understanding of the material already covered. If you are unable to answer these questions, please ask for assistance, but DO NOT POST THE ANSWERS IN THE MAIN WORKSHOP THREAD. Any questions which is not marked with [Extra Credit] can be answered by reading your textbook. Questions which are marked [Extra Credit] either have been answered in the thread previously, or can be answered by doing a bit of research. Chapter 1
  • What is an interpreter?
  • What is a compiler?
  • Why is compiled code faster than translated code?
  • What does structured programming (procedural programming) consist of?
  • What were the primary problems object oriented programming was designed to solve?
  • What are the three pillars of object oriented programming? Does C++ support them all?
  • What is encapsulation?
  • What is inheritance?
  • What is polymorphism?
  • Should you learn C before C++, why or why not?
  • What is the FIRST question you ask when beginning your designs?
  • What is the SECOND question you ask when beginning your designs?
  • What are the two primary steps in creating an executable? What is the output of each step?
  • If your compiler requires <iostream.h> does it follow the ANSI standard? What IS the standard?
Chapter 2
  • When you issue a command to compile your code, what is run first? What does it do?
  • What symbol indicates a preprocessor directive?
  • What does an "include" directive instruct the preprocessor to do?
  • What is the necessary function which all console C++ programs have?
  • Who calls that function?
  • Can you declare main as void? Why use int instead?
  • Are "curly braces" optional when enclosing a function?
  • What is the Standard Library?
  • How do you instruct the compiler that you want to use part of the Standard Library?
  • [Extra Credit] What's the difference between "\n" and endl?
  • What are the 3 ways to use a class in a namespace? Use "cout" as an example.
  • What are the two types of comments?
  • [Extra Credit] What is self-documenting code? Does that remove the need for comments?
  • Are the 3 components of a function declaration (function header)?
  • What do you call a function that is part of a class?
  • [Extra Credit]What does a compiler do with "white space?"
[Edited by - Fruny on June 12, 2006 1:03:44 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
1: Is there anything wrong with the following code? Give your reasoning.
int function(){}int main(){}


2: Is there anything wrong with the following code? Give your reasoning.
int function(){  return 10;}int main(){}


3: Is there anything wrong with the following code? Give your reasoning.
int main(int variable){  return variable;}int main(){  return main(10);}


4: Is there anything wrong with the following code? Give your reasoning.
int function(int variable){  return variable;}int function(){  return function(10);}int main(){  return function();}


5: Is there anything wrong with the following code? Give your reasoning.
int main(){  return function(10);}int function(int variable){  return variable;}



6: Is there anything wrong with the following code? Give your reasoning.

int function();int main(){  return function(10);}int function(int variable){  return variable;}


7: Is there anything wrong with the following code? Give your reasoning.

int function(int);int main(){  return function(10);}int function(int variable){  return variable;}


8: Is there anything wrong with the following code? Give your reasoning.

int Function(int);int main(){  return Function(10);}int function(int variable){  return variable;}


9: Is there anything wrong with the following code? Give your reasoning.

int function(int);int main(){  return Function(10);}int Function(int variable){  return variable;}


10: Why does one of the two previous examples (8 and 9) produce a compiler error when the other produces a linker error?

Good Luck.
hi,

the compiler gives me an error when compiling my code :

#include <iostream>void func1 (void){	std::cout << "*******";}void func2 (void){	std::cout << " ***** ";}int main(){	func1();	func2();	func1();	func2();	return 0;}


The errormessage is :

Error	1	error C2039: 'exit' : is not a member of '`global namespace''	f:\program files\microsoft visual studio 8\vc\include\cstdlib	23Error	2	error C2873: 'exit' : symbol cannot be used in a using-declaration	f:\program files\microsoft visual studio 8\vc\include\cstdlib	23


don´t no what the problem is but I don´t think it´s in my programm.

amoK



EDIT :

when I comment this piece of code in the cstdlib.h header it works :

#ifndef RC_INVOKED #if _GLOBAL_USING_STD_BEGINusing ::size_t; using ::div_t; using ::ldiv_t;using ::abort; using ::abs; using ::atexit;using ::atof; using ::atoi; using ::atol;using ::bsearch; using ::calloc; using ::div;//using ::exit; using ::free; using ::getenv;using ::labs; using ::ldiv; using ::malloc;using ::mblen; using ::mbstowcs; using ::mbtowc;using ::qsort; using ::rand; using ::realloc;using ::srand; using ::strtod; using ::strtol;using ::strtoul; using ::system;using ::wcstombs; using ::wctomb;_STD_END


I commented the line with the using::exit directive and it works, but I think its not so good to start changing in the header files.


amoK
ACK!! Change that back!! Never modify the Standard Library. Doing so guarantees long term repercussions which may or may not be immediately apparent.

In this case, your problem is not in the C Standard Library, your problem is the way it's being included. Normally the <cstdlib> is included via <xlocnum> as a natural part of including <iostream>. But for some reason for you 'exit' is either being undefined or re-defined in such a way that the Standard Library is complaining when it encounters it.

I created a project as per the instructions at the top of the Week 1 post, then I created a file called Test.cpp and copied/pasted your code into the file.

...it compiled and ran fine.

So your problem lies outside the scope of that code. Some questions:

Is that ALL that's in that file?
Do you have ANY other files in your project?
How did you create the project/solution?

Answer those questions for me and then I might have you copy/paste the contents of your .vcproj file so I can see the project settings.

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Quote: Original post by jwalsh
ACK!! Change that back!! Never modify the Standard Library. Doing so guarantees long term repercussions which may or may not be immediately apparent.

In this case, your problem is not in the C Standard Library, your problem is that you should not be including the <cstdlib> to begin with. So the bigger question is WHY is it including it. I created a project as per the instructions at the top of the Week 1 post, then I created a file called Test.cpp and copied/pasted your code into the file.

...it compiled and ran fine.

So your problem lies outside the scope of that code. Some questions:

Is that ALL that's in that file?
Do you have ANY other files in your project?
How did you create the project/solution?

Answer those questions for me and then I might have you copy/paste the contents of your .vcproj file so I can see the project settings.

Cheers!


cstdlib seems to be included by the iostream header file in the Visual C++ Express standard library implementation.

The exit function which is giving the compiler error is declared in the stdlib.h header file, which should be included by the cstdlib header file.

When I comment out the exit declarations in stdlib.h I get the same compiler errors.

I would make sure that you have not commented them out to fix another error you may have had Am0k. They should be on lines 406 and 407 of stdlib.h. If they are then I would be inclined to think that you had set up your project wrong as well.
String, yeah...I noticed the same thing after my post, I updated my post and then noticed your post.
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
First to answer frunys questions:

1. Write a program that print your name on the screen.
#include <iostream>using namespace std;int main(){        cout << "Name: alex" << endl;        return 0;}


2. Track the errors on this code then fix the errors and guess what is this code do without using the compiler. There are 13 errors in this code
#include <iostream>using namespace std;int main(){        cout << "There are many mistakes in this code." << endl;        cout << "Can you help me fix all the bugs in this code" << endl;        cout << "Thank you!" << endl;        return 0;}


3. Write a program that display something like this:
#include <iostream>using namespace std;int main(){        cout << "*******\n *****\n*******\n *****" << endl;        return 0;}


4. Write a program that ask the age of the user and display it on the screen.
#include <iostream>using namespace std;int main(){        int age;        cout << "How old are you? : ";        cin >> age;        cout << "You are " << age << " years old" << endl;        return 0;}


5. Write a program that ask user to insert 2 numbers and then display the summary, subtraction and multiplication between those two numbers.
#include <iostream>using namespace std;int main(){        int x, y;        cout << "Please enter your first number: ";        cin >> x;        cout << "Please enter your second number: ";        cin >> y;        cout << "The summary is: " << x+y << endl;        cout << "The subtraction is: " << x-y << endl;        cout << "The multiplication is: " << x*y << endl;        return 0;}


Could have added functions for the arithmetic but wrote these answers quick :P

Answers to strings questions:

1: Both functions are missing a return statement.
2: Main missing a return statement.
However both the above should still compile.

3. You cant overload the main function.
4. Nothing wrong here, functions can be overloaded so long as their parameters are different.
5. Needs to be a function prototype if the function is declared after the main function.
6. Prototypes parameters do not match the function signature.
7. Nothing wrong here.
8. Prototype is miss spelled (I.e should be lower case f).
9. Like above, prototype does not match with function signature.
10. 9 Will produce a compiler error because the function declared by the prototype cannot be found (out of scope). While 8 will not be able to link with the function declared by the prototype.

(I dont currently have the sams c++ book, using an old C++ programming language by Stroustrup, book coming next week).

Answers to jwalsh's questions:

Chapter 1
What is an interpreter?
An interpreter is program that interprets code and executes it.

What is a compiler?
A compiler is a program that takes source code files and translates them into machine code (object code), which can be executed at a later time.

Why is compiled code faster than translated code?
Compiled code is faster than translated code because the compiler can optimise the code.

What does structured programming (procedural programming) consist of?
Procedural programming consists of writing procedures (functions, methods etc..) to give a more modular approach to programming.

What were the primary problems object oriented programming was designed to solve?
Object orientated programming was developed to give greater flexibility and make it easier to maintain complexed programs.

What are the three pillars of object oriented programming? Does C++ support them all?
Encapsulation, Inheritance and Polymotphism are the three pillars of OO programming. C++ supports them all.

What is encapsulation?
Encapsulation is data hiding, and providing certain access methods for objects.

What is inheritance?
Inheritance is an object can inherit functionality from whatever it has derived.

What is polymorphism?
This means an object can take many forms.

Should you learn C before C++, why or why not?
(Is this not just personnel opinion?) I think you should not learn C before C++ because it would be harder to get into the OO style of thinking required.

// No idea about these two, could be anything (don't have Sams book :P)
What is the FIRST question you ask when beginning your designs?
What is the SECOND question you ask when beginning your designs?

What are the two primary steps in creating an executable? What is the output of each step?
Compiling and Linking are the two main steps. The output of the compilation step is object code. After it has been linked, the output is an executable binary.

If your compiler requires <iostream.h> does it follow the ANSI standard? What IS the standard?
No it does not follow ANSI standard, the standard is just as #include <iostream>.

Chapter 2
When you issue a command to compile your code, what is run first? What does it do?
A lexical analyser is run first on the source code. This tokenises and outputs a series of symbols(tokens).

What symbol indicates a preprocessor directive?
# Indicates a preprocessor directive.

What does an "include" directive instruct the preprocessor to do?
An include instructs the preprocessor to include a separate file into the code.

What is the necessary function which all console C++ programs have?
A main() method.

Who calls that function?
The system calls the main function, this marks the entry point to the program.

Can you declare main as void? Why use int instead?
Because int is used to check to see if that function has executes correctly. (I.E 0 = no error)

Are "curly braces" optional when enclosing a function?
No.

What is the Standard Library?
The standard library is a set of functions, algorithms, classes etc.. that have been standardised and included in a library.

How do you instruct the compiler that you want to use part of the Standard Library?
To use part of the standard library you must include to relevant header files.

[Extra Credit] What's the difference between "\n" and endl?
(Guessing :-) \n creates a new line but keeps buffer open. endl flushes the buffer.

What are the 3 ways to use a class in a namespace? Use "cout" as an example.
std::cout << “test”;
Put, using std::cout; at top of source.
Put, using namespace std; at the top then use cout << “test”; etc as normal.

What are the two types of comments?
The two types of comments are // and /* */

[Extra Credit] What is self-documenting code? Does that remove the need for comments?
Self documenting code is code that reads as clearly/easily as possible. This does not however remove the needs for comments. Complicated algorithms/functions/hacks should always be commented.

Are the 3 components of a function declaration (function header)?
Yes return type, identifier, and parameters.

What do you call a function that is part of a class?
A member function.

[Extra Credit]What does a compiler do with "white space?"
Uh, the white space is handled by the lexical parser I guess.

[Edited by - alexjp on June 12, 2006 3:00:40 AM]
hi,

thanks for the fast replays.

I have deleted the comments now and I am getting the same errors.
That is all what is in the file nad there are no other files in my project.

I created an empty project, so I don´t know were the error can be there.

amoK
am0K,

As string asked, had you ever modifed your Standard Library before? That is, had you commented out ANY previous lines of code in a Visual C++ header file?

Can you open your .vcproj file in Wordpad or notepad and copy/paste the contents into a source tag here. That'll help us evaluate whether the project was set up correctly.

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
1. Write a program that print your name on the screen.
#include <iostream>
int main()
{
std::cout << "Gambler" <<std::endl;
return 0;
}
2. Track the errors on this code then fix the errors and guess what is this code do without using the compiler. There are (15) 13 errors in this code

#include <iostream>
int main()
{
1(std::)cout 2(<<)>> "There are many mistakes in this code." 3(<<)>>
std::endl 4(;)
5(std::)cout 6(<<)>> "Can you help me fix all the bugs in this code"
7(<<)>> 8(std::)end 9(l) 10(<<)>> 11(std::)endl;
12(std::)cout 13(<<)>> "Thank You!" 14(<<)>> std::endl;
15(return 0;)
}


3. Write a program that display something like this:
*******
*****
*******
*****

#include <iostream>
int main()
{
std::cout << "*******" << std::endl;
std::cout << " ***** " << std::endl;
std::cout << "*******" << std::endl;
std::cout << " ***** " << std::endl;
return 0;
}

4. Write a program that ask the age of the user and display it on the screen.

For example:
How old are you? : 21
You are 21 years old

#include <iostream>
int main()
{
int age;
std::cout << "How old are you? ";
std::cin >> age;
std::cout << "You are " << age << " years old....man that's old!" <<
std::endl;
return 0;
}


5. Write a program that ask user to insert 2 numbers and then display the summary, subtraction and multiplication between those two numbers.

For example:
Please insert your first number: 5
Please insert your seond number: 2
The summary is: 7
The subtraction is: 3
The multiplication is : 10

#include <iostream>
int main()
{
int first, second;
std::cout << "Please insert your first number: ";
std::cin >> first;
std::cout << "Please insert your second number: ";
std::cin >> second;
std::cout << "The summary is: " << first + second << std::endl;
std::cout << "The subtraction is: " << first - second << std::endl;
std::cout << "The multiplication is: " << first * second << std::endl;
return 0;
}

Enjoy!

JWalsh's Quizz

Chapter 1
What is an interpreter?
An Interpreter translates and executes a program as it reads it
What is a compiler?
A compiler translates code into an intermediary form producing an object file and then an executable program
Why is compiled code faster than translated code?
No translation is necessary to run the code
What does structured programming (procedural programming) consist of?
A set of tasks (procedures)
What were the primary problems object oriented programming was designed to solve?
Grouping data with associated functions, and reuse
What are the three pillars of object oriented programming? Does C++ support them all?
Encapsulation, inheritance, and polymorphism
What is encapsulation?
The property of being a self-contained unit
What is inheritance?
Ability to create a new type which is an extension of the old type
What is polymorphism?
The ability of a function to take various forms
Should you learn C before C++, why or why not?
The book says that C can teach bad habits, who really knows?
What is the FIRST question you ask when beginning your designs?
“What is the problem I am trying to solve?”
What is the SECOND question you ask when beginning your designs?
“Who is going to pay me for this?” or alternatively “Can this be accomplished without resorting to writing custom software?”
What are the two primary steps in creating an executable? What is the output of each step?
Creating an object file and then an executable
If your compiler requires <iostream.h> does it follow the ANSI standard? What IS the standard?
No, American National Standards Institute.

Chapter 2
When you issue a command to compile your code, what is run first? What does it do?
Preprocessor
What symbol indicates a preprocessor directive?
#
What does an "include" directive instruct the preprocessor to do?
Find the file name that follows, read it, and put it right here.
What is the necessary function which all console C++ programs have?
Main()
Who calls that function?
No one – it is called automatically
Can you declare main as void? Why use int instead?
You can, but it is illegal.
Are "curly braces" optional when enclosing a function?
No
What is the Standard Library?
A Collection of classes that comes with every ANSI compliant compiler
How do you instruct the compiler that you want to use part of the Standard Library?
“std::”
[Extra Credit] What's the difference between "\n" and endl?
\n starts a new line, endl starts a new line and “flushes the buffer to the screen”
What are the 3 ways to use a class in a namespace? Use "cout" as an example.
Std::cout, using std::cout, using namespace std
What are the two types of comments?
/* + */ and //
[Extra Credit] What is self-documenting code? Does that remove the need for comments?
Clear naming and code layout, and no, you still need comments, but to say “why” you are doing something, not “what”
Are the 3 components of a function declaration (function header)?
Return type, function name and parameters
What do you call a function that is part of a class?
method
[Extra Credit]What does a compiler do with "white space?"
Nothing

This topic is closed to new replies.

Advertisement