0% found this document useful (0 votes)
39 views10 pages

@may - June 2016 Answers

The document contains code snippets and questions related to C++ programming. It includes code for while loops, arrays, functions, structures, and reading/writing data. The key aspects are: 1) Code shows use of while loops to iterate until a condition is met, arrays to store data, and functions for calculating values and checking conditions. 2) Questions ask about errors in code snippets, the purpose of code sections, and appropriate function definitions. 3) Later code shows structures to store product data and functions to read/write structure members and display stored values.

Uploaded by

Cass Ius
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views10 pages

@may - June 2016 Answers

The document contains code snippets and questions related to C++ programming. It includes code for while loops, arrays, functions, structures, and reading/writing data. The key aspects are: 1) Code shows use of while loops to iterate until a condition is met, arrays to store data, and functions for calculating values and checking conditions. 2) Questions ask about errors in code snippets, the purpose of code sections, and appropriate function definitions. 3) Later code shows structures to store product data and functions to read/write structure members and display stored values.

Uploaded by

Cass Ius
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

JUNE/MAY2

J 2016 COS1511

newval
n == 2

a == 9

cout
c << fi
indValue(3
3); // 23
The
T purpos
se : Conti
inue to ad
dd (s) to (n) per loop
l until
l n is gre
eater than
n 5

The
T purpos
se : Conti
inue to in
ncrement (
(c) per lo
oop when the
t a spec
cific
element
e in
n the "num
mbers" arr
ray specif
fied by th
he index variable
v (
(i) is
not
n equal to zero

Question
Q 3 (8marks)
a count
Liine 3 2
a count
Liine 4 2 0
a count
Liine5 2 0
a count
Liine7 3 0
a count
Line
L 8 3 2

a count
Liine 7 > 5 3 2
a count
Liine 7 6 2
a count
Liine 8 6 4
a count
Liine 7 > 5 6 4
a count
Liine 7 13 4
a count
Liine 8 13 6
a count
Liine 7 > 5 13 6
a count
Liine 10 13 6

int
i num_re
egistrants
s;
int
i amount
t;

cout
c Input the number of
<< "I f registra
ants : "; cin >> nu
um_registr
rants;
while(num_
w _registran
nts <= 0)
{
co
out << "Nu
umber of registrant
r ts cannot be zero or
o less thhan zero!\
\n";
co
out << "Pl
lease re-e
enter the number of
f registra
ants : "; cin >>
num_regist
n trants;
}
if(num_re
i egistrants <= 4) amount
a = 100.00;
else
e if(n
num_registrants <=
= 10) amo
ount = 80
0.00;
else
e amou
unt = 60.00;

cout
c "The amount of mon
<< " ney a com
mpany owe
es for a seminar : " << amount <<
endl;
e

Question
Q arks)
5 : (5ma

switch(nrW
s Wheels)
{
ca
ase 2 : co
ountTwo++;
; break;
ca
ase 4 : co
ountFour++
+; break;
de
efault : if(nrWheel
i ls > 4)couuntMore++;
; break;

for
f (int i = 1;i <== 10; i++)
cout
c << i << endl;

int
i i = 0;
while(i
w <=
= 10);
i++
i
cout
c << i << endl;
int
i num = 1;
while(num
w < 4)
{
coout << num
m << endl;
;
}

The
T error :
+ The proggrammer fo
orgot to increment
i (i) durin
ng the whi
ile loop
+ while(nuum < 4) sh
hould be while(num
w <= 4)
Correction
C n :
int
i num = 1;
while(num
w <= 4)
{
coout << num
m << endl;
; num++;
}

The
T error
r :
+ While lloop should be if loop
+ Missingg error message
m wh
hen i < 0
0.0
+ "Commisssion" shhould be "commisio
" on"
Correctio
C on :
if(sales
i > 0.0)
{
c
commision = sales * 0.1;
cout << commision;
c ;
}
else
e sales < 0.0) cout << "Inva
if(s alid sale
es. Pleas
se try ag
gain.\n";
Question
Q 7 : (6ma
arks)

float
f calccAverage(i
int crates
s[NUM_DAYSS])
{
in
nt total = 0;
fo
or(int i = 0; i < NUM_DAYS;
N i++)
to
otal += cr
rates[i];
re
eturn (flo
oat) total
l / (floatt) NUM_DAY
YS;

 
 

void
v chec
ck (int num1, floa
at num2);
;

double
d mu
ult(double num1, double
d nu
um2);

void
v time
e(int &seconds, in
nt &minut
tes, int &hours);
;

 
int
i count
tLets(string senP,
, char le
etter);
 

 
 
float
f cal
lcFinal(float, flo
oat, floa
at, int)
 

 
calcDiscou
c unt(discou
unt, produ
uctCode, n
number, da
ayOfWeek);
;
 
int
i sales
s[NUM_WEEKS][NAME]
]; 
 

int
i highesst_sales_p
per_week = 0;
for(int
f i = 0; i < NUM_WEEKSS; i++)
{
hiighest_sal
les_per_we
eek = sale
es[i][0];
foor(int j = 1; j < NAME;
N j++)
)
{
iff(highest_
_sales_per
r_week < s
sales[i][j
j]) highes
st_sales_p
per_week =
sales[i][j
s j];
}
coout << "Th
he highest
t sales fo
or week " << i + 1 << " : " <<
highest_sa
h ales_per_w
week << en
ndl;
}
 
cout
c << "
"name : " << p1.na
ame << en
ndl;
cout
c << "
"weight : " << p1.
.weight <
<< endl;
cout
c << " price << endl; 
"price : " << p1.p
 

void
v readd_Product_Record(P
Product &
&new_Prod
duct)
{
c
cout << "Enter nam
me : " <<
< new_Pro
oduct.nam
me << end
dl;
c
cout << "Enter wei
ight : " << new_P
Product.w
weight <<
< endl;
c
cout << "Enter pri
ice : " <
<< new_Pr
roduct.pr
rice << e
endl;

}
 
Question
Q marks)
11 : (7m

 
 
voidd display
yInitials(
()
{
str
ring firstname;
ring secondname;
str
ring surname;
str

ut << "Please ente


cou er your f
first nam
me: ";
get
tline(cin, firstna
ame);

cou
ut << "Please ente
er your s
second na
ame: ";
get
tline(cin, secondn
name);

cout
t << "please enter
r your su
urname na
ame: ";
getl
line(cin, surname)
);

cout
t << "\nY
Your initi
ials: " <
<< firstn
name[0] <<
< second
dname[0] <<
surname[0
s 0] << "\n";
     } 
 
OR 
O
 
 
 
void
v playInitials()
disp
{
string namesAndsurname, initials;
int space;

cout << "Please enter full names and surname , all seperated by
spaces: " <<endl;
getline(cin, namesAndsurname, '\n');
initials = namesAndsurname[0];
space = namesAndsurname.find (" ") ;//position of next space

while (space > 0)


{
initials = initials + namesAndsurname[space + 1];
space = namesAndsurname.find (" ", space + 1); //next blank space
}
cout << " Initials are : " << initials << endl;
}

You might also like