Uploaded by Michael Belete

Cover-merged (1)

advertisement
Admas Department Of CS
Introduction to Programming I
Assignment
Name: Michael Belete
ID: AD/0937/18
Submitted to : Getnet
Date: 09/08/2022
Addis Ababa Ethiopia,
2.1
Fill in the blanks in each of the following.
a. Every C++ program begins execution at the function
Answer : : Main
b. A(n)
begins the body of every function and a(n)
ends the body.
Answer :: Left brace({) , right brace(})
c. Every C++ statement ends with a(n)
.
Answer :: Semicolon ;
d.
The escape sequence \n represents the
of the next line on the screen.
character, which causes the cursor to position to the beginning
Answer :: \n new line
e.
The
statement is used to make decisions.
Answer :: if
2.2 State whether each of the following is true or false. If false, explain why. Assume the statement using std::cout; is
used
a. Comments cause the computer to print the text after the // on the screen when the program is executed.
Answer :: False
b. The escape sequence \n, when output with cout and the stream insertion operator, causes the cursor to position to
the beginning of the next line on the screen.
Answer :: True
c. All variables must be declared before they're used.
Answer :: True
d. All variables must be given a type when they're declared.
Answer :: True
e. C++ considers the variables number and NuMbEr to be identical.
Answer :: False Because Every C++ Program is Cas Sensitive
f.
Declarations can appear almost anywhere in the body of a C++ function.
Answer :: True
g. The modulus operator (%) can be used only with integer operands.
Answer :: True
h. The arithmetic operators *, /, %, + and - all have the same level of precedence.
Answer :: False
i.
A C++ program that prints three lines of output must contain three statements using cout and the stream
insertion operator.
Answer :: False
a.
Declare the variables c, thisIsAVariable, q76354 and number to be of type int.
Answer :: int c, thisIsAVariable, q76354, number
b. Prompt the user to enter an integer. End your prompting message with a colon (:) followed by a space and leave
the cursor positioned after the space.
cout<<”Enter an integer : ”;
c. Read an integer from the user at the keyboard and store it in integer variable age.
cin>>age;
d. If the variable number is not equal to 7, print "The variable number is not equal to 7".
If(number !=7)
{
Cout<<”The number is not equal to 7”
}
e. Print the message "This is a C++ program" on one line.
cout<<”This is a C++ program”
f.
Print the message "This is a C++ program" on two lines. End the first line with C++.
cout<<”This is a C++\n program”;
g. Print the message "This is a C++ program" with each word on a separate line.
cout<<”This\n is\n a\n C++\n program”;
h. Print the message "This is a C++ program". Separate each word from the next by a tab.
cout<<”This\t is\t a\t C++\t program\n”;
2.4
a.
State that a program calculates the product of three integers.
//This Program Will Calculate The Product Of Three Integers
b. Declare the variables x, y, z and result to be of type int (in separate statements).
Int x,y,z,result;
c. Prompt the user to enter three integers.
Cout<<”Please Enter Three Integers”;
d. Read three integers from the keyboard and store them in the variables x, y and z.
Cin>>x>>y>>z;
e. Compute the product of the three integers contained in variables x, y and z, and assign the result to the
variable result
result=x*y*z;.
f.
Print "The product is " followed by the value of the variable result.
Cout<<”The Product is ”<<result;
g.
Return a value from main indicating that the program terminated successfully.
Return 0;
2.5
// This Program Calculates the product of three integers
#include <iostream>
Using namesacestd;
int main()
{
Int x,y,z,Result;
Cout<<”Enter Three Integers”;
Cin>>x>>y>>z;
Result=x*y*z;
Cout<<”The Result is”<<Result;
Return 0;
}
2.6
Identify and correct the errors in each of the following statements (assume that the statement using std::cout; is used):
a. If(c<7);
Answer : : Error: Semicolon after the right parenthesis of the condition if(c<7); Correction Remove Semicolon
After The Right Bracket
b. cout << "c is less than 7\n";
Answer : : Space Between Cout ,<< and “
c. if ( c => 7 )
Answer : : Error: The relational operator => is Not Correct. So Change ‘=>’to “>=”.
d. Answer : : Space Between Cout ,<< and “
2.7
Discuss the meaning of each of the following objects :
a. std::cin
Answer :
(character input) The "c" in C++ cin refers to "character" and "in" means "input". Thus, cin means “character
input ". The C++ cin object belongs to the instream class. It accepts input from a standard input device, such as
a keyboard, and is linked to stdin, the regular C input source.
b. std::cout
Answer :
The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout
object is used along with the insertion operator << in order to display a stream of characters.
2.8
a. ________ are used to document a program and improve its readability.
Answer : : Comment
b. The object used to print information on the screen is ________.
Answer : : cout
c. A C++ statement that makes a decision is
Answer : : If
d. Most calculations are normally performed by _________ statements.
Answer : : Assignment
e. The ________ object inputs values from the keyboard.
Answer : : cin
2.9
Write a single C++ statement or line that accomplishes each of the following:
a. Print the message "Enter two numbers".
Cout<<”Enter two numbers”;
b. Assign the product of variables b and c to variable a.
a=b*c;
c. State that a program performs a payroll calculation (i.e., use text that helps to document a program).
//This Program Preforms a payroll calculation
d. Input three integer values from the keyboard into integer variables a, b and c
Cin>>a>>b>>c;
2.10
State which of the following are true and which are false. If false, explain your Answer :s.
a. C++ operators are evaluated from left to right.
Answer :
False :because all operators not evaluated from left to right some of the operator.
b. The following are all valid variable
names: _under_bar_, m928134, t5, j7, her_sales, his_account_total, a, b, c, z, z2.
Answer :
True :
c. The statement cout << "a = 5"; is a typical example of an assignment statement.
Answer :
False : because this statement will just print a = 5; to the screen. It does not perform any assignment in the program.
d. A valid C++ arithmetic expression with no parentheses is evaluated from left to right.
False : because it will be evaluated according to their precedence is evaluated from left to right.
e. The following are all invalid variable names: 3g, 87, 67h2, h22, 2h.
False : because h22 is a valid variable name
2.11
Fill in the blanks in each of the following:
a. What arithmetic operations are on the same level of precedence as multiplication? _______.
Answer :
Division and Modulus
b. When parentheses are nested, which set of parentheses is evaluated first in an arithmetic expression? ________.
Answer : : Innermost
c. A location in the computer's memory that may contain different values at various times throughout the execution
of a program is called a(n) ________.
Answer : : Variable
2.12
What, if anything, prints when each of the following C++ statements is performed? If nothing prints, then Answer :
"nothing." Assume x = 2 and y = 3.
a. cout << x;
Answer : : 2
b. cout << x + x;
Answer : : 4
c. cout << "x=" ;
Answer : : 2=
d. cout << "x = " << x;
Answer : : X=2
e. cout << x + y << " = " << y + x;
Answer : : 5=5
f.
z = x + y;
Answer : : Nothing Displayed
g. cin >> x >> y;
Nothing Displayed
h. // cout << "x + y = " << x + y;
Answer : : Nothing Displayed Because It’s a Comment
i.
cout << "\n" ;
Answer :: The output will be Displayed in Newline
2.13
Which of the following C++ statements contain variables whose values are replaced?
a. cin >> b >> c >> d >> e >> f;
Answer : : a all variables are can be replaced
b. p = i + j + k + 7;
Answer : : B all variables are can be replaced
c. cout << "variables whose values are replaced" ;
d. cout << "a = 5" ;
2.14
Given the algebraic equation y = ax3 + 7, which of the following, if any, are correct C++ statements for this equation?
a. y = a * x * x * x + 7 ;
Answer : : Correct
b. y = a * x * x * ( x + 7 );
Answer : : Incorrect
c. y = ( a * x ) * x * ( x + 7 );
Answer : : Incorrect
d. y = (a * x) * x * x + 7 ;
Answer : : Correct
e. y = a * ( x * x * x ) + 7 ;
Answer : : Correct
f.
y = a * x * ( x * x + 7 );
Answer : : Incorrect
2.15
(Order of Evolution) State the order of evaluation of the operators in each of the following C++ statements and show
the value of x after each statement is performed.
a. x = 7 + 3 * 6 / 2 - 1 ;
Answer : :
➢
➢
➢
➢
3*6=18
18/2
9+7
16-1
b. x = 2 % 2 + 2 * 2 - 2 / 2 ;
Answer : :
➢
➢
➢
➢
➢
➢
2%2
2*2
2/2
0+4
4-1
X=3
c. x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );
Answer : :
➢
➢
➢
➢
➢
9*3=27
27/3=9
3+9=12
27*12=324
X=324
2.16
(Arithmetic) Write a program that asks the user to enter two numbers obtains the two numbers from the user and prints
the sum, product, difference, and quotient of the two numbers
(Arithmetic) Write a program that asks the user to enter two numbers obtains the two numbers from the user and prints
the sum, product, difference, and quotient of the two numbers.
Answer :
include <iostream>
using namespace std;
int main()
{
int x, y, sum, product, difference, quotient, remainder;
cout << " please enter two numbers \n" << endl;
cin>>x>>y;
sum = x + y;
cout<<" the sum of and is \n"<<sum<< endl;
product = x * y;
cout<<"the product is \n"<<product<< endl;
difference = x - y;
cout<<"the difference of and is \n"<<difference<< endl;
quotient = x / y;
cout<<"the quotient is \n"<<quotient<< endl;
remainder = x % y;
cout<<"the remainder of and is \n"<<remainder<< endl;
return 0;
}
2.17
(Printing) Write a program that prints the numbers 1 to 4 on the same line with each pair of adjacent numbers separated
by one space. Do this several ways:
a. Using one statement with one stream insertion operator.
b. Using one statement with four stream insertion operators
c. Using four statements.
Answer :
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
cout<<"1 2 3 4"<<endl;
cout<<"1 "<<"2 "<<"3 "<<"4 "<<endl;
cout<<"1 ";
cout<<"2 ";
cout<<"3 ";
cout<<"4 "<<endl;
return 0;
}
2.18
(Comparing Integers) Write a program that asks the user to enter two integers, obtains the numbers from the user, then
prints the larger number followed by the words "is larger." If the numbers are equal, print the message "These numbers
are equal."
Answer : :
#include <iostream>
using namespace std;
int main()
{
int integer1, integer2;
cout << "pleas enter two integers.\n";
cin >>integer1>>integer2;
if ( integer1 > integer2)
{
cout <<" is larger.\n"<<integer1<<endl;
}
if ( integer2 > integer1)
{
cout <<" is larger.\n"<<integer2<<endl;
}
if ( integer1 == integer2)
{
cout <<" these numbers are equal.\n";
}
return 0;
}
2.19
(Arithmetic, Smallest and Largest) Write a program that inputs three integers from the keyboard and prints the sum,
average, product, smallest and largest of these numbers. The screen dialog should appear as follows:
Input three different integers: 13 27 14
Sum is 54
Average is 18
Product is 4914
Smallest is 13
Largest is 27
Answer :
//program arithmetic and comparing three numbers
#include <iostream>
using namespace std ;
int main () {
int x(0) , y(0) , z(0) , min(0) , max(0) ;
cout << " Input three different integers: \n" ;
cin >> x >> y >> z ;
//sum
cout << " sum is " << x+y+z << endl;
//average
cout << " Average is " << (x+y+z)/3 <<endl ;
//multiplicatio
cout << " Product is " << x * y * z <<endl ;
//The smallest
min = x ;
if (y<min)
{
min =y ;
}
if(z<min)
{
min = z ;
}
cout << " Smallest is " << min <<endl;
//The largest
max = x ;
if (y>max)
{
max = y ;
}
if (z>max)
{
max = z ;
}
cout << " Largest is " << max << endl ;
}
2.20
(Diameter, Circumference and Area of a Circle) Write a program that reads in the radius of a circle as an integer and
prints the circle's diameter, circumference and area. Use the constant value 3.14159 for p. Do all calculations in output
statements. [Note: In this chapter, we've discussed only integer constants and variables. In Chapter 4 we discuss
floating-point numbers, i.e., values that can have decimal points.]
Answer :
//this Program Accepts Radius from he user and calculates Diameter,Circumfrence and area of a circle
#include <iostream>
using namespace std;
int main()
{
const float PI=3.14;
int radius;
cout<<"Enter circle's radius"<<endl;
cin>>radius;
cout<<"Diameter: "<<2*radius<<endl;
cout<<"Circumference: "<<2*PI*radius<<endl;
cout<<"Area: "<<PI*radius*radius<<endl;
}
2.21
(Displaying Shapes with Asterisks) Write a program that prints a box, an oval, an arrow and a diamond as follows:
*********
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*********
***
*
*
*
*
*
*
*
***
*
*
***
**
*****
* *
*
* *
*
*
*
*
* *
*
* *
*
**
*
*
Answer :
#include <iostream>
using namespace std;
int main()
{
cout<<"**********"<<" "<<" *** "<<" "<<" * "<<" "<<"
*"<<endl;
cout<<"*
*"<<" "<<" * * "<<" "<<" *** "<<" "<<" * *"<<endl;
cout<<"*
*"<<" "<<"*
*"<<" "<<"*****"<<" "<<" * *"<<endl;
cout<<"*
*"<<" "<<"*
*"<<" "<<" * "<<" "<<" *
*"<<endl;
cout<<"*
*"<<" "<<"*
*"<<" "<<" * "<<" "<<"*
*"<<endl;
cout<<"*
*"<<" "<<"*
*"<<" "<<" * "<<" "<<" *
*"<<endl;
cout<<"*
*"<<" "<<"*
*"<<" "<<" * "<<" "<<" * *"<<endl;
cout<<"*
*"<<" "<<" * * "<<" "<<" * "<<" "<<" * *"<<endl;
cout<<"**********"<<" "<<" *** "<<" "<<" * "<<" "<<"
return 0;
}
*"<<endl;
2.22
What does the following code print?
cout << "*\n**\n***\n****\n*****" << endl;
Answer :
2.23
(Largest and Smallest Integers) Write a program that reads in five integers and determines and prints the largest and the
smallest integers in the group. Use only the programming techniques you learned in this chapter.
Answer :
#include <iostream>
using namespace std ;
int main ()
{
int a(0),b(0),c(0),d(0),e(0) , min(0) , max(0);
cout <<"Enter five numbers : " ;
cin>> a>>b>>c>>d>>e ;
min =a ;
if (b<min)
{
min=b ;
}
if (d<min)
{
min =d ;
}
if (c<min)
{
min =c ;
}
if (e<min)
{
min = e;
}
cout << " The smallest is : " << min <<endl;
max = a ;
if (b>max)
{
max=b;
}
if (c>max)
{
max =c ;
}
if (d>max)
{
max = d ;
}
if (e>max)
{
max = e ;
}
cout << " The lagest is : " << max << endl ;
}
2.24
(Odd or Even) Write a program that reads an integer and determines and prints whether it's odd or even. [Hint: Use the
modulus operator. An even number is a multiple of two. Any multiple of two leaves a remainder of zero when divided
by 2.]
Answer :
#include <iostream>
using namespace std;
int main()
{
int x;
cout<<"Enter an integer"<<endl;
cin>>x;
if(x%2==0)
cout<<"The Number is Even"<<endl;
else
cout<<"The Number is Odd";
}
2.25
(Multiples) Write a program that reads in two integers and determines and prints if the first is a multiple of the second.
[Hint: Use the modulus operator.]
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int num1, num2;
cout<<"Enter two integers:";
cin>>num1>>num2;
if(num1%num2==0)
cout<<num1<<" is a multiple of the "<<num2<<endl;
if(num1%num2!=0)
cout<<num1<<" is not a multiple of the "<<num2<<endl;
return 0;
}
2.26
(Checkerboard Pattern) Display the following checkerboard pattern with eight output statements, then display the same
pattern using as few statements as possible.
// This Program Will Display Checkboard Pattern
#include <iostream>
using namespace std;
int main()
{
cout << "* * * * * * * *"<<endl;
cout << " * * * * * * * *"<<endl;
cout << "* * * * * * * *"<<endl;
cout << " * * * * * * * *"<<endl;
cout << "* * * * * * * *"<<endl;
cout << " * * * * * * * *"<<endl;
cout << "* * * * * * * *"<<endl;
cout << " * * * * * * * *"<<endl;
return 0;
}
2.27
(Integer Equivalent of a Character) Here is a peek ahead. In this chapter you learned about integers and the type int.
C++ can also represent uppercase letters, lowercase letters and a considerable variety of special symbols. C++ uses
small integers internally to represent each different character. The set of characters a computer uses and the
corresponding integer representations for those characters are called that computer's character set. You can print a
character by enclosing that character in single quotes, as with
cout << 'A'; // print an uppercase A
You can print the integer equivalent of a character using static_cast as follows:
cout << static_cast< int >( 'A' ); // print 'A' as an integer
This is called a cast operation (we formally introduce casts in Chapter 4). When the preceding statement executes, it
prints the value 65 (on systems that use the ASCII character set). Write a program that prints the integer equivalent of a
character typed at the keyboard. Store the input in a variable of type char. Test your program several times using
uppercase letters, lowercase letters, digits and special characters (like $).
Answer :
// this program changes the alphabet characters value to Unicode
#include <iostream>
using namespace std;
int main ()
{
cout << 'A' << " = " << static_cast< int >( 'A' ) << endl
<< 'B' << " = " << static_cast< int >( 'B' ) << endl
<< 'C' << " = " << static_cast< int >( 'C' ) << endl
<< 'a' << " = " << static_cast< int >( 'a' ) << endl
<< 'b' << " = " << static_cast< int >( 'b' ) << endl
<< 'c' << " = " << static_cast< int >( 'c' ) << endl
<< '0' << " = " << static_cast< int >( '0' ) << endl
<< '1' << " = " << static_cast< int >( '1' ) << endl
<< '2' << " = " << static_cast< int >( '2' ) << endl
<< '$' << " = " << static_cast< int >( '$' ) << endl
<< '*' << " = " << static_cast< int >( '*' ) << endl
<< '+' << " = " << static_cast< int >( '+' ) << endl
<< '/' << " = " << static_cast< int >( '/' ) << endl
<< ' ' << " = " << static_cast< int >( ' ' ) << endl;
return 0;
}
2.28
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int num;
cout<<"Enter a 5-digit number:";
cin>>num;
cout<<num/10000<<" ";
num=num%10000;
cout<<num/1000<<" ";
num=num%1000;
cout<<num/100<<" ";
num=num%100;
cout<<num/10<<" ";
num=num%10;
cout<<num<<endl;
return 0;
}
2.29
(Table) Using the techniques of this chapter, write a program that calculates the squares and cubes of the integers from 0 to 10. Use tabs
to print the following neatly formatted table of values:
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"integer"<<"\t"<<"square"<<"\t"<<"cube"<<endl;
num=0;
cout<<num<<"\t"<<num*num<<"\t"<<num*num*num<<endl;
num=1;
cout<<num<<"\t"<<num*num<<"\t"<<num*num*num<<endl;
num=2;
cout<<num<<"\t"<<num*num<<"\t"<<num*num*num<<endl;
num=3;
cout<<num<<"\t"<<num*num<<"\t"<<num*num*num<<endl;
num=4;
cout<<num<<"\t"<<num*num<<"\t"<<num*num*num<<endl;
num=5;
cout<<num<<"\t"<<num*num<<"\t"<<num*num*num<<endl;
num=6;
cout<<num<<"\t"<<num*num<<"\t"<<num*num*num<<endl;
num=7;
cout<<num<<"\t"<<num*num<<"\t"<<num*num*num<<endl;
num=8;
cout<<num<<"\t"<<num*num<<"\t"<<num*num*num<<endl;
num=9;
cout<<num<<"\t"<<num*num<<"\t"<<num*num*num<<endl;
num=10;
cout<<num<<"\t"<<num*num<<"\t"<<num*num*num<<endl;
return 0;
}
2.30
//this program will calculate the sum of two numbers
#include <iostream>
using namespace std;
int main()
{
int no1,no2,sum;
cout<<"enter two integers"<<endl;
cin>>no1>>no2;
sum=no1+no2;
cout<<"the sum of the two numbers is : "<<sum;
return 0;
}
2.31
//this program will calculate the subtraction of two numbers
#include <iostream>
using namespace std;
int main()
{
int no1,no2,sub;
cout<<"enter two integers"<<endl;
cin>>no1>>no2;
sum=no1-no2;
cout<<"the sum of the two numbers is : "<<sub;
return 0;
}
2.32
//this program will calculate the Product of two numbers
#include <iostream>
using namespace std;
int main()
{
int no1,no2,product;
cout<<"enter two integers"<<endl;
cin>>no1>>no2;
sum=no1*no2;
cout<<"the sum of the two numbers is : "<<product;
return 0;
}
2.34
//this program will calculate the Division of two numbers
#include <iostream>
using namespace std;
int main()
{
int no1,no2,sub;
cout<<"enter two integers"<<endl;
cin>>no1>>no2;
sum=no2-no2;
cout<<"the sum of the two numbers is : "<<sub;
return 0;
}
Download