0% found this document useful (0 votes)
94 views

Exercise Sequential Control Structure

This document provides examples of sequential control structures in C++ including variable declarations, data types, input/output statements, mathematical expressions, and conditional statements. It contains multiple questions asking the learner to: 1) Identify valid variable names and determine appropriate data types. 2) Write declaration statements for variables of different data types. 3) Write simple programs to assign and display string values. 4) Calculate mathematical expressions and determine variable values after operations. 5) Identify errors in code snippets and write C++ expressions for mathematical formulas.

Uploaded by

wann ielaa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Exercise Sequential Control Structure

This document provides examples of sequential control structures in C++ including variable declarations, data types, input/output statements, mathematical expressions, and conditional statements. It contains multiple questions asking the learner to: 1) Identify valid variable names and determine appropriate data types. 2) Write declaration statements for variables of different data types. 3) Write simple programs to assign and display string values. 4) Calculate mathematical expressions and determine variable values after operations. 5) Identify errors in code snippets and write C++ expressions for mathematical formulas.

Uploaded by

wann ielaa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

EXERCISE – SEQUENTIAL CONTROL STRUCTURE

CSC415

QUESTION 1

a. Tick in the box provided if the identifier/variable name is a valid name.

1) prod_a 6) average
2) c1234 7) 12345
3) while 8) finGrad
4) sum.of 9) grade1
5) $total 10) new bal

b. Give the most appropriate data type for each of the following values:
i. 23 v. 20125.12345
ii. ‘c’ vi. true
iii. 8.52 vii. 40000
iv. “9374”

c. Create a variable name for each of the following and give the appropriate data type:
i. Speed of an automobile
ii. Shipping rate per ringgit
iii. Highest score in exam
iv. Initial ‘m’ for male or ‘f’ for female
v. Amount of students in a class
vi. Pass or fail

d. Write a declaration statement in C++ to declare the following variables :-


i) grade1, grade2 and grade3 to store integer numbers.
ii) gender and race to store character types.
iii) bunga to store a string type of length 10

e. Write a program based on the following instruction:


1. Declare a variable to store string character.
2. Assign a message the variable, e.g “HELLO WORLD” using strcpy() function.
3. Display the value of the variable.

f. Write a C++ declaration for each of the following questions


a) Get input for user's age
b) Display output for totalprice
c) Declare a double named salary
d) Declare variable named address to store 14 characters
e) Declare a variable named studName as character. The length of variable is 50
f) Declare three variables as integer. Assign first variable to 5, second and third variables to 10.
g) Declare four variables whereby the first two are float and the last two variables are integer

g. Give appropriate data types for the following statements :


a. number of students at school,
b. a letter grade for an exam
c. the average weight of students in a class.

Ms Maznie Manaf, FSKM


h. What is the output for each of the following questions?
i) cout<<"\tI \nlike \nb\to\to\tk\ts";

ii) cout<<setw(10)<< "Hello"<<endl;


cout<<setw(20)<< "This is program C++";

iii) float num=4.5663;


cout<< "First number : "<<setprecision(3)<< num;
cout<<"Second number : "<<setprecision(2)<<num;

QUESTION 2

a. Write a program to calculate the result based on the following formula:

I. z = [a2 + 4ac] ÷ 2
III. b2 – 4ac
II. y = √a(b+c)(b-c)
2

*Prompt the user to enter the appropriate input and display the value of z and y. Use pow() and
sqrt() functions in your formula if necessary.

b. Write down the value of num after each of the following statements. Assume that the value of num is
100 before each statement executed :-
i) num++
ii) num *= 3
iii) num += 10
iv) num -= 100

c. Find and explain the errors if any for the following expression :-
i) 35.0 = num1 + num2
ii) second = first ++ second;
iii) num1 + 3 = num2 + 7
iv) sum += 5;

d. Determine the value of the following expression, given


int a = 2, b = 5, c = 7;
float x = 10.0, y = 10.5, z = 20.0;
i) a < 2 && y > 5.0
ii) a < b || b > c
iii) z != 10.0
iv) b%a==c%b

e. Write C++ expressions for the following mathematic expressions.


i. total interest =2*total loan *2% + 13 *total loan * 5%
ii. z = -b + √(b2 – 4ac)
2a
iii. a = (k2 + m) (√n)
(k – x)m
iv. c = a2 + b2
√r – 1
v. w = (z mod y) 2z + b
2
vi. Net sales = (total sales + commission) – total expenses – 15% of import tax

Ms Maznie Manaf, FSKM

You might also like