You are on page 1of 26

Fundamentals Of C++ Programming

Assignment No. 1
(By : Richard L. Halterman)___________

Name:
Shakeel Zafar
Class:
Bachelor of Electrical Engineering
. (1st semester)
Roll No:
FA19-BEE-083
Section:

C
Date:
15.01.2020
EXERCISE#1

Question.1:

What is a compiler?

Answer:

A compiler translates the source code to target 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.

Question.2:

How is compiled code different from source code?

Answer:

Source code is the set of human-readable instructions a


programmer writes. Source code is the input to a compiler. Compiled
code is the output from a compiler. A compiler is just a program that
translates source code into compiled code.

Question.3:

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

Answer:

A programmer uses editor to enter the program source code


and save it to files.

Question.4:

What tool(s) does a programmer use to convert C++ source


code into executable machine code?

Answer:
A programmer uses preprocessor to change source code into
enhanced source code, then he uses compiler to translate it to object
code and then he uses linker to convert it to executable machine code.

Question.5:

What does the linker do?

Answer:

It combines the compiler-generated machine code with


precompiled library code or compiled code from other sources to make a
complete executable program.

Question.6:

Does the linker deal with files containing source code or


machine language code?

Answer:

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.

Question.7:

What does the preprocessor do to source code?

Answer:

It adds to or modifies the contents of the source file before the


compiler begins processing the code. We use the services of the
preprocessor mainly to #include information about library routines or
program’s use.
Question.8:

List several advantages of developing software in a higher-level


language has over developing software in machine language.

Answer:

Advantages of High level language

 High level languages are programmer friendly.


 It provides higher level of abstraction from machine languages.
 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.

Question.9:
How can an IDE improve a programmers’s productivity?
Answer:
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.

Question.10:
Name a popular a C++ IDE used by programmers for developing
Microsoft Windows.
Answer.10;
1. Eclipse. It is one of the most popular, powerful and useful IDEs used by
developers for C/C++ programming. ...
2. NetBeans. ...
3. Visual Studio Code. It is an open-source code editor developed Sublime Text. ...
4. Atom. ...
5. Code::Blocks. ...
6. CodeLite. ...
7. CodeWarrior.

Question.11
Name a popular a C++ IDE used by programmers for developing Apple
MacOS.
Answer,11:
0. Eclipse is one of the simplest and most powerful IDEs for
C++ development. This is an open source IDE that is available for
Windows, Mac OS X, and Linux

1. Visual Studio Code. Visual Studio Code is possibly the best JavaScript ide for
Windows, Mac, and Linux. ...
2. RJ TextEd. ...
3. Light Table. ...
4. NetBeans. ...
5. Brackets. ...
6. Komodo Edit. ...
7. Atom by Github. ...
8. SUBLIME TEXT 3.

EXERCISE#2

Question.1:

What preprocessor directive is necessary to use statements with


the std: :cout printing stream object?

Answer:
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>.

Question.2:

What statement allows the short name cout to be used instead of


std: :cout?

Answer:

The use of directive using namespace std; ,before int main () ,


allows us to use the short name cout instead of std: :cout in statement.

Question.3:

What does the name std stand for?

Answer:

std is an abbreviation of standard. std is the standard


namespace. cout, cin and a lot of other things are defined in it.

Question.4:

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

Answer:

Every C++ program must have a function called main. We will


think of this as the body of the program – the section of
your program containing the executable statements outlined in your
flowchart.

Question.5:

The body of main is enclosed within what symbols?

Answer:

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 is generated.

Question.6:

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

Answer:

The output stream object std::cout—normally connected to the


screen, is used to output data. Multiple data items can be output by linking
together stream insertion (<<) operators.

Question.7:

Write a C++ program that prints your name in the console


window.

Answer:
#include <iostream>
int main() {

std::cout << “Hello User, Enter your first name!\n”

<< “Shakeel!\n”

<< “Hello Shakeel, It was nice to know your name!\n”;

IN CONSOLE WINDOW
Hello User, Enter your first name.
Shakeel
Hello Shakeel, It was nice to know your name!

Question.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 on the next line.

Answer:
#include <iostream>

int main() {

std::cout << “Input First Name: Shakeel!\n”

<< “Input Last Name: Zafar!\n”;


}

IN CONSOLE WINDOW
Input First Name: Shakeel
Input Last Name: Zafar

Question.10:

Can a single statement in C++ span multiple lines in the source


code?

Answer:

Yes. C++, like C and many other programming languages, is


free-form. You can break up statements across multiple lines, as long as
you don't separate the characters within tokens. For example, if
your statement has an operator >>=, don't break up those three characters
with a newline or any other whitespace characters.
EXERCISE#3

Question.1:

Will the following lines of code print the same thing? Explain why
or why not.
std::cout << 6 << '\n';
std::cout << "6" << '\n';

Answer:

Yes, they will print the same thing ,since 6 has no character type
assigned to it in ASCII system.

Question.2:

Will the following lines of code print the same thing? Explain why
or why not.
std::cout << x << '\n';
std::cout << "x" << '\n';

Answer:

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


prints 10, the value of the variable x, but the statement
std::cout << "x" << '\n';
prints x, the message containing the single letter x.

Question.3:

What is the largest int available on your system?

Answer:

2,147,483,647 the largest int available on our system.


Question.4:

What is the smallest int available on your system?

Answer:

- 2,147,483,648 is the smallest int available on our system.

Question.5:

What is the largest double available on your system?

Answer:

1.79769*10+308 is the largest double available on our system.

Question.6:

What is the smallest double available on your system?

Answer:

2.22507*10-308 is the smallest double available on our system.

Question.7:

What C++ data type represents nonnegative integers?

Answer:

C++ provides integer-like types that exclude negative numbers.


These types include the word unsigned in their names, meaning they do
not allow a negative sign.
Question.8:

What happens if you attempt to use a variable within a program,


and that variable is not declared?

Answer:

If you failed to declare variable, then an ERROR will be thrown


by the compiler. If you failed to define a value to variable, then compiler
will throw a warning mess. If you are using any variable inside code,
then declaring a variable is must.

Question.9:

What is wrong with the following statement that attempts to


assign the value ten to variable x?
10 = x;

Answer:

An integer can’t be written before a variable, so an error will be


generated in this case.

Question.10:

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

Answer:

Once declared, a particular variable cannot be re-declared in the


same context. A variable may not change its type during its lifetime.
Question.11:

What is another way to write the following declaration and


initialization?
int x = 10;

Answer:

1. #include <iostream>
2. int main() {
3. int x{10};
4. std::cout << x << '\n';
5. }
This alternate form is not commonly used for simple variables, but it
necessary for initializing more complicated
kinds of variables called objects.

Question.12:

In C++ can you declare more than one variable in the same
declaration statement? If so, ow?

Answer:

Variables can be reassigned different values as needed, as


Listing shows.Listing 3.5: multipleassignment.cpp
1. #include <iostream>
2. int main() {
3. int x;
4. x = 10;
5. std::cout << x << '\n';
6. x = 20;
7. std::cout << x << '\n';
8. x = 30;
9. std::cout << x << '\n';
10. }

Question.13:

In the declaration
int a;
int b;

do a and b represent the same memory location?

Answer:

Importantly, the statement

int a;
int b;

does not mean a and b refer to the same box (memory location).

Question.14:

Classify each of the following as either a legal or illegal C++


identifier:
(a) fred
(b) if
(c) 2x
(d) -4
(e) sum_total
(f) sumTotal
(g) sum-total
(h) sum total
(i) sumtotal
(j) While
(k) x2
(l) Private
(m) public
(n) $16
(o) xTwo
(p) _static
(q) _4
(r) _ _ _
(s) 10%
(t) a27834
(u) wilma's

Answer:
(a) fred Legal
(b) if Illegal
(c) 2x Illegal
(d) -4 Illegal
(e) sum_total Legal
(f) sumTotal Legal
(g) sum-total Illegal
(h) sum total Illegal
(i) sumtotal Legal
(j) While Legal
(k) x2 Legal
(l) Private Legal
(m) public Illegal
(n) $16 Illegal
(o) xTwo Legal
(p) _static llegal
(q) _4 llegal
(r) _ _ _ llegal
(s) 10% Illegal
(t) a27834 Legal
(u) wilma's Illegal

Question.15:

What can you do if a variable name you would like to use is the
same as a reserved word?

Answer:

When variables must be declared, the compiler can catch


typographical errors that dynamically-typed
languages cannot detect. For example, consider the following section of
code:
char While;
While = 1;

Question.16:

Why does C++ require programmers to declare a variable before


using it? What are the advantages of declaring variables?
Answer:

Before they are used, all variables have to


be declared. Declaring a variable means defining its type, and optionally,
setting an initial value (initializing the variable). Variables do not have to
be initialized (assigned a value) when they are declared, but it is often
useful.

Question.17:

What is the difference between float and double?

Answer:

The Decimal, Double, and Float variable types are different in


the way that they store the values. Precision is the
main difference where float is a single precision (32bit) floating point data
type, double is a double precision (64 bit) floating point data type and
decimal is a 128-bit floating point data type.

Question.18:

How can a programmer force a floating-point literal to be a float


instead of a double?

Answer:

To make a literal floating-point value a float, you must append an


f or F to the number, as in
3.14f
(The f or F suffix is used with literal values only; you cannot change a
double variable into a float variable by appending an f. Attempting to do so
would change the name of the variable!)
Question.19:

How is the value 2.45*10-5 expressed as a C++ literal?

Answer:

It is expressed as 2.45e-5.

Question.20:

How can you ensure that a variable’s value can never be


changed after its initialization?
Answer:

Constants are declared like variables with the addition of the


const keyword:
const double PI = 3.14159;
Once declared and initialized, a constant can be used like a variable in all
but one way—a constant may not be reassigned. It is illegal for a constant
to appear on the left side of the assignment operator (=) outside its
declaration statement. A subsequent statement like
PI = 2.5;
would cause the compiler to issue an error message:
error C3892: ’PI’ : you cannot assign to a variable that is const

Question.21:

How can you extend the range of int on some systems?

Answer:

We can increase the range of int by adding longh and long long
before int like:

long int;

long long int;

Question.22:

How can you extend the range and precision of double on some
systems?

Answer:

We can extend the range and precision of double by adding long


before double like:

long double;

It almost doubles the range and precision of double.


Question.23:

Write a program that prints the ASCII chart for all the values from
0 to 127.

Answer:
1. /*
2. * C++ Program to Print ASCII table (0 - 127)
3. */
4.
5. #include<iostream>
6. #include<iomanip>
7. using namespace std;
8.
9. char const* character[] = {"", "", "", "", "", "", "", "",
10. "\\a","\\b","\\t","\\n","\\v","\\f","\\r", "",
11. "", "", "", "", "", "", "", "",
12. "", "", "", "", "", "", "", ""};
13.
14. int main()
15. {
16. char c;
17. int row;
18. cout << " ASCII Table" << endl << "=============" << endl;
19. for(int i = 0; i < 16; i++)
20. {
21. row = i;
22. while (row <= 127) {
23. if (row < 32)
24. cout << setfill('0') << setw(2) << setbase(16)
25. << row << " = " << setw(3) << setfill(' ')
26. << character[i] << " | ";
27. else if (row >= 32 && row < 127)
28. {
29. c = row;
30. cout << setfill('0') << setw(2) << setbase(16)
31. << row << " = " << setw(3) << setfill(' ')
32. << c << " | ";
33. }
34. else
35. cout << setfill('0') << setw(2) << setbase(16)
36. << row << " = " << setw(3) << setfill(' ')
37. << "DEL" << " | ";
38. row = row + 16;
39. }
40. cout << endl;
41. }
42. }
43. cout << "!" << endl;
44. }

ASCII Table
=============
00 = | 10 = | 20 = | 30 = 0 | 40 = @ | 50 = P | 60 = ` | 70 = p
|
01 = | 11 = | 21 = ! | 31 = 1 | 41 = A | 51 = Q | 61 = a | 71 = q
|
02 = | 12 = | 22 = " | 32 = 2 | 42 = B | 52 = R | 62 = b | 72 = r
|
03 = | 13 = | 23 = # | 33 = 3 | 43 = C | 53 = S | 63 = c | 73 = s
|
04 = | 14 = | 24 = $ | 34 = 4 | 44 = D | 54 = T | 64 = d | 74 = t
|
05 = | 15 = | 25 = % | 35 = 5 | 45 = E | 55 = U | 65 = e | 75 = u
|
06 = | 16 = | 26 = & | 36 = 6 | 46 = F | 56 = V | 66 = f | 76 = v
|
07 = | 17 = | 27 = ' | 37 = 7 | 47 = G | 57 = W | 67 = g | 77 = w
|
08 = \a | 18 = \a | 28 = ( | 38 = 8 | 48 = H | 58 = X | 68 = h | 78 = x
|
09 = \b | 19 = \b | 29 = ) | 39 = 9 | 49 = I | 59 = Y | 69 = i | 79 = y
|
0a = \t | 1a = \t | 2a = * | 3a = : | 4a = J | 5a = Z | 6a = j | 7a = z
|
0b = \n | 1b = \n | 2b = + | 3b = ; | 4b = K | 5b = [ | 6b = k | 7b = {
|
0c = \v | 1c = \v | 2c = , | 3c = < | 4c = L | 5c = \ | 6c = l | 7c = |
|
0d = \f | 1d = \f | 2d = - | 3d = = | 4d = M | 5d = ] | 6d = m | 7d = }
|
0e = \r | 1e = \r | 2e = . | 3e = > | 4e = N | 5e = ^ | 6e = n | 7e = ~
|
0f = | 1f = | 2f = / | 3f = ? | 4f = O | 5f = _ | 6f = o | 7f = DEL
|
Question.24:

Is "i" a string literal or character literal?

Answer:

It is a string literal.

Question.25:

Is 'i' a string literal or character literal?

Answer:

It is a character literal.

Question.26:

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

Answer:

If the numeric value is in between 0 to 127, then we can assign


char value to the int value according to ASCII system.

Question.27:

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

Answer:

If the character is from the ASCII system, then we can assign int
value to char value according to the ASCII system.

Question.28:
What is printed by the following code fragment?
int x;
x = 'A';
std::cout << x << '\n';

Answer:

It prints int 65 acording to ASCII system, which is A’s value.

Question.29:

What is the difference between the character 'n' and the


character '\n'?

Answer:

'n' is just a character, whereas '\n' is used to end a line.

Question.30:

Write a C++ program that simply emits a beep sound when run.

Answer:
1. #include <iostream>
2. #include <windows.h> // WinApi header
3.
4. using namespace std;
5.
6. int main()
7. {
8. Beep(523,500); // 523 hertz (C5) for 500 milliseconds
9. cin.get(); // wait
10. }

Question.31:

Create an unscoped enumeration type that represents the days


of the week.
Answer:

1. #include <iostream>
2. using namespace std;
3.
4. // Define the enum data type Days
5. enum Days
6. {
7. monday, //monday = 0 by default
8. tuesday = 0,//tuesday = 0 also
9. wednesday, //wednesday = 1
10. thursday, //thursday = 2
11. friday, //and so on.
12. saturday,
13. sunday
14. };

15. int main(void)


16. {
17. // Declaring the enum variable
18. // Try changing the tuesday constant, recompile and re run
this program
19. enum Days WhatDay = tuesday;
20.
21. switch (WhatDay)
22. {
23. case 0: cout<<"It's Monday"<<endl;
24. break;
25. default: cout<<"Other day"<<endl;
26. }
27. return 0;
28. }
Output:
It's Monday

Question.35:

Determine the exact type of each of the following variables:


(a) auto a = 5;
(b) auto b = false;
(c) auto c = 9.3;
(d) auto d = 5.1f;
(e) auto e = 5L;
©

Answer:

(a) auto a = 5; int


(b) auto b = false; bool
(c) auto c = 9.3; double
(d) auto d = 5.1f; float
(e) auto e = 5L large int

CHAPTER NO 4
Q1: Is the literal 4 a valid C++ expression?

Answer:-
No,
An expression is the one which involves any arithmetic
operators. Since no operator has been used with 4, so it is an
invalid expression.

Q2. Is the variable x a valid C++ expression?

Answer:-
No,
An expression is the one which involves any arithmetic
operators. Since no operator has been used with x, so it is an
invalid expression.
Q3. Is x + 4 a valid C++ expression?

Answer:-
Yes, It is a valid expression.

Q4: What affect does the unary + operator have

when applied to a numeric expression?

Answer:-
It has no effect on a numeric expression.

Q5: Sort the following binary operators in order of high to low


precedence: +, -, *, /, %, =.

Answer:-
( *> / >% >+> - > = )

You might also like