Sign in to follow this  

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

This topic is 3498 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 23

Error 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_BEGIN
using ::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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
hi,

I have not changed something in my standard library before.
This is my vcproj file :


<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="8,00"
Name="c++Workshop"
ProjectGUID="{90627FF6-BF7C-4548-924E-D7AD2B70BBCC}"
RootNamespace="cWorkshop"
Keyword="Win32Proj"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
Detect64BitPortabilityProblems="true"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="1"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCWebDeploymentTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="2"
UsePrecompiledHeader="0"
WarningLevel="3"
Detect64BitPortabilityProblems="true"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="1"
GenerateDebugInformation="true"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCWebDeploymentTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\display.cpp"
>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
</Filter>
<Filter
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>


But there maybe is an error in the standard library, I maybe have to reinstall it?


amoK

EDIT :
I have tried to compile the .cpp file from an other text editor, on a shell, but I´m getting the same errors.
Maybe I have changed something in the stdandard library.
Is there a possibility to just download the include files of the standard lib?

amoK

[Edited by - am0K on June 11, 2006 3:15:22 AM]

Share this post


Link to post
Share on other sites
Heya am0K,

I compared your vcproj file with mine and they are for the most part identical. As well, your code compiles and runs fine on my machine. So whatever the problem is, its not with your code or project, so it almost has to be with your visual studio installation or configuration.

Did you accept all the defaults when installing? Are you SURE you didn't change anything in the visual studio header files?

At this point, I'm afraid I cant offer much more help. All the "normal" problems seem to be ok. You might try re-installing Visual C++. And no, I don’t believe you can just install the header files. You can, however, re-install with the same executable you used to install it, and it might even have a "repair" mode installation.

Cheers!

Share this post


Link to post
Share on other sites
Trying to do this without looking:)

What is an interpreter?
An interpreter reads a pgm and runs it as it is being read.

What is a compiler?
A compiler takes source code, turns it into an object file and then links it to produce an executable.

Why is compiled code faster than translated code?
Because compiled code contains code that has already been translated into an object file and linked (both of those processes take time).

What does structured programming (procedural programming) consist of?
Smaller, broken down tasks that refer to each other when necessary.

What were the primary problems object oriented programming was designed to solve?
Previously, you were unable to associate specific data with the proper structures easily. With object-oriented programming, you can do this.

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

What is encapsulation?
The act of having self-contained units.

What is inheritance?
Being able to extend upon things that already exist.

What is polymorphism?
The ability for things to take on different forms.

Should you learn C before C++, why or why not?
They are extremely similar, so it is not necessary.

What is the FIRST question you ask when beginning your designs?
"What are my exact goals?"

What is the SECOND question you ask when beginning your designs?
"Do I need to create custom software to achieve these goals?"

What are the two primary steps in creating an executable? What is the output of each step?
Compiling your code, which produces an object file, and then linking that file, which creates an executable.

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


Chapter 2

When you issue a command to compile your code, what is run first? What does it do?
The preprocessor looks for '#' commands.

What symbol indicates a preprocessor directive?
The '#' symbol.

What does an "include" directive instruct the preprocessor to do?
It instructs the preprocessor to look for the include <file> from its set of libraries, and include it in this program.

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

Who calls that function?
Nobody, it is run automatically.

Can you declare main as void? Why use int instead?
Int is not only standard, but you can also use it for error detection as well.

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

What is the Standard Library?
A set of classes.

How do you instruct the compiler that you want to use part of the Standard Library?
You can use either 'Using namespace std;' or 'Using std::XXXx;'. Or, simply std::xxxx in lines of your code.

[Extra Credit] What's the difference between "\n" and endl?
endl clears the memory buffer on your machine.

What are the 3 ways to use a class in a namespace? Use "cout" as an example.
std::cout
using namespace std;
using std:coutl

What are the two types of comments?
//This is one type of comment.
/*This is another
type of comment*/

[Extra Credit] What is self-documenting code? Does that remove the need for comments?
Self-documenting code is simply using proper, easy-to-understand function names and variables in your code. It does not remove the need for comments, however.

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?
A method.

[Extra Credit]What does a compiler do with "white space?"
It ignores it.

Share this post


Link to post
Share on other sites
Instead of posting a list with all the same answers again, I'm just going to answer these two. This is, because my answers were a little bit different from the others and I wonder if they are correct.

* [Extra Credit] What's the difference between "\n" and endl?
1. \n only starts a new line and endl starts a new line and clears the memory buffer
2. endl takes care of platform differences with end-line characters. (is this correct?)

* [Extra Credit]What does a compiler do with "white space?"
Depends where the white space is placed:
1.
char space = ' ';

The space is used as the value of the character.
2.
char     space = ' ';

All the extra spaces between char and space are ignored.

[Edit]And I see the extra spaces are ignored by the forum too :'([/Edit]

[Edited by - RinusMaximus on June 12, 2006 12:25:07 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Palejo
What is an interpreter?
An interpreter reads a pgm and runs it as it is being read.

Is it what the workshop book state? While being technically true, the sentence is still quite obscure. Does it give a better explaination?

Quote:
Original post by Palejo
[Extra Credit]What does a compiler do with "white space?"
It ignores it.


It ignores white spaces that have no repercussion on the lexical analysis, which is quite different. As RinusMaximus, there's a difference between
char      var;

and
char var = "     "
.
Whitespaces are useful during the lexical analysis (when not enclosed between " or ') because a whitespace automatically ends a lexeme (token) and don't have any meaning by itself. It means that you can add any number of whitespace between two lexemes without changing the meaning of the lexemes.

This sounds rather dull but has a very important application in computer engineering.

There is something true about programs: they are hard to write. There is something even more true: they are even harder to read. A good use of whitespaces can really enhance the readability of the source code, that's the reason why nearly all the programmer's guidelines around the world says "code should be correctly indented and whitespaces should be used in this way".

To make sure you understood everything (not difficult), read this and tell me which program is the most understandable:

1)

#include <iostream>

int main()
{
int age;

std::cout << "How old are you ? ";
std::cin >> age;
std::cout << "You are " << age << " y.o" << std::endl;
}


2)

#include <iostream>
int main(){int age; std::cout<<"How old are you ? "
;std::cin>>age;std::cout<<"You are "<<age<<" y.o"<<std::endl;}


Regards,

Share this post


Link to post
Share on other sites
Maybe one of the tutors could post the correct answers to the quizzes after three weeks or so? This way we can check if we were right. It's not that important now, but for the later chapters it will probably be useful...

Share this post


Link to post
Share on other sites
Quote:
Original post by twoaterisn
Maybe one of the tutors could post the correct answers to the quizzes after three weeks or so? This way we can check if we were right. It's not that important now, but for the later chapters it will probably be useful...


In my idea, it would be better if the attendee post their answers first, because then the tutors will be able to explain them what are there errors and to give them hints to avoid doing the same error in the future.

Anyway, if the tutors want to post the whole answers, I suggest them to send them to jwalsh for centralization (having N tutors posting their own answers to the quizz is rather useless and confusing).

Regards,

Share this post


Link to post
Share on other sites
Quote:
Original post by Emmanuel Deloget
In my idea, it would be better if the attendee post their answers first, because then the tutors will be able to explain them what are there errors and to give them hints to avoid doing the same error in the future.



I agree, but after this has been done, it would be easier to check your own answers against one post. This way you don't have to read the entire thread to see which answer was given most.

Share this post


Link to post
Share on other sites
OK now i'd like to post my poor solution ...

i checked the code in stdlib.h and i find : (the source code are sorted ... )

#ifndef _CRT_TERMINATE_DEFINED
#define _CRT_TERMINATE_DEFINED

#ifdef exit // this is not defined ...
#undef exit
_CRTIMP __declspec(noreturn) void __cdecl exit(__in int _Code);
#endif

_CRTIMP __declspec(noreturn) void __cdecl [color=red]_exit[/color] (__in int _Code); // what should i do for strengthening the code ? ...
_CRTIMP void __cdecl abort(void);
#endif

in the empty new project without and MACRO defined and you will find no 'exit' function defined ...

in that case i did the poor decision in code :

// cstdlib_ques.cpp
#define exit

#include <iostream>
using namespace std ;

int main( void )
{
cout << "ok now ... but ..." << endl ;
return (0) ;
}

yes, ok now ... but ...

of course, as someone said, we should not change '_exit' to 'exit' ...

Share this post


Link to post
Share on other sites
another test ... for <cstdlib> under visual C++ .net

// cstdlib_ques.cpp

// my declaration ...
void exit(int) ;

#include <cstdio>
#include <cstdlib>

int main( void )
{
printf_s( "ok now ... but ... \n" ) ;
system( "pause" ) ;
return (0) ;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by littlewater
another test ... for <cstdlib> under visual C++ .net

// cstdlib_ques.cpp

// my declaration ...
void exit(int) ;

#include <cstdio>
#include <cstdlib>

int main( void )
{
printf_s( "ok now ... but ... \n" ) ;
system( "pause" ) ;
return (0) ;
}


This is a bad example. We're not using cstdlib or cstdio at this point. As well, you're using C-Functions for printing to the screen (printf) and we're using C++ streams. Finally, as was discussed previously, dont use "pause" to trap a keystroke at the end of your executable. Its bad for the shell.

Cheers!

Share this post


Link to post
Share on other sites
Greetings all!

Every Monday in addition to posting the chapter thread for that week I will post the answers for the quiz from two weeks ago. Today I will post the quiz answers for week 1. This coming Monday will be week 4, and I will post the answers for the week 2 quiz.

Chapter 1
Q. What is an interpreter?
A. An interpreter translates and executes a program as it reads it, turning the program instructions, or source code, directly into actions.

Q. What is a compiler?
A. A compiler translates source code into an intermediary form. This step is called compiling, and it produces an object file. The compiler then invokes a linker, which combines the object files into an executable program.

Q. Why is compiled code faster than translated code?
A. Compiled programs can run very fast because the time consuming task of interpreting the code into machine language is done once when the executable is built, not when the program is run.

Q. What does structured programming (procedural programming) consist of?
A. A series or procedures that act upon data, where the data is kept separate from the procedures. The programmer must keep track of which procedures call which other procedures and where the data is modified. Any procedure that was too large to manage was simply broken down into smaller, more manageable procedures.

Q. What were the primary problems object oriented programming was designed to solve?
A. The lack of pairing between data and procedures. The inability to re-use code due to incompatibility with the data types.

Q. What are the three pillars of object oriented programming? Does C++ support them all?
A. Encapsulation, Inheritance, and Polymorphism

Q. What is encapsulation?
A. Encapsulation is the property of being a self-contained unit and allows data hiding and abstraction. It frees developers from the need to understand how something works and allows them to focus on what it does.

Q. What is inheritance?
A. Inheritance is the ability to create a new data type that is an extension of an existing type. The new data type can have some or all of the properties and functionality of the existing type, while allowed developers to add or override unique functionality in the derived data type.

Q. What is polymorphism?
A. Polymorphism is the ability of an object to take on many multiple forms. Technically speaking, its the ability for a derived class to be treated as its parent type, while functioning as its derived type.

Q. Should you learn C before C++, why or why not?
A. No. Nearly all industry professionals agree that learning C before C++ is not only NOT helpful, but can also be disadvantageous to do so. The concepts of C, ie. procedural programming had many problems and encouraged what most today would consider bad programming practice. Remember that C++ was developed as a result of the deficiencies and limitations of the C language. Don’t think of C++ as being built 'on top' of C. Think of C++ as being built to be used INSTEAD of C.

Q. What is the FIRST question you ask when beginning your designs?
A. "What is the problem I'm trying to solve?"

Q. What is the SECOND question you ask when beginning your designs?
A. "Can this be accomplished without resorting to writing custom software?"

Q. What are the two primary steps in creating an executable? What is the output of each step?
A. Compiling and Linking. Compiling outputs object files, linking outputs either a library or an executable.

Q. If your compiler requires <iostream.h> does it follow the ANSI standard? What IS the standard?
A. No. The ANSI standard is to remove the '.h' from all Standard Library headers. Thus, <iostream> is the standard.


Chapter 2
Q. When you issue a command to compile your code, what is run first? What does it do?
A. The preprocessor is run first. The preprocessor reads through your code looking for lines that begin with the # symbol. When it finds those lines in performs some operation on the line depending on the command following the # symbol.

Q. What symbol indicates a preprocessor directive?
A. The # symbol

Q. What does an "include" directive instruct the preprocessor to do?
A. The include directive instructs the preprocessor to find the named file, read it, and dump its contents into the current file before compilation begins.

Q. What is the necessary function which all console C++ programs have?
A. All C++ console applications must have a function called 'main'.

Q. Who calls that function?
A. The Operating System calls the main function.

Q. Can you declare main as void? Why use int instead?
A. No. main should always return an integer so that the shell knows how the program exited, that is - with or without errors.

Q. Are "curly braces" optional when enclosing a function?
A. No. They are required.

Q. What is the Standard Library?
A. The Standard Library is the standard collection of classes and global objects that comes with every ANSI-compliant compiler.

Q. How do you instruct the compiler that you want to use part of the Standard Library?
A. By including the header file required by the object or type you want to use, and then specifying the use of the standard namespace 'std'.

Q.[Extra Credit] What's the difference between "\n" and endl?
A. \n inserts a 'new line' into the stream. endl inserts a new line as well as flushing the stream.

Q. What are the 3 ways to use a class in a namespace? Use "cout" as an example.
A. using namespace std; using std::cout; std::cout << ...

Q. What are the two types of comments?
A. Single line comments. ie. // and multi-line comments ie. /* */

Q.[Extra Credit] What is self-documenting code? Does that remove the need for comments?
A. Self-documenting code is code written such that function and variable names give a clear picture of what the code is doing. No, this does not remove the need for comments. Many people feel that comments are meant to explain WHY something is be doing done, while the code itself clearly explains WHAT is being done. Clearly written comments in addition to well formed and illustrative function and variable names can go a long way in working together to make your code more readable and easier to understand.

Q. What Are the 3 components of a function declaration (function header)?
A. Return type, identifier (function name), and argument list.

Q. What do you call a function that is part of a class?
A. A method of the class.

Q.[Extra Credit] What does a compiler do with "white space?"
A. It ignores all non-relevant white space, while preserving white space within literals.

Cheers!

Share this post


Link to post
Share on other sites
Invisal's Extra Exercises


1. Write a program that print your name on the screen.

#include <iostream>
#define name "Coury Ditch"


int main()
{
std::cout << name << 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 13 errors in this code

#include <iostream>

int main()
{
std::cout << "There are many mistakes in this code." << std::endl;
std::cout << "Can you help me fix all the bugs in this code" << std::endl;
std::cout << "Thank You!" << std::endl;
return 0;
}


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

#include <iostream>

int star()
{

std::cout << "*******\n" << " ***** \n";
return 0;
}

int main()
{
star();
star();
return 0;
}



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

#include <iostream>

int main()
{
int age;

std::cout << "How old are you?: ";
std::cin >> age;
std::cout << "You are " << age << " years 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.

#include <iostream>

int Add(int x, int y)
{
std::cout << "The summary is: " << x+y << std::endl;
return 0;
}

int Sub(int x, int y)
{
std::cout << "The subtraction is: " << x-y << std::endl;
return 0;
}

int Mul(int x, int y)
{
std::cout << "The multiplication is: " << x*y << std::endl;
return 0;
}


int main()
{
int a, b;

std::cout << "Please enter your first number: ";
std::cin >> a;
std::cout << "Please enter your second number: ";
std::cin >> b;
Add(a, b);
Sub(a, b);
Mul(a, b);
return 0;
}





JWalsh's Quizz

CHAPTER 1

1. What is an interpreter?
Interpreter translates code directly into actions.

2.What is a compiler?
Compiler translates code to intermediary form, constructs an object file.

3.Why is compiled code faster than translated code?
Its not interpreted, and ran on the spot. It stays interpreted and can be run anytime.

4.What does structured programming (procedural programming) consist of?
A function, or set of instructions executed line after line. Meant to keep track of what functions call other functions, and what data is changed.

5.What were the primary problems object oriented programming was designed to solve?
Managing enormous complexity, and coupling data with the tasks that manipulate that data.

6.What are the three pillars of object oriented programming? Does C++ support them all?
encapsulation, data hiding, inheritance, and polymorphism. Trick question? C++ fully supports object-oriented programming.

7.What is encapsulation?
Self contained unit of code.

8.What is inheritance?
New types, based off other types, or extensions of other types, can be declared.

9.What is polymorphism?
Same name taking many forms.

10.Should you learn C before C++, why or why not?
No. The language has stayed very similar, but some of the syntax has changed. Learning C before C++ might make learning C++ confusing.

11.What is the FIRST question you ask when beginning your designs?
What is the problem I am trying to solve.

12.What is the SECOND question you ask when beginning your designs?
Can this be accomplished without resorting to writing custom software?

13.What are the two primary steps in creating an executable? What is the output of each step? Compile the source to an object file, link the object and create the executable.

14.If your compiler requires <iostream.h> does it follow the ANSI standard? What IS the standard? No, <iostream> is the correct way, many older books will state syntax not compliant with the ANSI C++ standards.


CHAPTER 2

1.When you issue a command to compile your code, what is run first? What does it do?
The preprocessor is run, this runs through all of your code and searches for #, it will make sure all your #define, #include , etc. are introduced in your program before anything else.

2.What symbol indicates a preprocessor directive? #

3. What does an "include" directive instruct the preprocessor to do?
Include the exact contents of that certain file in that spot of your code.

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

5. Who calls that function?
The compiler auto-calls it.

6.Can you declare main as void? Why use int instead?
Void is not ANSI compliant, you must return an integer to the function to see if it passes or errors out.

7.Are "curly braces" optional when enclosing a function?
No, everything within {} is your function.

8.What is the Standard Library?
A collection of classes and global objects that comes with every ANSI-compliant compiler.

9.How do you instruct the compiler that you want to use part of the Standard Library?
By either putting std:: in front of the class/object. Or declaring "using namespace standard"

10.[Extra Credit] What's the difference between "\n" and endl?
\n creates a new line, endl clears the buffer and starts a new line.

11.What are the 3 ways to use a class in a namespace? Use "cout" as an example.
std::cout, using namespace standard; cout; , using namespace std::cout.

12.What are the two types of comments?
/* */ and //

13.[Extra Credit] What is self-documenting code? Does that remove the need for comments?
Self-documenting code is an IDE option, in inserts a comments section before your code to better document it. Comments are still recomended for use throughout the code, explaining differant functions, etc.

14.What Are the 3 components of a function declaration (function header)?
Return type, identifier (function name), and argument list.

15.What do you call a function that is part of a class?
A Method of the Class.

16.[Extra Credit] What does a compiler do with "white space?"
Ignores it unless its used in '' ir "" literals.




Share this post


Link to post
Share on other sites

This topic is 3498 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this