SlideShare a Scribd company logo
1 of 18
Download to read offline
INTERNATIONAL ISLAMIC UNIVERSITY MALAYSIA
MID-TERM EXAMINATION
SEMESTER 2, 2014/2015 SESSION
KULLIYYAH OF INFORMATION AND COMMUNICATION TECHNOLOGY
Programme : BIT Level of Study : Undergraduate
Time : 10.00 a.m. – 12.15 p.m Date : 13th
March 2015
Duration : 2 Hours and 15 Minutes
Course Code : CSC1100 Section(s) : All
Course Title : Elements of Programming
This Question Paper consists of 17 Printed Pages including cover page containing 4 sections
Section 1: Multiple-Choice (10 questions)
Section 2: Fill-in-the-blank (10 Questions)
Section 3: Short Answers (3 Questions)
Section 4: Programming (1 Question)
You are required to answer ALL questions.
You are allowed to bring a calculator into the exam hall.
INSTRUCTION(S) TO CANDIDATES
DO NOT OPEN UNTIL YOU ARE ASKED TO DO SO
ANSWER ALL QUESTIONS IN THIS QUESTION PAPER
YOU ARE ONLY ALLOWED TO ASK QUESTIONS DURING THE FIRST 15
MINUTES OF THE EXAMINATION PERIOD
Any form of cheating or attempt to cheat is a serious offence, which may lead to dismissal.
Name:
Matric No:
Section:
Lecturer’s Name:
Page 2 of 16
SECTION 1: Multiple-Choice
Circle the correct answer in this question paper [1 marks x 10 = 10 marks].
1. Which of the following is NOT TRUE about C++ ?
a. C++ is an extension of the C language
b. C++ is a low-level programming language
c. C++ is an object-oriented language
d. C++ is a high-level programming language
2. When a declaration statement is used to store a value in a variable, the variable is said to
be ______.
a. created c. initialized
b. declared d. referenced
3. The C++ statement ‘cout << (6 + 15);’ yields the result ____.
a. 6 + 15 c. (6 + 15)
b. 21 d. (21)
4. A data ____ is defined as a set of values and a set of operations that can be applied to
these values
a. type c. base
b. set d. dictionary
5. What will be the value of the variable n4 after running the following codes?
int main ()
{
int n1 = 3, n2 = 4, n4;
double n3 = 2.2;
n4 = (n1/n2) * n3 + abs(n1-n2);
cout << double(n4) << endl;
return 0;
}
a. 2.65 c. 1
b. 0.65 d. 0
Page 3 of 16
6. The use of default in a switch-case statement is to _______
a. exit from the program
b. exit from the case structure
c. cover unhandled possibilities
d. continue with the remaining case conditions
7. Which of the following is a valid call to a C++ cmath function?
a. sqr(9.0)
b. ab(-y)
c. exp(2.5)
d. power(x,2)
8. What will be the output of the following code?
int main ()
{
int num = -5;
bool sign = false;
for(int i = 1; i <= 4; i++)
{
if (sign && num > 0) {
cout << ++num << endl;
break;
}
else if (!sign || num < 0) {
cout << --num ;
continue;
}
else
cout << num++ ;
}
return 0;
}
a. -4 c. -5-4-3-2
b. -5-6-7-8 d. -4-3-2-1
9. If an expression is false to begin with, then !expression is true and evaluates to a
value of ____.
a. 0 c. 2
b. 1 d. 3
Page 4 of 16
10. Using nested if statements without including _______ to indicate the structure is a
common programming error.
a. parentheses c. braces
b. quotation marks d. switches
SECTION 2 : Fill-in-the-blank
Choose the correct term from the box given below and complete each of the following
statement. Write your answers in the blank column next to each statement .
[1 marks x 10 = 10 marks]
namespace main( ) robust compound pre-processor
sentinel pseudocode relational compiler
loader class algorithm sizeof( )
i. _______ is the file accessed by compiler when looking
for prewritten classes or functions
pre-processor
ii. A _______program detects and responds effectively to
unexpected user inputs.
robust
iii. The ______ operator determines the amount of storage
reserved for a variable.
sizeof( )
iv. An _______ is a procedure or step by step sequence of
instructions to perform a computation in a program
algorithm
v. A _______ places programs into main memory for
execution.
loader
vi. In programming, a _______ is a special value that can be
used as a flag or signal to start or end a series of data.
sentinel
vii. One or more individual statements enclosed within
braces is also known as a ______ statement.
compound
viii. _________________ expressions are used to compare
operands.
relational
ix. In object-oriented programming, a _______ is a set of
objects that share similar attributes.
class
x. _______ forces the conversion of a value to another type casting
Page 5 of 16
SECTION 3: Short Answer Questions
Answer all questions in the spaces provided.
[40 marks]
Question 1: Short Answers
i. A source program, cannot be executed until it is translated into machine language. Draw
a diagram that demonstrates the process of program translation.
[2 marks]
ii. List down two important rules for evaluating arithmetic expressions.
[2 marks]
Both operands are integers: result is integer
One operand is floating-point: result is floating-point
iii. What is a symbolic constant? Provide a C++ example declaration of a symbolic constant
of type double.
[2 marks]
Symbolic constants are identifiers whose values cannot be changed throughout the
program.
Example:
const PI = 3.142 OR
#define PI 3.142
iv. Describe the meaning of an ‘infinite loop’ and a ‘fixed count loop’ in repetition.
[2 marks]
- An infinite loop is a loop with an expression that is always true and never ends.
- A fixed count loop is a loop with a tested expression which is a counter that
checks for a fixed number of repetitions.
Page 6 of 16
v. Explain with examples the three (3) types of logical operators in C++.
[5 marks]
 AND operator, &&:
 Compound condition is true (has value of 1) only if both conditions are true
 Example: (age > 40) && (term < 10)
 OR operator, || (double vertical bar):
 Compound condition is true if either one of the expressions is true or if both
conditions are true
 Example: (age > 40) || (term < 10)
 NOT operator, !:
 Changes an expression to its opposite state
 If expression is true, then !expression is false
Question 2: Output Identification
Determine the outputs for the following code segments (assume all necessary predecessor
commands have been defined).
i. For the following codes, assume that the number entered is 42339.
[5 marks]
int main()
{
int num;
cout << "Enter a five-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;
}
Page 7 of 16
ii. What is the printout of the following switch-case statement?
[3 marks]
int main()
{
int val = 22;
switch(val%3)
{
case 2: cout<< "Good, ";
break;
case 1: cout<< "Better, ";
case 0: cout<< "Best, ";
default: cout<< "Thank you!";
}
return 0;
}
4 2 3 3 9
Output:
Better, Best, Thank you!
Output:
Page 8 of 16
i. What is the output for the following codes?
[3 marks]
int main()
{
int x = 9, y=11;
if ( x < 10 )
if ( y > 10 )
cout << "%%%%" << endl;
else
cout << "#####" << endl;
cout << "$$$$$" << endl;
return 0;
}
ii. Determine the output of the following codes.
[4 marks]
int main()
{
int x, y;
for(x = 5; x >= 1; x--)
{
for(y = 6; y > x; y--)
{
cout << x + y;
}
cout << char('a' + 2) << endl ;
}
return 0;
}
%%%%
$$$$$
Output:
Page 9 of 16
Question 3: Find and correct the error(s) in the following programming statements:
i.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
int main ()
{
int a = 1; b = 2;
double c;
c = (a + b) * 2 + (a - b)/2;
cout >> c >> endl;
return 0;
}
[3 marks]
11c
109c
987c
8765c
76543c
Output:
Line No. Correction
5 int a=1, b=2;
9 cout << c << endl;
Page 10 of 16
ii.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
int a, b, c;
cout << "Enter 3 numbers: ";
cin >> a >> b >> c;
if ( c * c = a * a + b * b )
cout << The are 3 numbers
<< endl;
if else
cout << "THERE ARE 3 NUMBERS"
<< endl;
return 0;
}
[3 marks]
iii.
1 #include <iostream>
2
3 using namespace std;
4 int main()
5 {
6 float average;
7 average = 45/2.0;
8 cout << setw[6] << setfill('*') << average << endl;
9
10 if (average = 22.5)
11 cout << setiosflags(ios::showpoint) << average
12 << endl;
13 cout << setprecision(2) << average << endl;
14 return 0;
15 }
Line No. Correction
6 if ( c * c == a * a + b * b )
7 cout << "The are 3 numbers"
9 else
Page 11 of 16
[6 marks]
SECTION 4: Programming Question
Answer all questions in the spaces provided.
[25 marks]
Question 1:
In Japan, vending machines are not only used to buy drinks or snacks, but also to buy books or
magazines. You are required to create a C++ program that acts as vending machine that is able
to dispense novels, magazines and newspapers. Each category of item consists of 3
different titles to be represented by any characters between a-z (ie., name the titles as ‘a’, ‘b’,
‘c’, etc….). Given that the initial number of items in all categories is 9 units (3 units for each
item), use the ‘while’ and ‘switch-case statements to perform the following:
a. Draw a simple flow chart for your program (refer to the relevant symbols in the
attachment)
b. Your program should prompt the user to enter the amount of items they want to
purchase (e.g, entering 5 means user can buy 5 items in any combination of categories)
and display the titles selected (you may just use a cout for titles).
c. Your program should calculate the total price to be paid for all items purchased by
the user at the end of the program.
Line No. Correction
2 #include <iomanip>
6 cout << setw(6) << setfill('*') << average << endl;
10 if (average == 22.5) {
13 cout << fixed << setprecision(2) << average << endl; }
Page 12 of 16
d. Once an item in each category is purchased, the number of items will decrease by one.
Your program should be able to print the balance of each category of item at the end of
the program.
The following is the sample output of your program:
Please enter number of items: 2
Item 1
Choose the type of item (1 – novel, 2 – magazine, 3 – newspaper) : 1
Enter title : ‘a’
You have selected the novel “In the eyes of the night”
The price of this item is RM 10.
Item 2
Choose the type of item (1 – novel, 2 – magazine, 3 – newspaper) : 3
Enter title : ‘z’
You have selected the newspaper “The New Straits Time”
The price of this item is RM 2.50.
Total price of all items is RM 12.50
Balance for novel is 2
Balance for newspaper is 2
Press any key to continue . . .
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Page 13 of 16
Flowchart (Simplified)
Start
Input
numItem
Compute
balance = balance-1
count < numItem
Input
category
category is
1 or 2 or 3
title between
‘a’-‘z’
Input title
Compute
totalPrice = totalPrice + price
End
Display totalPrice,
balance
true
false
false
false
true
true
Display
title, price
Page 14 of 16
//Solution Midterm Exam (S4)
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int limit,count;
bool select1=false,select2=false,select3=false;
double totalp = 0.0,p_novel,p_mag,p_news,priceCat = 0.0;
int balance1 = 3, balance2 = 3, balance3 = 3;
int category;
char title;
cout << "************************************************" << endl;
cout << "Books & Magazines Vending Machinen " << endl;
cout << "Please enter the number of items you wish to purchase: " ;
cin >> limit;
count = 1;
while (count <=limit)
{
cout << "nItem " << count << endl;
cout << "Please enter the category of item to purchasen" ;
cout << "Press '1' for novelsn" ;
cout << "Press '2' for magazinesn" ;
cout << "Press '3' for newspapersnn" ;
cin >> category;
switch(category)
{
case 1:
cout << "You have selected 1 for novelsn";
cout << "Select 'a' for : In the eyes of the nightn";
cout << "Select 'b' for : I love Programming, not!n";
cout << "Select 'c' for : Exam, exam, go away...n";
cin >> title;
select1 = true;
switch(title)
{
case 'a': case 'A':
p_novel = 10.00;
cout << "You have selected the novel 'In the eyes "
<< "of the night'n";
cout << "The price of the novel is RM " << fixed
<< setprecision(2) << p_novel << endl;
break;
case 'b': case 'B':
p_novel = 12.00;
cout << "You have selected the novel 'I love "
<< "Programming, not!' " << endl;
Page 15 of 16
cout << "The price of the novel is RM " << fixed
<< setprecision(2) << p_novel << endl;
break;
case 'c': case 'C':
p_novel = 8.50;
cout << "You have selected the novel 'Exam, exam, "
<< "go away...' " << endl;
cout << "The price of the novel is RM " << fixed
<< setprecision(2) << p_novel << endl;
break;
default:
cout << "Wrong selection, please try again..."
<< endl;
continue;
}//inner switch1
balance1 = balance1 - 1 ;
priceCat = p_novel;
break;
case 2:
cout << "You have selected 2 for magazinesn";
cout << "Select 'j' for : Home Improvementn";
cout << "Select 'k' for : Cooking with Madamn";
cout << "Select 'l' for : Programming Bits and Bytesn";
cin >> title;
select2 = true;
switch(title)
{
case 'j': case 'J':
p_mag = 5.00;
cout << "You have selected the magazine 'Home "
<< "Improvement' " << endl;
cout << "The price of the magazine is RM "
<< fixed << setprecision(2) << p_mag << endl;
break;
case 'k': case 'K':
p_mag = 7.00;
cout << "You have selected the magazine 'Cooking "
<< "with Madam' " << endl;
cout << "The price of the magazine is RM "
<< fixed << setprecision(2) << p_mag << endl;
break;
case 'l': case 'L':
p_mag = 6.50;
cout << "You have selected the magazine "
<< "Programming Bits and Bytes' " << endl;
Page 16 of 16
cout << "The price of the magazine is RM "
<< fixed << setprecision(2) << p_mag << endl;
break;
default:
cout << "Wrong selection, please try again..."
<< endl;
continue;
} //inner switch2
balance2 = balance2 - 1;
priceCat = p_mag;
break;
case 3:
cout << "You have selected 3 for newspapersn";
cout << "Select 'x' for : Berita Hariann";
cout << "Select 'y' for : New Straits Timen";
cout << "Select 'z' for : Utusan Malaysian";
cin >> title;
select3 = true;
switch(title)
{
case 'x': case 'X':
p_news = 1.50;
cout << "You have selected the newspaper 'Berita "
<< "Harian' " << endl;
cout << "The price of the newspaper is RM "
<< fixed << setprecision(2) << p_news
<< endl;
break;
case 'y': case 'Y':
p_news = 2.00;
cout << "You have selected the newspaper 'The New "
<< "Straits Time' " << endl;
cout << "The price of the newspaper is RM "
<< fixed << setprecision(2) << p_news
<< endl;
break;
case 'z': case 'Z':
p_news = 1.80;
cout << "You have selected the newspaper 'Utusan "
<< "Malaysia' " << endl;
cout << "The price of the newspaper is RM "
<< fixed << setprecision(2) << p_news << endl;
break;
default:
cout << "Wrong selection, please try again..."
<< endl;
Page 17 of 16
continue;
} //inner switch 3
balance3 = balance3 - 1;
priceCat = p_news;
break;
default:
cout << "Wrong selection, please try again..." << endl;
continue;
} //outer switch
totalp = totalp + priceCat;
count++;
} // end while
cout << "nThe total price for your items is: " << fixed
<< setprecision(2) << totalp << endl ;
if (select1 == true)
cout << "Balance for novels is now: " << balance1 << endl;
if (select2 == true)
cout << "Balance for magazines is now: " << balance2 << endl;
if (select3 == true)
cout << "Balance for newspapers is now: " << balance3 << endl;
cout << endl;
return 0;
}
- End of question –
Page 18 of 16
Attachment (Symbols for flow chart)

More Related Content

What's hot

Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 InheritanceAmrit Kaur
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Languagesatvirsandhu9
 
Java Data Types
Java Data TypesJava Data Types
Java Data TypesSpotle.ai
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#Doncho Minkov
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language Mohamed Loey
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab ManualAkhilaaReddy
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template LibraryKumar Gaurav
 
FINAL PAPER FP301 OBJECT ORIENTED PROGRAMMING
FINAL PAPER FP301 OBJECT ORIENTED PROGRAMMINGFINAL PAPER FP301 OBJECT ORIENTED PROGRAMMING
FINAL PAPER FP301 OBJECT ORIENTED PROGRAMMINGAmira Dolce Farhana
 

What's hot (20)

Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 Inheritance
 
FP 301 OOP FINAL PAPER
FP 301 OOP FINAL PAPER FP 301 OOP FINAL PAPER
FP 301 OOP FINAL PAPER
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
C fundamental
C fundamentalC fundamental
C fundamental
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
FINAL PAPER FP301 OBJECT ORIENTED PROGRAMMING
FINAL PAPER FP301 OBJECT ORIENTED PROGRAMMINGFINAL PAPER FP301 OBJECT ORIENTED PROGRAMMING
FINAL PAPER FP301 OBJECT ORIENTED PROGRAMMING
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 

Similar to Mid term sem 2 1415 sol

Bis 311 final examination answers
Bis 311 final examination answersBis 311 final examination answers
Bis 311 final examination answersRandalHoffman
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201rohassanie
 
Name _______________________________ Class time __________.docx
Name _______________________________    Class time __________.docxName _______________________________    Class time __________.docx
Name _______________________________ Class time __________.docxrosemarybdodson23141
 
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...RSathyaPriyaCSEKIOT
 
Introduction to C++ lecture ************
Introduction to C++ lecture ************Introduction to C++ lecture ************
Introduction to C++ lecture ************Emad Helal
 
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...Alpro
 
Spring 2014 CSCI 111 Final exam of 1 61. (2 points) Fl.docx
Spring 2014 CSCI 111 Final exam   of 1 61. (2 points) Fl.docxSpring 2014 CSCI 111 Final exam   of 1 61. (2 points) Fl.docx
Spring 2014 CSCI 111 Final exam of 1 61. (2 points) Fl.docxrafbolet0
 
Cmis 102 Effective Communication / snaptutorial.com
Cmis 102  Effective Communication / snaptutorial.comCmis 102  Effective Communication / snaptutorial.com
Cmis 102 Effective Communication / snaptutorial.comHarrisGeorg12
 
C chap02
C chap02C chap02
C chap02Kamran
 
Cmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.comCmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.comStephenson22
 
Cmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.comCmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.comWilliamsTaylorza48
 

Similar to Mid term sem 2 1415 sol (20)

Bis 311 final examination answers
Bis 311 final examination answersBis 311 final examination answers
Bis 311 final examination answers
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201
 
Name _______________________________ Class time __________.docx
Name _______________________________    Class time __________.docxName _______________________________    Class time __________.docx
Name _______________________________ Class time __________.docx
 
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
 
LMmanual.pdf
LMmanual.pdfLMmanual.pdf
LMmanual.pdf
 
Introduction to C++ lecture ************
Introduction to C++ lecture ************Introduction to C++ lecture ************
Introduction to C++ lecture ************
 
Cp manual final
Cp manual finalCp manual final
Cp manual final
 
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
 
Spring 2014 CSCI 111 Final exam of 1 61. (2 points) Fl.docx
Spring 2014 CSCI 111 Final exam   of 1 61. (2 points) Fl.docxSpring 2014 CSCI 111 Final exam   of 1 61. (2 points) Fl.docx
Spring 2014 CSCI 111 Final exam of 1 61. (2 points) Fl.docx
 
Intro
IntroIntro
Intro
 
Unit-IV.pptx
Unit-IV.pptxUnit-IV.pptx
Unit-IV.pptx
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
 
c programing
c programingc programing
c programing
 
Cmis 102 Effective Communication / snaptutorial.com
Cmis 102  Effective Communication / snaptutorial.comCmis 102  Effective Communication / snaptutorial.com
Cmis 102 Effective Communication / snaptutorial.com
 
C chap02
C chap02C chap02
C chap02
 
C chap02
C chap02C chap02
C chap02
 
Cmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.comCmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.com
 
Cmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.comCmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.com
 
I PUC CS Lab_programs
I PUC CS Lab_programsI PUC CS Lab_programs
I PUC CS Lab_programs
 
Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++
 

More from IIUM

How to use_000webhost
How to use_000webhostHow to use_000webhost
How to use_000webhostIIUM
 
Chapter 2
Chapter 2Chapter 2
Chapter 2IIUM
 
Chapter 1
Chapter 1Chapter 1
Chapter 1IIUM
 
Kreydle internship-multimedia
Kreydle internship-multimediaKreydle internship-multimedia
Kreydle internship-multimediaIIUM
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblockIIUM
 
Chap2 practice key
Chap2 practice keyChap2 practice key
Chap2 practice keyIIUM
 
Group p1
Group p1Group p1
Group p1IIUM
 
Tutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seoTutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seoIIUM
 
Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009IIUM
 
03 the htm_lforms
03 the htm_lforms03 the htm_lforms
03 the htm_lformsIIUM
 
Exercise on algo analysis answer
Exercise on algo analysis   answerExercise on algo analysis   answer
Exercise on algo analysis answerIIUM
 
Redo midterm
Redo midtermRedo midterm
Redo midtermIIUM
 
Heaps
HeapsHeaps
HeapsIIUM
 
Report format
Report formatReport format
Report formatIIUM
 
Edpuzzle guidelines
Edpuzzle guidelinesEdpuzzle guidelines
Edpuzzle guidelinesIIUM
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam PaperIIUM
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam PaperIIUM
 
Group assignment 1 s21516
Group assignment 1 s21516Group assignment 1 s21516
Group assignment 1 s21516IIUM
 
Avl tree-rotations
Avl tree-rotationsAvl tree-rotations
Avl tree-rotationsIIUM
 
Week12 graph
Week12   graph Week12   graph
Week12 graph IIUM
 

More from IIUM (20)

How to use_000webhost
How to use_000webhostHow to use_000webhost
How to use_000webhost
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Kreydle internship-multimedia
Kreydle internship-multimediaKreydle internship-multimedia
Kreydle internship-multimedia
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblock
 
Chap2 practice key
Chap2 practice keyChap2 practice key
Chap2 practice key
 
Group p1
Group p1Group p1
Group p1
 
Tutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seoTutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seo
 
Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009
 
03 the htm_lforms
03 the htm_lforms03 the htm_lforms
03 the htm_lforms
 
Exercise on algo analysis answer
Exercise on algo analysis   answerExercise on algo analysis   answer
Exercise on algo analysis answer
 
Redo midterm
Redo midtermRedo midterm
Redo midterm
 
Heaps
HeapsHeaps
Heaps
 
Report format
Report formatReport format
Report format
 
Edpuzzle guidelines
Edpuzzle guidelinesEdpuzzle guidelines
Edpuzzle guidelines
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
 
Group assignment 1 s21516
Group assignment 1 s21516Group assignment 1 s21516
Group assignment 1 s21516
 
Avl tree-rotations
Avl tree-rotationsAvl tree-rotations
Avl tree-rotations
 
Week12 graph
Week12   graph Week12   graph
Week12 graph
 

Recently uploaded

Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 

Recently uploaded (20)

Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 

Mid term sem 2 1415 sol

  • 1. INTERNATIONAL ISLAMIC UNIVERSITY MALAYSIA MID-TERM EXAMINATION SEMESTER 2, 2014/2015 SESSION KULLIYYAH OF INFORMATION AND COMMUNICATION TECHNOLOGY Programme : BIT Level of Study : Undergraduate Time : 10.00 a.m. – 12.15 p.m Date : 13th March 2015 Duration : 2 Hours and 15 Minutes Course Code : CSC1100 Section(s) : All Course Title : Elements of Programming This Question Paper consists of 17 Printed Pages including cover page containing 4 sections Section 1: Multiple-Choice (10 questions) Section 2: Fill-in-the-blank (10 Questions) Section 3: Short Answers (3 Questions) Section 4: Programming (1 Question) You are required to answer ALL questions. You are allowed to bring a calculator into the exam hall. INSTRUCTION(S) TO CANDIDATES DO NOT OPEN UNTIL YOU ARE ASKED TO DO SO ANSWER ALL QUESTIONS IN THIS QUESTION PAPER YOU ARE ONLY ALLOWED TO ASK QUESTIONS DURING THE FIRST 15 MINUTES OF THE EXAMINATION PERIOD Any form of cheating or attempt to cheat is a serious offence, which may lead to dismissal. Name: Matric No: Section: Lecturer’s Name:
  • 2. Page 2 of 16 SECTION 1: Multiple-Choice Circle the correct answer in this question paper [1 marks x 10 = 10 marks]. 1. Which of the following is NOT TRUE about C++ ? a. C++ is an extension of the C language b. C++ is a low-level programming language c. C++ is an object-oriented language d. C++ is a high-level programming language 2. When a declaration statement is used to store a value in a variable, the variable is said to be ______. a. created c. initialized b. declared d. referenced 3. The C++ statement ‘cout << (6 + 15);’ yields the result ____. a. 6 + 15 c. (6 + 15) b. 21 d. (21) 4. A data ____ is defined as a set of values and a set of operations that can be applied to these values a. type c. base b. set d. dictionary 5. What will be the value of the variable n4 after running the following codes? int main () { int n1 = 3, n2 = 4, n4; double n3 = 2.2; n4 = (n1/n2) * n3 + abs(n1-n2); cout << double(n4) << endl; return 0; } a. 2.65 c. 1 b. 0.65 d. 0
  • 3. Page 3 of 16 6. The use of default in a switch-case statement is to _______ a. exit from the program b. exit from the case structure c. cover unhandled possibilities d. continue with the remaining case conditions 7. Which of the following is a valid call to a C++ cmath function? a. sqr(9.0) b. ab(-y) c. exp(2.5) d. power(x,2) 8. What will be the output of the following code? int main () { int num = -5; bool sign = false; for(int i = 1; i <= 4; i++) { if (sign && num > 0) { cout << ++num << endl; break; } else if (!sign || num < 0) { cout << --num ; continue; } else cout << num++ ; } return 0; } a. -4 c. -5-4-3-2 b. -5-6-7-8 d. -4-3-2-1 9. If an expression is false to begin with, then !expression is true and evaluates to a value of ____. a. 0 c. 2 b. 1 d. 3
  • 4. Page 4 of 16 10. Using nested if statements without including _______ to indicate the structure is a common programming error. a. parentheses c. braces b. quotation marks d. switches SECTION 2 : Fill-in-the-blank Choose the correct term from the box given below and complete each of the following statement. Write your answers in the blank column next to each statement . [1 marks x 10 = 10 marks] namespace main( ) robust compound pre-processor sentinel pseudocode relational compiler loader class algorithm sizeof( ) i. _______ is the file accessed by compiler when looking for prewritten classes or functions pre-processor ii. A _______program detects and responds effectively to unexpected user inputs. robust iii. The ______ operator determines the amount of storage reserved for a variable. sizeof( ) iv. An _______ is a procedure or step by step sequence of instructions to perform a computation in a program algorithm v. A _______ places programs into main memory for execution. loader vi. In programming, a _______ is a special value that can be used as a flag or signal to start or end a series of data. sentinel vii. One or more individual statements enclosed within braces is also known as a ______ statement. compound viii. _________________ expressions are used to compare operands. relational ix. In object-oriented programming, a _______ is a set of objects that share similar attributes. class x. _______ forces the conversion of a value to another type casting
  • 5. Page 5 of 16 SECTION 3: Short Answer Questions Answer all questions in the spaces provided. [40 marks] Question 1: Short Answers i. A source program, cannot be executed until it is translated into machine language. Draw a diagram that demonstrates the process of program translation. [2 marks] ii. List down two important rules for evaluating arithmetic expressions. [2 marks] Both operands are integers: result is integer One operand is floating-point: result is floating-point iii. What is a symbolic constant? Provide a C++ example declaration of a symbolic constant of type double. [2 marks] Symbolic constants are identifiers whose values cannot be changed throughout the program. Example: const PI = 3.142 OR #define PI 3.142 iv. Describe the meaning of an ‘infinite loop’ and a ‘fixed count loop’ in repetition. [2 marks] - An infinite loop is a loop with an expression that is always true and never ends. - A fixed count loop is a loop with a tested expression which is a counter that checks for a fixed number of repetitions.
  • 6. Page 6 of 16 v. Explain with examples the three (3) types of logical operators in C++. [5 marks]  AND operator, &&:  Compound condition is true (has value of 1) only if both conditions are true  Example: (age > 40) && (term < 10)  OR operator, || (double vertical bar):  Compound condition is true if either one of the expressions is true or if both conditions are true  Example: (age > 40) || (term < 10)  NOT operator, !:  Changes an expression to its opposite state  If expression is true, then !expression is false Question 2: Output Identification Determine the outputs for the following code segments (assume all necessary predecessor commands have been defined). i. For the following codes, assume that the number entered is 42339. [5 marks] int main() { int num; cout << "Enter a five-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; }
  • 7. Page 7 of 16 ii. What is the printout of the following switch-case statement? [3 marks] int main() { int val = 22; switch(val%3) { case 2: cout<< "Good, "; break; case 1: cout<< "Better, "; case 0: cout<< "Best, "; default: cout<< "Thank you!"; } return 0; } 4 2 3 3 9 Output: Better, Best, Thank you! Output:
  • 8. Page 8 of 16 i. What is the output for the following codes? [3 marks] int main() { int x = 9, y=11; if ( x < 10 ) if ( y > 10 ) cout << "%%%%" << endl; else cout << "#####" << endl; cout << "$$$$$" << endl; return 0; } ii. Determine the output of the following codes. [4 marks] int main() { int x, y; for(x = 5; x >= 1; x--) { for(y = 6; y > x; y--) { cout << x + y; } cout << char('a' + 2) << endl ; } return 0; } %%%% $$$$$ Output:
  • 9. Page 9 of 16 Question 3: Find and correct the error(s) in the following programming statements: i. 1 2 3 4 5 6 7 8 9 10 11 12 #include <iostream> using namespace std; int main () { int a = 1; b = 2; double c; c = (a + b) * 2 + (a - b)/2; cout >> c >> endl; return 0; } [3 marks] 11c 109c 987c 8765c 76543c Output: Line No. Correction 5 int a=1, b=2; 9 cout << c << endl;
  • 10. Page 10 of 16 ii. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main() { int a, b, c; cout << "Enter 3 numbers: "; cin >> a >> b >> c; if ( c * c = a * a + b * b ) cout << The are 3 numbers << endl; if else cout << "THERE ARE 3 NUMBERS" << endl; return 0; } [3 marks] iii. 1 #include <iostream> 2 3 using namespace std; 4 int main() 5 { 6 float average; 7 average = 45/2.0; 8 cout << setw[6] << setfill('*') << average << endl; 9 10 if (average = 22.5) 11 cout << setiosflags(ios::showpoint) << average 12 << endl; 13 cout << setprecision(2) << average << endl; 14 return 0; 15 } Line No. Correction 6 if ( c * c == a * a + b * b ) 7 cout << "The are 3 numbers" 9 else
  • 11. Page 11 of 16 [6 marks] SECTION 4: Programming Question Answer all questions in the spaces provided. [25 marks] Question 1: In Japan, vending machines are not only used to buy drinks or snacks, but also to buy books or magazines. You are required to create a C++ program that acts as vending machine that is able to dispense novels, magazines and newspapers. Each category of item consists of 3 different titles to be represented by any characters between a-z (ie., name the titles as ‘a’, ‘b’, ‘c’, etc….). Given that the initial number of items in all categories is 9 units (3 units for each item), use the ‘while’ and ‘switch-case statements to perform the following: a. Draw a simple flow chart for your program (refer to the relevant symbols in the attachment) b. Your program should prompt the user to enter the amount of items they want to purchase (e.g, entering 5 means user can buy 5 items in any combination of categories) and display the titles selected (you may just use a cout for titles). c. Your program should calculate the total price to be paid for all items purchased by the user at the end of the program. Line No. Correction 2 #include <iomanip> 6 cout << setw(6) << setfill('*') << average << endl; 10 if (average == 22.5) { 13 cout << fixed << setprecision(2) << average << endl; }
  • 12. Page 12 of 16 d. Once an item in each category is purchased, the number of items will decrease by one. Your program should be able to print the balance of each category of item at the end of the program. The following is the sample output of your program: Please enter number of items: 2 Item 1 Choose the type of item (1 – novel, 2 – magazine, 3 – newspaper) : 1 Enter title : ‘a’ You have selected the novel “In the eyes of the night” The price of this item is RM 10. Item 2 Choose the type of item (1 – novel, 2 – magazine, 3 – newspaper) : 3 Enter title : ‘z’ You have selected the newspaper “The New Straits Time” The price of this item is RM 2.50. Total price of all items is RM 12.50 Balance for novel is 2 Balance for newspaper is 2 Press any key to continue . . . ______________________________________________________________________________ ______________________________________________________________________________ ______________________________________________________________________________ ______________________________________________________________________________ ______________________________________________________________________________
  • 13. Page 13 of 16 Flowchart (Simplified) Start Input numItem Compute balance = balance-1 count < numItem Input category category is 1 or 2 or 3 title between ‘a’-‘z’ Input title Compute totalPrice = totalPrice + price End Display totalPrice, balance true false false false true true Display title, price
  • 14. Page 14 of 16 //Solution Midterm Exam (S4) #include <iostream> #include <iomanip> using namespace std; int main() { int limit,count; bool select1=false,select2=false,select3=false; double totalp = 0.0,p_novel,p_mag,p_news,priceCat = 0.0; int balance1 = 3, balance2 = 3, balance3 = 3; int category; char title; cout << "************************************************" << endl; cout << "Books & Magazines Vending Machinen " << endl; cout << "Please enter the number of items you wish to purchase: " ; cin >> limit; count = 1; while (count <=limit) { cout << "nItem " << count << endl; cout << "Please enter the category of item to purchasen" ; cout << "Press '1' for novelsn" ; cout << "Press '2' for magazinesn" ; cout << "Press '3' for newspapersnn" ; cin >> category; switch(category) { case 1: cout << "You have selected 1 for novelsn"; cout << "Select 'a' for : In the eyes of the nightn"; cout << "Select 'b' for : I love Programming, not!n"; cout << "Select 'c' for : Exam, exam, go away...n"; cin >> title; select1 = true; switch(title) { case 'a': case 'A': p_novel = 10.00; cout << "You have selected the novel 'In the eyes " << "of the night'n"; cout << "The price of the novel is RM " << fixed << setprecision(2) << p_novel << endl; break; case 'b': case 'B': p_novel = 12.00; cout << "You have selected the novel 'I love " << "Programming, not!' " << endl;
  • 15. Page 15 of 16 cout << "The price of the novel is RM " << fixed << setprecision(2) << p_novel << endl; break; case 'c': case 'C': p_novel = 8.50; cout << "You have selected the novel 'Exam, exam, " << "go away...' " << endl; cout << "The price of the novel is RM " << fixed << setprecision(2) << p_novel << endl; break; default: cout << "Wrong selection, please try again..." << endl; continue; }//inner switch1 balance1 = balance1 - 1 ; priceCat = p_novel; break; case 2: cout << "You have selected 2 for magazinesn"; cout << "Select 'j' for : Home Improvementn"; cout << "Select 'k' for : Cooking with Madamn"; cout << "Select 'l' for : Programming Bits and Bytesn"; cin >> title; select2 = true; switch(title) { case 'j': case 'J': p_mag = 5.00; cout << "You have selected the magazine 'Home " << "Improvement' " << endl; cout << "The price of the magazine is RM " << fixed << setprecision(2) << p_mag << endl; break; case 'k': case 'K': p_mag = 7.00; cout << "You have selected the magazine 'Cooking " << "with Madam' " << endl; cout << "The price of the magazine is RM " << fixed << setprecision(2) << p_mag << endl; break; case 'l': case 'L': p_mag = 6.50; cout << "You have selected the magazine " << "Programming Bits and Bytes' " << endl;
  • 16. Page 16 of 16 cout << "The price of the magazine is RM " << fixed << setprecision(2) << p_mag << endl; break; default: cout << "Wrong selection, please try again..." << endl; continue; } //inner switch2 balance2 = balance2 - 1; priceCat = p_mag; break; case 3: cout << "You have selected 3 for newspapersn"; cout << "Select 'x' for : Berita Hariann"; cout << "Select 'y' for : New Straits Timen"; cout << "Select 'z' for : Utusan Malaysian"; cin >> title; select3 = true; switch(title) { case 'x': case 'X': p_news = 1.50; cout << "You have selected the newspaper 'Berita " << "Harian' " << endl; cout << "The price of the newspaper is RM " << fixed << setprecision(2) << p_news << endl; break; case 'y': case 'Y': p_news = 2.00; cout << "You have selected the newspaper 'The New " << "Straits Time' " << endl; cout << "The price of the newspaper is RM " << fixed << setprecision(2) << p_news << endl; break; case 'z': case 'Z': p_news = 1.80; cout << "You have selected the newspaper 'Utusan " << "Malaysia' " << endl; cout << "The price of the newspaper is RM " << fixed << setprecision(2) << p_news << endl; break; default: cout << "Wrong selection, please try again..." << endl;
  • 17. Page 17 of 16 continue; } //inner switch 3 balance3 = balance3 - 1; priceCat = p_news; break; default: cout << "Wrong selection, please try again..." << endl; continue; } //outer switch totalp = totalp + priceCat; count++; } // end while cout << "nThe total price for your items is: " << fixed << setprecision(2) << totalp << endl ; if (select1 == true) cout << "Balance for novels is now: " << balance1 << endl; if (select2 == true) cout << "Balance for magazines is now: " << balance2 << endl; if (select3 == true) cout << "Balance for newspapers is now: " << balance3 << endl; cout << endl; return 0; } - End of question –
  • 18. Page 18 of 16 Attachment (Symbols for flow chart)