Mr.Lohani khan / 11A-5 / 597 /
COMP151 INTRODUCTION TO ALGORITHMS AND PROGRAMMING
ASSIGNMENT – II
STUDENT’S NAME: ______
STUDENT’S ID: ______
SECTION: ______
/College of Arts and Sciences
Department of Mathematical and Physical ScienceSection: Computer Science
Semester: spring 2014
1. Write a single C++ statement or line that accomplishes each of the following -5 Marks
a). Print the message “Enter two numbers”
b). Assign the product of variables b and c to variable a
c). Input three integer values from the keyboard into integer variables a, b and c
d). Print the size of short integer
e). Calculate
BMI= Weight in Pounds X 703
Height in inches X Height in inches
f). Print an Uppercase Z
g). Declare variables age, number and max as integer with max initialized to 45
h). Initialize the constant integer variable count to 50
i). Initialize the character variable ch to F
j) declare force variable and initialize with 76.3
2. Identify the following Literals - 6 Marks
a). 23ULb). 4567
c) 8.2345L
d). ‘c’
e).0xBB
f). 1234.786
g). -1234
h). 057
i). “WELCOME”
j). 4.78345E-5f
k).34567L
l). “Hai Ready to do the Work”
3. State which of the following are legal and which are illegal. If illegal, explain -6 Marks your answer
a). The following are all valid variable names: _under_bar, m928134, t5, j7, her_sales, his account total, a, b, c, z, z2
b). The statement cout<”a=5;”; is a typical example of an assignment statement
c). The following are invalid variable names 3g, 87, 67h2, h22, 2h
d). cout<45;
e). cin>88;
f). Variable is a location in the computer’s memory that may contain different values at various times throughout the execution of a program
g). char ‘ch’;
h). cout<’\n’;
i). cout<’G’;
j). cin>miles per litter;
k). x+y+z=v;
l). float main();
4. TRUE or FALSE - 6 Marks
a). A variable must be declared before it can be used
b). Variable names may be begin with a number
c). Variable names may be up to 31 characters long
d). A left brace (open brace) in a C++ program should always be followed by a right brace (close brace) later in program.
e).You will be unable to execute your program until you correct all the syntax errors in it
f). C++ language is not a case sensitive language
g).The #define directive is not a C++ statement but a directive for the pre-processor
h). Manipulators are operators used with the extraction operator > to manipulate the way of data display.
i).When new value placed into variable, overwrites the old value
j). Once the default (right justified) is changed with left alignment then all the setw(n) follows the left alignment
k). The keyword left should be given before setw(n).
l).We can not use integer data type variable to store characters
5. Write a program that displays the following pieces of information, each on a -3 Marks separate line :
Get the input for all of these items from the user and show the output
Your ID
Your Age
Your Sex
Your Telephone number
Your Height
Your Weight
Your ZIP code
Note: Use only a single cout statement with \n to display all of this information
6. A customer in a store purchasing five items. The prices are - 4 Marks
Price for Item1 = 5.5 OMR
Price for Item2 = 2.6 OMR
Price for Item3 = 7.8 OMR
Price for Item4 = 12.6 OMR
Price for Item5 = 10.0 OMR
Write a Program that holds the prices of the five items in five variables. Display each item’s price, the subtotal of the sale, the amount of sales tax, and total. Assume the sales tax is 6%.
7. Write a program to convert miles to kilometer. Take input as miles and convert it kilometre (output). - 3 Marks
1 mile= 1.6 km
8. A program has the following variable definitions. - 1 Mark
long miles;
int feet;
float inches;
Check the above statements are correct or not?
Write one cin statement that reads a value into each of these variables.
9. What does the following code print? - 1 Mark
cout<”\”Run, Spot, run, \” she said.”;
10. Does the following program calculate the correct values? If not give the reason.
#include<iostream> - 2 Marks
using namespace std;
int main()
{
short int var1=25000;
unsigned short int var2=25000;
unsigned short int var3=30000;
var1=(var1*5)/3;
var2=(var2*5)/3;
var3=(var3*5)/2;
cout<var1<endl;
cout<var2<endl;
cout<var3;
return(0);
}
11. Check the following code is correct or not? If it is correct write the output otherwise mention where it is having errors - 3 Marks
#include<iostream>using namespace std;
int main()
{
int count=8;
cout<count<'\n';
cout<count<'\t';
count=count-1;
cout<count<'\n';
cout<count<'\t';
count=count-1;
cout<count<'\t';
count=count-1;
cout<count<'\n';
cout<count<'\t';
count=count-1;
cout<count<'\t';
count=count-1;
cout<count<'\t';
count=count-1;
cout<count<endl;
return(0);
}
Output:
8