Assignment-1: Introduction To Computing EE-112
Assignment-1: Introduction To Computing EE-112
Assignment-1: Introduction To Computing EE-112
INTRODUCTION TO COMPUTING
EE-112
Roll no: FA-20-BEE-041
Name: Muhammad Usman Sharif
1. Exercises
1. What is a compiler?
A compiler translates the source code to target code. It translates C++ source code to
machine code. The target code may be the machine language for a particular platform or
embedded device. The target code could be another source language; for example, the earliest
C++ compiler translated C++ into C, another higher-level language. The resulting C code was
then processed by a C compiler to produce an executable program. C++ compilers today
translate C++ directly into machine language.
Many developers use integrated development environments (IDEs). An IDE includes editors,
debuggers, and other programming aids in one comprehensive program. Examples of IDEs for
C++ include Microsoft’s Visual Studio 2015, the Eclipse Foundation’s Eclipse CDT, and Apple’s
XCode. Despite the plethora of tools (and tool vendors’ claims), the programming process for all
but trivial programs is not automatic. Good tools are valuable and certainly increase the
productivity of developers, but they cannot write software. There are no substitutes for sound
logical thinking, creativity, common sense, and, of course, programming experience.
4. What tool(s) does a programmer use to convert C++ source code into executable machine
code?
A compiler takes the program code (source code) and converts the source code to a machine
language module (called an object file). Another specialized program, called a linker, combines
this object file with other previously compiled object files (in particular run-time modules) to
create an executable file.
When a program comprises multiple object files, the linker combines these files into a unified
executable program, resolving the symbols as it goes along. Linkers can take objects from a
collection called a library or runtime library.
6. Does the linker deal with files containing source code or machine language code?
A compiler takes the program code (source code) and converts the source code to a machine
language module (called an object file). Another specialized program, called a linker, combines
this object file with other previously compiled object files (in particular run-time modules) to
create an executable file.
The preprocessor is a part of the compiler which performs preliminary operations (conditionally
compiling code, including files etc...) to your code before the compiler sees it. These
transformations are lexical, meaning that the output of the preprocessor is still text
8. List several advantages developing software in a higher-level language has over developing
software in machine language.
It is machine independent language. Easy to learn. Less error prone, easy to find and debug
errors. High level programming results in better programming productivity.
10. Name a popular C++ IDE used by programmers developing for Microsoft Windows.
11. Name a popular C++ IDE is used by programmers developing for Apple macOS.
Chapter #2
2. Exercises
1. What preprocessor directive is necessary to use statements with the std::cout printing
stream object?
2. What statement allows the short name cout to be used instead of std::cout?
using namespace std; is used to allow the short name cout to be used instead of std::cout.
3. What does the name std stand for?
“std” is an abbreviation for standard. So that means we use all the things with in “std”
namespace
A C++ program starts with function called main(). The body of the function is enclosed between
curly braces. The program statements are written within the brackets. Each statement must end
by a semicolon, without which an error message in generated.
By default, the standard output of a program is the screen, and the C++ stream object defined to
access it is cout. cout is used in conjunction with the insertion operator, which is written as <<
(two "less than" signs).
7. Write a C++ program that prints your name in the console window.
Input:
Output:
8. Write a C++ program that prints your first and last name in the console window. Your first
name should appear on one line, and your last name appear on the next line.
Input:
Output:
9. What other files must you distribute with your executable file so that your program will
run on a Windows PC without Visual Studio installed?
I don't need Visual Studio to run the executable file, but the thing that it needs is that the right
.NET Framework is installed, i.e. the version that the executable was compiled for.
10. Can a single statement in C++ span multiple lines in the source code?
We can break up statements across multiple lines in C++, as long as we don't separate the
characters within tokens. For example, if our statement has an operator >>=, we can’t break up
those three characters with a newline or any other whitespace characters.
Chapter #3
Values and Variables
3. EXERCISE:
1. Will the following lines of code print the same thing? Explain why or why not.
Both programs behave identically, but Listing 1 (number6.cpp) prints the value of the number six, while
Listing 2 (number4-alt.cpp) prints a message containing the digit six. However, Listing 1 (number6.cpp)
does not use quotation marks ("). The number 6 appears unadorned with no quotes. The expression '\n'
represents a single newline character. In C++, a single character represents a distinct type of data and is
enclosed within single quotes ('). And multiple characters comprising a string appear in double quotes
(").
2. Will the following lines of code print the same thing? Explain why or why not.
If we try to run the first program we will get an error because there is no value or statement
assigned to it:
If we assign any value to x…Then result will be different:
The largest integer that can be stored in a double without losing precision is the same as
the largest possible value of a double. That is, DBL_MAX or approximately 1.8 ×10308 (if our
double is an IEEE 754 64-bit double). It's an integer.
8. What happens if you attempt to use a variable within a program, and that variable is not
declared?
9. What is wrong with the following statement that attempts to assign the value ten to
variable x?
10 = x;
10. Once a variable has been properly declared and initialized can its value be changed?
11. What is another way to write the following declaration and initialization?
int x = 10;
Int x{10};
12. In C++ can you declare more than one variable in the same declaration statement? If so,
how?
We can declare more than one variable in the same declaration statement as follow:
int a;
int b;
Yes, in the following declaration a and b represent the same memory location.
14. Classify each of the following as either a legal or illegal C++ identifier:
(c) 2x : Illegal
(d) -4 : Legal
(k) x2 : Illegal
(q) _4 : Illegal
15. What can you do if a variable name you would like to use is the same as a reserved word?
A reserved word is a word that cannot be used as an identifier, such as the name of a variable,
function, or label – it is "reserved from use". This is a syntactic definition, and a reserved word
may have no meaning.
16. Why does C++ require programmers to declare a variable before using it? What are the
advantages of declaring variables?
C++ is a strongly-typed language, and requires every variable to be declared with its
type before its first use. This informs the compiler the size to reserve in memory for the variable
and how to interpret its value.
A double is 64 and single precision (float) is 32 bits. The double has a bigger mantissa (the
integer bits of the real number).
18. How can a programmer force a floating-point literal to be a float instead of a double?
We should use float if we have memory constraint because it takes almost half as much space
as double. If our numbers cannot fit in the range offered by float then use double. We must be
careful with floating point calculation and representation, we can't use double or float for
monetary calculation, instead use Big Decimal.
20. How can you ensure that a variable’s value can never be changed after its initialization?
21. How can you extend the range of int on some systems?
In C, the language itself does not determine the representation of certain datatypes. It can
vary from machine to machine, on embedded systems the int can be 16 bit wide, though
usually it is 32 bit.
The only requirement is that short int <= int <= long int by size. Also, there is a
recommendation that int should represent the native capacity of the processor.
All types are signed. The unsigned modifier allows you to use the highest bit as part of the
value (otherwise it is reserved for the sign bit).
22. How can you extend the range and precision of double on some systems?
If we’re extending both the range and precision of double precision floating point,we wouldn’t
call it double precision anymore. At least, not IEEE 754 double precision. Especially if we’re
adding bits a la 80-bit extended double precision.
So let’s limit ourselves to 64 bits. We could extend range OR precision by trading off mantissa
and exponent bits: more exponent gives more range, at the cost of precision, and vice versa.
A similar case exists in the form of the bfloat16 floating-point format: more range than 16-bit
half precision floating point, nearly the same as IEEE 754 single precision since it has the same
exponent size, but less precision than IEEE 754 half precision since it has only 8 bits of
mantissa. It’s been widely adopted at this point.
23. Write a program that prints the ASCII chart for all the values from 0 to 127.
Input:
Output:
24. Is "i" a string literal or character literal?
“I” is a string literal because it is enclosed within the double quotation marks (“”).
‘I’ is a character literal because it is enclosed within the single quotation marks (‘’).
To convert higher data type into lower, we need to perform typecasting. Here, the ASCII
character of integer value will be stored in the char variable. To get the
actual value in char variable, we can add '0' with int variable. Alternatively, we can use
Character.
The possible range in the value of a char is 0 to 65535, but an int can be anywhere from
-2147483648 to 2147483647. ... char is an unsigned type so even short and byte variables still
need a conversion to char as they may be negative.
int x;
x = 'A';
std::cout << x << '\n';
Input:
Output:
29. What is the difference between the character 'n' and the character '\n'?
Firstly, 'n' and '\n' are not strings but characters. 'n' is the n character. '\n' is the newline
character. As there are no key into your keyboard nor a graphics to write and represent the
newline character, it is used that letters (\n) to write only one character (the newline).
30. Write a C++ program that simply emits a beep sound when run.
31. Create an unscoped enumeration type that represents the days of the week.
Output:
Input:
32. Create a scoped enumeration type that represents the days of the week.
THE END