0% found this document useful (0 votes)
9 views8 pages

Tutorial1 - Problem Solving and Programming

The document is a tutorial on the basics of C++ programming, covering key concepts such as syntax, compilers, algorithms, and the compilation process. It includes explanations of programming tools like structure charts, pseudocode, and flowcharts, as well as comparisons between structure and organization charts. Additionally, it addresses common programming errors, the importance of comments, and provides examples of variable declarations and data types.

Uploaded by

tanym-wp24
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)
9 views8 pages

Tutorial1 - Problem Solving and Programming

The document is a tutorial on the basics of C++ programming, covering key concepts such as syntax, compilers, algorithms, and the compilation process. It includes explanations of programming tools like structure charts, pseudocode, and flowcharts, as well as comparisons between structure and organization charts. Additionally, it addresses common programming errors, the importance of comments, and provides examples of variable declarations and data types.

Uploaded by

tanym-wp24
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/ 8

BMCS1013 C BMCS1014 Problem Solving and Programming Tutorial 1

Tutorial 1 – Introduction & Basic Elements of C++


1. Define the following terms:

(a) Syntax
-Syntax refers to the spelling and grammars of a programming language
(b) Compiler
Compiler is used to translate a program written in a high-level language to machine language
(c) Algorithm
-Algorithm is a sequence of instructions written for computer to solve a problem.

2. Explain the compilation and execution process of a C++ program. Describe each phase
from writing source code to viewing the program output. Illustrate your explanation with a
simple diagram.

3. Identify and describe three tools commonly used in planning a program. Explain how each
aids the development process.

a) Structure chart: is a program hierarchy chart that shows the relationships between
modules. It is also known as hierarchical chart, shows the functional flow through our
program. Structure chart shows how we are going to break our program into logical steps;
each step will be a separate module. The structure chart shows the interaction between all the
parts (modules) of our program.

b) Pseudocode: a notation resembling a simplified programming language, used in program


design. Pseudocode is using English words to convey the program logic. Its purpose is to describe,
in precise algorithmic detail, what the program is being designed to do. This requires defining the
steps to accomplish the task in sufficient detail so that they can be converted into a computer
program. Pseudocode excels at this type of precise logic.

c) Flowchart: Flowcharts graphically represent program logic by a series of geometric symbols


Page 1 of 8
BMCS1013 C BMCS1014 Problem Solving and Programming Tutorial 1

and connecting lines. A flowchart is a program design tool in which standard graphical symbols
are used to represent the logical flow of data through a function. It is basically same as the
pseudocode, that it requires defining the steps to accomplish the task.

4. Compare a structure chart with an organization chart. Use clear diagrams to support your
explanation of the differences in purpose and layout.
Purpose

Structure Chart Organization Chart


Purpose To show how a To show the hierarchy and
software/system is broken into roles of employees within an
modules and how they interact organization.
Focus Functional decomposition of a Management structure, roles,
system into smaller modules. and reporting lines.
Used by Software developers, system Managers, HR departments,
analysts. employees.

Layout
Structure Chart Organization Chart
Hierarchy Type Based on modules/functions Based on positions,
and data flow. departments, and authority.
Relationship Shown Control and data passing Supervision, management, and
between system modules. reporting relationships.

Structure Chart

Organization Chart

Page 2 of 8
BMCS1013 C BMCS1014 Problem Solving and Programming Tutorial 1

Sample ans:
Structure chart vs. organisation chart
• Structure chart is a program hierarchy chart that shows the relationships between modules.
Organisation chart is a hierarchy chart that shows the organisation structure

5. Draw a flowchart to represent the following pseudocode:

IF code is EQUAL TO '1'


Display "junior"
Assign 800.00 to salary
ELSE IF code is EQUAL TO '2'
Display "senior"
Assign 1200.00 to salary
ELSE
Display "Wrong code!"

SAMPLE ANS:

Page 3 of 8
BMCS1013 C BMCS1014 Problem Solving and Programming Tutorial 1

6. Design a pseudocode algorithm to calculate and display a student’s GPA (Grade Point
Average). The GPA is based on the total grade points earned and total credit hours taken.
BEGIN
SET totalPoints ← 0
SET totalCredits ← 0

// Course 1
INPUT grade1, credits1
totalPoints ← totalPoints + (grade1 × credits1)
totalCredits ← totalCredits + credits1

// Course 2
INPUT grade2, credits2
totalPoints ← totalPoints + (grade2 × credits2)
totalCredits ← totalCredits + credits2

// Course 3
INPUT grade3, credits3
totalPoints ← totalPoints + (grade3 × credits3)
totalCredits ← totalCredits + credits3

IF totalCredits > 0 THEN


GPA ← totalPoints / totalCredits
OUTPUT "GPA:", GPA
ELSE
OUTPUT "Invalid: No credit hours"
END IF
END

Page 4 of 8
BMCS1013 C BMCS1014 Problem Solving and Programming Tutorial 1

7. Differentiate between the following types of programming errors:

Compilation error vs. runtime error


-Error occurs during compilation time is known as compilation error. Usually caused by syntax
error.
-Error occurs during execution time is known as runtime error. It can be fatal which is caused by
system error. Nonfatal is usually caused by program logic
error

8. Why is it important to use comments in a program? List three reasons and demonstrate two
ways to write comments in C++.

1. it makes easier for other people to understand your program.


2. it helps you while programming to organize and write your code.
3. it helps you track down and debug errors.
The // comment style comments out any text on that line to the right of the slashes.
The /**/ comment style comments out all code contained between the asterisks.

Page 5 of 8
BMCS1013 C BMCS1014 Problem Solving and Programming Tutorial 1

9. Examine the list of identifiers(variable) below.

• Identify which identifiers are valid and which are not based on C++ naming rules.
• For invalid identifiers, explain why they are not allowed.

Identifier Is It Valid? Reason (If Invalid)


a) 40Hours Invalid-cannot start with digit
b) year valid
c) cost_in_$ Invalid-special character not allowed
d) number1 valid
e) include Invalid -Reserved keyword
f) int Invalid-Reserved keyword
g) box-44 Invalid-hyphen is not allowed
h) Student-name Invalid-hyphen is not allowed
i) DVD_ROM valid
j) 2morrow Invalid – cannot start with digit
k) my_age valid
l) Get Data Invalid – space not allowed
m) Double Invalid – reserved keyword if case-
sensitive use
n) A4 valid
o) mark&score Invalid-Special character ‘&’ not allowed
p) first name Invalid-Space not allowed.
q) 5th-Edition Invalid-Hyphen , starts with digit
r) Page# Invalid-Special characters not allowed
s) C++ Invalid- ‘+’ is not allowed
t) 1stName Invalid-Cannot start with digit

10. Write the output produced by the following C++ statements:

(a) cout << "\"\\nnow\"";


output:"\nnow"
(b) cout << "A1 \"Butch\" Jones";
A1 "Butch" Jones

(c) cout << "\"Hello\\\n Gandalf!";


"Hello\
Gandalf! (note the space before Gandalf)

11. Identify and correct the error(s) in each of the following code segments:

(a)
string word;
Page 6 of 8
BMCS1013 C BMCS1014 Problem Solving and Programming Tutorial 1

cout << "Enter a word: ";

cin << word;

string word;
cout << "Enter a word: ";

cin >> word;

(b)
cout << "Two plus two is " 2+2;

cout << "Two plus two is " << 2+2;

(c)
double invest = 2000.0;
double return = 1.016 * invest;
cout << "The return on your investment of $" << invest << " is
$" << return;

double invest = 2000.0;


double my_Return = 1.016 * invest; (cannot use return as variable
name)
cout << "The return on your investment of $" << invest << " is $"
<< calculatedReturn << endl;

(d)
int x, y;
cin << x << y;

int x,y;
cin >> x >> y;

12. Suggest a suitable data type for each of the following examples:

Scenario Data Type


(a) The average of four marks double
(b) The number of days in a month int
(c) The length of the Penang Bridge double
(d) The number of students in your class int
(e) The distance from your house to TAR UC KL (in km) double
(f) A single-character prefix used to specify product type char

Page 7 of 8
BMCS1013 C BMCS1014 Problem Solving and Programming Tutorial 1

13. Write the C++ variable declarations for the following:

(a) A character variable named option


char option;
(b) An integer variable sum, initialized to 0
Int sum = 0;
(c) A floating-point variable product, initialized to 1
float product = 1.0;

14. Write a single C++ statement for each of the following:

(a) Declare num1, num2, and num3 as integer variables


int num1, num2, num3;

(b) Declare tempA, tempB, and tempC as double variables, and initialize tempA to
28.9
double tempA = 28.9, tempB, tempC;

(c) Declare a character variable let1 and initialize it with 'a'


char let1 = 'a';

Eg:char gender = ‘M’;


String gender = “Male”;

(d) Declare a named constant rate with a value of 2.95


const float rate = 2.95;

(e) Assign the letter 'B' to the character variable initial


char initial = 'B';

(f) Output the value stored in the variable name to the console
cout << name ;

Page 8 of 8

You might also like