Sie sind auf Seite 1von 20

ASSIGNMENT-1

INTRODUCTION TO COMPUTING
EE-112
Roll no: FA-20-BEE-041
Name: Muhammad Usman Sharif

Fundamentals of C++ Programming


Chapter #1

The Context of Software Development:

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.

2. How is compiled code different from source code?

Source code Gets Converted Before It Can Run. ... Compiled languages is when a


person writes the code, compiler separates the file and the end result is an executable
file. Basically, owner keeps the source code. Interpreted languages
are different because the code is not compiled first hand.

3. What tool does a programmer use to produce C++ source code?

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.

5. What does the linker do?

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.

7. What does the preprocessor do to source code?

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.

9. How can an IDE improve a programmer’s productivity?

An IDE, or Integrated Development Environment, enables programmers to consolidate the


different aspects of writing a computer program. IDEs increase programmer productivity by
combining common activities of writing software into a single application: editing source code,
building executables, and debugging.

10. Name a popular C++ IDE used by programmers developing for Microsoft Windows.

1. Visual Studio Code. It is an open-source code editor developed


by Microsoft for Windows, Linux and Mac OS.
2. Eclipse. It is one of the most popular, powerful and useful IDEs used by developers for
C/C++ programming.

11. Name a popular C++ IDE is used by programmers developing for Apple macOS.

Eclipse. Eclipse is a popular open-source IDE that C++


programmers can use to develop applications using a special C++ plugin. Like most IDEs we'll
touch upon, this one is multiplatform, running on Mac OS X.

Chapter #2

Writing a C++ Program:

2. Exercises
1. What preprocessor directive is necessary to use statements with the std::cout printing
stream object?

When a cout statement executes, it sends a stream of characters to the standard output stream


object - std::cout - which is normally “connected” to the screen. The std:: before cout is required
when we use names that we've brought into the program by the preprocessing
directive #include <iostream>.

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

4. All C++ programs must have a function named what?

2 Functions ( Control structures ) every C++ program must have a function called main


( program execution ) .

5. The body of main is enclosed within what symbols?

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.

6. What operator directs information to the std::cout output stream?

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.

std::cout << 6 << '\n';

std::cout << "6" << '\n';

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.

std::cout << x << '\n';

std::cout << "x" << '\n';

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:

However, if we run the second program it will be printed as it is:


3. What is the largest int available on your system?

The number 2,147,483,647 (or hexadecimal 7FFFFFFF16) is the maximum positive value


for a 32-bit signed binary integer in computing. It is therefore the maximum value for variables
declared as integers (e.g., as int ) in many programming languages, and the maximum possible
score, money, etc.

4. What is the smallest int available on your system?

A short int which has two bytes of memory, has a minimum value range of -32,768


and a maximum value range of 32,767 . An unsigned short int , unsigned meaning having no
negative sign (-), has a minimum range of 0 and a maximum range of 65,535.

5. What is the largest double available on your system?

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.

6. What is the smallest double available on your system?

−2,147,483,648 with the storage of 4 bytes.

7. What C++ data type represents nonnegative integers?


C++ supports unsigned integers to represent non-negative integers.
Unsigned integers are integers that can only hold non-negative whole numbers. A 1-byte
unsigned integer has a range of 0 to 255. Compare this to the 1-byte signed integer range of
-128 to 127.

8. What happens if you attempt to use a variable within a program, and that variable is not
declared?

It will not be printed.. It will give an error:

9. What is wrong with the following statement that attempts to assign the value ten to
variable x?
10 = x;

Variable x is an identifier and it must be declared otherwise it will give error:

10. Once a variable has been properly declared and initialized can its value be changed?

Once we declare a variable, the type of that variable cannot change, but the value can. There


are methods for converting information from one type of variable to another type of variable, for
example if we need to use an integer value as a string.

11. What is another way to write the following declaration and initialization?
int x = 10;

Another way to write following declaration and initialization is:

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:

13. In the declaration do a and b represent the same memory location?

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:

(a) fred : Illegal


(b) if : Legal

(c) 2x : Illegal

(d) -4 : Legal

(e) sum_total: Illegal

(f) sumTotal : Illegal

(g) sum-total : Illegal

(h) sum total : Illegal

(i) sumtotal : Illegal

(j) While : Illegal

(k) x2 : Illegal

(l) Private: Illegal

(m) public : Legal

(n) $16 : Illegal

(o) xTwo : Illegal

(p) _static : Illegal

(q) _4 : Illegal

(r) ___ : Illegal

(s) 10% : Legal

(t) a27834 : Illegal

(u) wilma's: 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.

17. What is the difference between float and double?

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.

19. How is the value 2.45×10−5 expressed as a C++ literal?

20. How can you ensure that a variable’s value can never be changed after its initialization?

Using const we can define variables whose values never change. We MUST assign an


initial value into a constant variable when it is declared. If we do not place this initial value, C++
will not allow us to assign a value at a later time. Constants cannot be changed within a
program.

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 (“”).

25. Is 'i' a string literal or character literal?

‘I’ is a character literal because it is enclosed within the single quotation marks (‘’).

26. Is it legal to assign a char value to an int variable?

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.

27. Is it legal to assign an int value to a char variable?

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.

28. What is printed by the following code fragment?

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.

35. Determine the exact type of each of the following variables:

(a) auto a = 5; constant

(b) auto b = false; Boolean

(c) auto c = 9.3; Floating number


(d) auto d = 5.1f; Floating number

(e) auto e = 5L; constant

THE END

Das könnte Ihnen auch gefallen