Topic 02 Writing First C++ Program

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 58

Topic 2

Writing First C/C++ Program

1
A Small C Program
 Below is a small C/C++ program
 It consists of one function

int main(void)
{
return 0;
}

2
A Small C Program
 A function is a block of codes/instructions.
 A C/C++ program consists of one or more functions.
 There must be a minimum of one function.
 main must be one of the functions.
 The main function is where program execution begins.
 When a C/C++ program runs, it gives back a value to indicate
whether program execution is successful.
 0 (zero) shows successful execution.
 Non-zero shows there was some problem.

3
A Small C Program

Function header – marks the


int main(void) beginning of a function.
{
return 0; Indicates that the function
name is main and the
}
function gives back an integer
type value (int) and the
Return Return Parameter/ function does not need any
type value argument information (void).

4
A Small C Program (cont.)

int main(void) Opening curly brace -


{ indicates the start of the
return 0; body of the function.
}
Closing curly brace -
indicates the end of the body
of the function.

5
A Small C Program (cont.)

int main(void) The return statement


{ terminates the execution of
return 0; the function and returns
control to the operating
}
system. It also gives back
the function’s result.

Zero return value indicates


the program finished
without error.

6
Data Type int
 The numbers (0,1,2, etc.) are integers.
 Integers are whole numbers which do not have decimal
point.
 Integer values are one category of constants i.e. values
that appear in a program.
 In C/C++, integers belong to a group of data called int.
 int is a C/C++ data type.

7
Statements
 A statement is a C/C++ instruction that causes an action
to be performed by the CPU.
 A C/C++ statement translates into one or more machine
language instructions.

int main(void)
{
return 0; Notice the statement ends
with a semicolon (;).
}

8
Input-Process-Output
 A basic program consists of the following
operations:
1. Get some input data
2. Process the data
3. Produce some output result

Input Process Output

(left/right arrows, (draw items on screen)


(determine x,y coordinates,
space bar)
keep scores)
9
Working with Data
 A program usually processes data (information).
 e.g. values, strings, pictures, audio

 How do we store the data?


 In memory cells (address and value/content)

 How does a program refer to the memory cells?


 By assigning names (identifiers) to the various memory cells.
 e.g. x, y, z, i, j, k, c, id, number, str, name

10
Declaration Statement
 Before we can use a memory cell in the program, we need
to tell the compiler the name and the kind of data it will
store (data type).

 We do this using a declaration.


 Example:
Declaration that tells the
int x; compiler the name of the memory
cell is x and the type of data it
will store is integer.

The compiler will prepare x using


the available memory.
11
Declaration Statement
 We can declare more than one name in one declaration.
 Example:
Tells compiler the program
uses 3 memory cells with
int x, y, z; the names x, y and z to
store integer values.

Notice the names are


separated by commas.

12
Assignment Statement
 How do we tell the program to store a value in the
memory cell?
 By writing an assignment statement.
 Example: Assignment statement tells the
program to store the value 5 in the
x = 5; memory cell with the name x.

You can read this statement as:


“assign 5 to x” or “x becomes 5”.

= is the assignment operator. It does


NOT mean equal (which is a
comparison operation) here.
13
Assignment Statement

We can describe this in picture


x = 5; form as:
x 5

14
Assignment Statement

x 5
x = 5;
y = x;

15
Assignment Statement

x 5
x = 5;
Copy value
y = x; of x into y
y

16
Assignment Statement

x 5
x = 5;
Copy value
y = x; of x into y
y 5

17
Complete Program

int main(void)
{
int x, y;

x = 5;
y = x;

return 0;
}

18
Complete Program

int main(void) Function header


{
int x, y;

x = 5; Function Body
y = x;

return 0;
}

19
Complete Program

int main(void)
{
Declarations
int x, y;

x = 5;
y = x; Statements

return 0;
}

20
Complete Program with Memory
Manipulation
int main(void)
{
int x, y;

x = 5; Assignment statements
y = x;
return statement
return 0;
}

Q&A: Any question


regarding memory?
21
Showing Results
 How do we show the results of a computation?
 By using the standard output stream. A stream is an entity
(object) where a program can either insert or extract
characters to/from.
 For formatted output operations, cout is used together with
the insertion operator, which is written as << (i.e. two "less
than" signs).

22
Showing Results
 The syntax of cout and << is:

23
Showing Results
 A manipulator is used to format the output
 Example: endl causes insertion point to move to the beginning
of the next line.

 The new line character is '\n'


 It may appear anywhere in the string. e.g.
cout << "Hello there. ";
cout << "My name is James.";
Output:
Hello there. My name is James.

cout << "Hello there.\n";


cout << "My name is James.";
Output :
Hello there.
My name is James.

24
Showing Results – Standard Libraries
 The cout object is defined in standard libraries.
 A library is a collection of classes and functions to provide
features and tasks for the language.
 The cout object is described in a file called iostream
which must be inserted into a program in order to use it.
 We insert this file by:
#include <iostream>
 This is called a preprocessor directive.

25
Showing Results - Namespace
 cin and cout are declared in the header file iostream, but
within std namespace. Namespace allows two functions to share a
common name. Function under a namespace must be addressed using
"namespace::" prefix.
 To use cin and cout in a program, we also can use the following:
#include <iostream>
using namespace std;
 The C++ Standard Library incorporates 18 headers of the ISO C90 C
standard library ending with ".h", but their use is deprecated. All other
headers in the C++ Standard Library DO NOT end in ".h".
 The header from the C++ Standard Library is included under a different
name, generated by removing the .h, and adding a 'c' at the start; for
example, 'time.h' becomes 'ctime'.

26
Complete Program with Display
#include <iostream> Memory Cells
using namespace std;
x 5
int main(void)
{
int x, y; y 5

x = 5; Computer Screen
y = x;
cout << "y = " << y; y=5
return 0;
}

27
Computation – Arithmetic Operators
 How do we make the program do some computation or
calculation?
 Use arithmetic operators.
Operator In Maths In C/C++
Add a+b a+b
Subtract a-b a-b
Multiply ab a*b

a
Divide --- or a / b or a ÷ b a/b
b
Modulus a mod b a%b
28
Variables
 Programs store data in variables. Variables refer to
the memory cells where values can be changed during
program execution.

 A variable is a memory cell that has:


 A name
 A data type
 A value
 An address

29
Variable Declaration and Assignment
int n; Variable declaration – tells
compiler the variabe’s name and
data type.
n = 72;
Assigns value 72 to variable n.

Only variables are


allowed on the Memory Cells
left-hand side of
an assignment n 72
statement.
The address of n is
determined by the
30
compiler.
Getting Input Data
 How do we make a program input the data from the
user?
 By using the standard input stream.
 cin is used together with the extraction operator, which is
written as >> (i.e., two "greater than" signs).

 After we get the data, where do we store it?


 In a variable

31
Getting Input Data
 The syntax of cin and >> is:

 Example:
 If num is an integer variable

cin >> num;


 Causes computer to get a value of type int
 Places it in the variable num

32
Getting Input Data
 We can input multiple data. Assume feet and
inches are variables of type int:
cin >> feet >> inches;
 Inputs two integers from the keyboard
 Places them in variables feet and inches respectively
 The user should type the first number, followed by a space or
“Enter”, then the second number, followed by a/another
“Enter”.

33
Getting Input Data

34
Complete Program with Input
#include <iostream>
using namespace std;

int main(void)
{
int num;
Read statement
cin >> num;

return 0;
}
35
Getting Input Data

Computer Screen
A blinking cursor appears
_ and program waits for the
user to enter a number.

 Better to let the user know that the program is expecting


some data by a prompt using cout.

36
Complete Program with Display and
Input
#include <iostream>
using namespace std;

int main(void)
Displays a
{
prompt.
int num;

cout << "Enter a number: ";


cin >> num;
Getting input
return 0; data
}

37
Getting Input Data
Computer Screen
Cursor appears and
Enter a number: _ program waits for the
user to enter a number

Prompt displayed by
cout object

38
Getting Input Data
Computer Screen
User types a number and
Enter a number: 91 presses “Enter” key.

The input data is stored


as an integer in the
variable num.

num 91

39
Store a value into variable
 Note the two ways to store a value into a variable:
int num;
 By using the assignment statement
num = 35;
 By using a read statement
cin >> num;

40
What does this program do?
#include <iostream>
using namespace std;

int main(void)
{
int n;
cout << "Enter n: ";
cin >> n;

int n_squared = n * n;

cout << "n x n = " << n_squared << endl;

return 0;
}
41
What does this program do?
Computer Screen
5 n
Enter n: 5
n x n = 25
25 n_squared

42
What does this program do?
#include <iostream>
using namespace std;

int main(void)
{
int a, b;
cout << "Enter a: ";
cin >> a;

cout << "Enter b: ";


cin >> b;

int sum = a + b;

cout << "a + b = " << sum << endl;

return 0;
}
43
What does this program do?
Computer Screen a
531
Enter a: 531
Enter b: 24 24 b
a + b = 555

555 sum

44
What does this program do?
#include <iostream>
using namespace std;

int main(void)
{
int a, b;
cout << "Enter a and b: ";
cin >> a >> b;

int sum = a + b;

cout << a << " + " << b << " = " << sum << endl;

return 0;
}

45
What does this program do?

Enter a and b: 531 24

a 531 b 24 sum ?

46
What does this program do?

Enter a and b: 531 24


531 + 24 = 555

a 531 b 24 sum 555

47
What does this program do?
#include <iostream>
using namespace std;

int main(void)
{
int a, b;
a = 3;
b = 5;
cout << "a = " << a << ", b = " << b << endl;

a = b;
b = a;
cout << "a = " << a << ", b = " << b << endl;

return 0;
}
48
What does this program do?
a = 3;
a 3 b ?
b = 5;

a = b;
b = a;

49
What does this program do?
a = 3;
a 3 b 5
b = 5;

a = b;
b = a;

50
What does this program do?
a = 3;
a 5 b 5
b = 5;

a = b;
b = a;

51
What does this program do?
a = 3;
a 5 b 5
b = 5;

a = b;
b = a;

52
What does this program do?
#include <iostream>
using namespace std;
int temp;
int main(void)
{ temp = a;
int a, b; a = b;
b = temp;
a = 3;
b = 5; cout << "a = " << a
<< ", b = " << b
cout << "a = " << a << endl;
<< ", b = " << b
<< endl; return 0;
}

53
What does this program do?
a = 3;
a 3 b ? temp ?
b = 5;

temp = a;
a = b;
b = temp;

54
What does this program do?
a = 3;
a 3 b 5 temp ?
b = 5;

temp = a;
a = b;
b = temp;

55
What does this program do?
a = 3;
a 3 b 5 temp 3
b = 5;

temp = a;
a = b;
b = temp;

56
What does this program do?
a = 3;
a 5 b 5 temp 3
b = 5;

temp = a;
a = b;
b = temp;

57
What does this program do?
a = 3;
a 5 b 3 temp 3
b = 5;

temp = a;
a = b;
b = temp;

58

You might also like