Professional Documents
Culture Documents
5 Chapters Assignments
5 Chapters Assignments
Chapter no 1
Compiler is used to convert source code into machine code. We can say compiler
is a translator that process statements written in high-level-language and turns
these statements into machine language.
SOURCE CODE
COMPLIED CODE
4. What tool(s) does a programmer use to convert C++ source code into
executable machine code?
Editor
Preprocessor
Compiler
Linker
These tools are required to convert source code into object code.
Linker is a program that makes an executable file. Linker combines the compiled
code or pre-compiled library code from other sources to make a complete
executable program.
6. Does the linker deal with files containing source code or machine language
code?
A compiler takes the program code and converts the source code into machine
language module (called an object file). Another specialized program called linker.
Now Linker Combines this object file with other previously compiled object files
(in particular run time modules) to create an executable file.
The preprocessor is used to include some additional library file in source code
which is more elleborate for the compiler.
The visual studio and Eclipse are popular C++ IDE that is a fully featured IDE used
by programmers developing by Microsoft Windows.
11. Name a popular C++ IDE is used by programmers developing for Apple
macOS?
return 0;
}
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 names appear on the next line.
#include <iostream>
using namespace std;
int main(){
cout<<"Muhammad"<<endl<<"Mudassar";
return 0;
}
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?
Skiped by sir.
10. Can a single statement in C++ span multiple lines in the source
code?
Yes, we can do this with new line /n.
2. Will the following lines of code print the same thing? Explain why
or why not.
std::cout << x << '\n';
std::cout << "x" << '\n';
No it is does not print the same thing because 1st one is the character
and the second one is the strings.so it is not print the same value.
10 = x;
10. Once a variable has been properly declared and initialized can its
value be changed?
Yes, we can change the value if the value is not constant. Const is a key
word that is used to fix any variable’s value.
12. In C++ can you declare more than variable in the same declaration
statement? If so, how?
Yes, we can declare more than one variable in the same statement like
int x,y,z; etc.
4. What affect does the unary + operator have when applied to a numeric
expression?
We use the unary + operator just for completeness. When we applied the unary
operator to a numeric expression, there is no difference between original value and
the resulting value the resulting value same as its original value of its operand.
*, /, %
+,-
6. Write a C++ program that receives two integer values from the user. The
program then should print the
sum (addition), difference (subtraction), product (multiplication), quotient
(division), and remainder
after division (modulus). Your program must use only integers. Can you explain
the results it produces for all of these operations?
#include <iostream>
using namespace std;
int main()
{
int a,b,sum,substract,multiply,divided,modulus;
cout<<"enter first number";
cin>>a;
cout<<"enter second number";
cin>>b;
sum=a+b;
substract=a-b;
multiply=a*b;
divided=a/b;
modulus=a%b;
cout<<"the sum is = "<<sum<<endl;
cout<<"the substraction is = "<<substract<<endl;
cout<<"the multiplication is = "<<multiply<<endl;
cout<<"the division is = "<<divided<<endl;
cout<<"the modulus is = "<<modulus<<endl;
return 0;
}
7. Write a C++ program that receives two double-precision floating-point values
from the user. The
program then should print the sum (addition), difference (subtraction), product
(multiplication), and
quotient (division). Your program should use only integers. Can you explain the
results it produces for all these operations? What happens if you attempt to
compute the remainder after division (modulus) with double-precision floating-
point values?
#include<iostream>
using namespace std;
int main()
{
float x;
float y;
cout<<"Enter your first num = ";
cin>>x;
cout<<"Enter your second num =";
cin>>y;
cout<<x<<"+"<<y<<"="<<x+y<<endl;
cout<<x<<"-"<<y<<"="<<x-y<<endl;
cout<<x<<"*"<<y<<"="<<x*y<<endl;
cout<<x<<"/"<<y<<"="<<x/y<<endl;
return 0;
}
If we compute the remainder after division (modulus) with double-
precision floating-point values it will give us error.
(a) i1 + i2 =7
(b) i1 / i2 =0
(c) i2 / i1 =2
(d) i1 * i3 =-6
(e) d1 + d2 =7
(f) d1 / d2 =0.4
(g) d2 / d1 =2.5
(h) d3 * d1 =-1
(i) d1 + i2 =7.0
(j) i1 / d2 =0.4
(k) d2 / i1 =2.5
(l) i2 / d1 =2.5
(m) i1/i2*d1 =0.8
(n) d1*i1/i2 =0.8
(o) d1/d2*i1 =0.8
(p) i1*d1/d2 =0.8
(q) i2/i1*d1 =4
(r) d1*i2/i1 =5
(s) d2/d1*i1 =5
(t) i1*d2/d1 =5
19. Consider the following program which contains some errors. You may
assume that the comments
within the program accurately describe the program’s intended behavior .
#include <iostream>
using namespace std;
int main() {
int n1, n2, d1; // 1
// Get two numbers from the user
cin << n1 << n2; // 2
// Compute sum of the two numbers
cout << n1 + n2 << endl; // 3
// Compute average of the two numbers
cout << n1+n2/2 << endl; // 4
// Assign some variables
d1 = d2 = 0; // 5
// Compute a quotient
cout << n1/d1 << endl; // 6
// Compute a product
n1*n2 = d1; // 7
// Print result
cout << d1 << endl; // 8
}
For each line listed in the comments, indicate whether or not a compile-
time, run-time, or logic error is present. Not all lines contain an error.
Line number 6 gave the compile time error because where the operator <<
is used with cin it is wrong the correct operator is >>.
Line number 12 they gave a compile time error because d2 was not
declared.
Line number 16 is also gave us a compile time error because the value is
assigned from the right side while the value is assign from left side.
22. Write the shortest way to express each of the following statements.
(a) x = x + 1;
(b) x = x / 2;
(c) x = x - 1;
(d) x = x + y;
(e) x = x - (y + 7);
(f) x = 2*x;
(g) number_of_closed_cases = number_of_closed_cases + 2*ncc;
(a) x++
(b) x /= 2;
(c) x -=1
(d) x += y;
(e) x -= (y + 7);
(f) x *= 2;
(g) number_of_closed_cases += 2*ncc;
B. The program does not produce the intended result. We write cin after
the formula first we write the radius that we collect from user than we
write the formula
C. We need to change operators that used wrong in line 7 and 8 and then
correct the position of (from user taker) and the formula then program run
accurately.
Chapter no 5
Conditional Execution
Exercise 5.10
1.What values can a variable of type bool assume?
True(1)
False(0)
The value 16 will be TRUE because any non-zero integer will be taken as TRUE.
9. Express the following Boolean expressions in simpler form; that is, use fewer
operators. x is an int.
(a) !(x == 2) ans; (x!=2)
(b) x < 2 || x == 2 ans;(x<=2)
(c) !(x < y) ans;(x!<y)
(d) !(x <= y) ans;(x!<y && x!=y)
(e) x < 10 && x > 20 ans; (x<!=10 && x>!=20)
(f) x > 10 || x < 20 ans; (x>!10 || x<!=20)
(g) x != 0 ans; (x>0||x<0)
(h) x == 0 ans; (x!<0 && x!>o)
12. Write a C++ program that requests an integer value from the user. If the
value is between 1 and 100
inclusive, print “OK;” otherwise, do not print anything.
#include <iostream>
using namespace std;
int main()
{
int x;
cout<<"Enter a value Between 1 to 100 = ";
cin>>x;
if(x>=1 && x<=100)
{
cout<<"ok";
}
else {
}
return 0;
}
13. Write a C++ program that requests an integer value from the user. If the
value is between 1 and 100
inclusive, print “OK;” otherwise, print “Out of range.”
#include <iostream>
using namespace std;
int main()
{
int x;
cout<<"Enter a value Between 1 to100 = ";
cin>>x;
if(x>=1 && x<=100)
{
cout<<"OK";
}
else
{
cout<<”Out of range”;
}
return 0;
}
15. The following program attempts to print a message containing the English
word corresponding to a given integer input. For example, if the user enters the
value 3, the program should print
"You entered a three". In its current state, the program contains logic errors.
Locate the
problems and repair them so the program will work as expected.
#include <iostream>
using namespace std;
int main() {
cout << "Please in value in the range 1...5: ";
int value;
cin >> value;
// Translate number into its English word
if (value == 1)
{
cout << "You entered a";
cout << "one";
cout << endl;
}
else if (value == 2)
{
else if (value == 3)
{
else if (value == 4)
{
else if (value == 5)
{
return 0;
What will the code print if the variables i, j, and k have the following values?
(a) i is 3, j is 5, and k is 7
(i=5,j=5,k=7)
(b) i is 3, j is 7, and k is 5
(i=3, j=5, k=5)
(c) i is 5, j is 3, and k is 7
(i=7, j=3, k=7)
(d) i is 5, j is 7, and k is 3
(i=5, j=3, k=3)
(e) i is 7, j is 3, and k is 5
(i=5, j=3, k=5)
(f) i is 7, j is 5, and k is 3
(i=7, j=7, k=3)
17. Consider the following C++ program that prints one line of text:
#include <iostream>
using namespace std;
int main() {
int input;
cin >> input;
if (input < 10) {
if (input != 5)
cout << "wow ";
else
input++;
}
else {
if (input == 17)
input += 10;
else
cout << "whoa ";
}
cout << input << endl;
}
What will the program print if the user provides the following input?
(a) 3 (wow 3)
(b) 21 (whoa 21)
(c) 5 (6)
(d) 17 (27)
(e) -5 (wow -5)
18. Why does the following section of code always print "ByeHi"?
int x;
cin >> x;
if (x < 0);
cout << "Bye";
cout << "Hi" << endl;
Code print always "ByeHi" this is because any number we will enter here it will
be always greater than 0.
19. Write a C++ program that requests five integer values from the user. It then
prints the maximum and
minimum values entered. If the user enters the values 3, 2, 5, 0, and 1, the
program would indicate
that 5 is the maximum and 0 is the minimum. Your program should handle ties
properly; for example,
if the user enters 2, 4 2, 3 and 3, the program should report 2 as the minimum
and 4 as maximum.
#include<iostream>
using namespace std;
int main(){
int a,b,c,d,e;
cout<<"Enter ist number = ";
cin>>a;
cout<<"Enter 2nd number = ";
cin>>b;
cout<<"Enter 3rd number = ";
cin>>c;
cout<<"Enter 4th number = ";
cin>>d;
cout<<"Enter 5th number = ";
cin>>e;
return 0;
}
20. Write a C++ program that requests five integer values from the user. It then
prints one of two
things: if any of the values entered are duplicates, it prints "DUPLICATES";
otherwise, it prints
"ALL UNIQUE".
#include <iostream>
using namespace std;
int main()
{
int a,b,c,d,e;
cout<<"enter ist value";
cin>>a;
cout<<"enter 2nd value";
cin>>b;
cout<<"enter 3rd value";
cin>>c;
cout<<"enter 4th value";
cin>>d;
cout<<"enter 5th value";
cin>>e;
if (a!=b && a!=c && a!=d && a!=e && b!=a && b!=c && b!=d && b!=e &&
c!=a && c!=b && c!=d && c!=e && d!=a && d!=b && d!=c && d!=e && e!=a
&& e!=b && e!=c && e!=d ){
cout<<"its all unique";
}
else
cout<<"DOUBLECATE";
}