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

PF Lecture 2

Uploaded by

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

PF Lecture 2

Uploaded by

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

Programming Fundamentals

Lecture 2

1
Problem Solving

 Problem-solving techniques in computer science refer to


the methods used to find solutions to complex issues
using algorithmic or heuristic approaches. These
techniques can be systematic, analytical, or intuitive,
encompassing traditional programming, machine
learning, or artificial intelligence methods.

2
Problem Solving

3
Pseudocode
 Pseudocode is an informal high-level description of the
operating principle of a computer program or other
algorithm.

 How to write a Pseudo-code?

1. Start with the statement of a pseudo code which


establishes the main goal or the aim.

2. Arrange the sequence of tasks and write the


pseudocode accordingly.

4
Pseudocode
Pseudocode for making tea?
Making Tea

5
Pseudocode
Pseudocode for making tea?
Making Tea

6
Pseudocode
Pseudocode for Two Variables?
Algorithm Add_Two
start
input a,b
sum=a + b
output sum
end

7
Evolution of Operating Systems
 Batch processing
 Do only one job or task at a
time
 Operating systems
 Manage transitions between
jobs
 Increased throughput
 Amount of work computers
process
 Multiprogramming
 Computer resources are
shared by many jobs or
tasks
 Timesharing
 Computer runs a small
portion of one user’s job then
moves on to service the next
user

8
A Computer Program
 A Computer Program is
used to solve problems
using computers.

 A Computer program is a
set of instructions (in a
certain programming
language)

9
Types of Programming Languages
Example : wages = rate * hours
Three types of programming
languages

1. Machine languages
 Strings of numbers giving
machine specific instructions
 Example:
+1300042774
+
+1200274027
2. Assembly languages
 English-like abbreviations
representing elementary
computer operations (translated
via assemblers)
 Example:
LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY

10
Machine Languages, Assembly Languages, and
High-level Languages
3. ,

11
C++ and C
 C++ is a powerful programming language. It
has its roots in C language.

 C was invented and first implemented on


UNIX operating system.

 Initially, an older language BCPL was


developed by Martin Richards in 1967, which
influenced another language known as B,
which was invented by Ken Thompson.

 B led to development of C in 1970s.

12
C Language
 C is often called a middle-level programming language.

 This does not mean that it is not powerful, or it is not


easy, or less developed.

 It is a middle level language because it combines the


best features of high-level languages (such as BASIC or
Pascal) with the power and flexibility of low-level
languages (such as Assembly).

13
C++
 C++ is built on top of C and provides capabilities for Object-Oriented
Programming.

 Objects are reusable software components, that model things in real


world.

 Object-Oriented programs are easy to understand, correct, and


modify.

 C++ programs consist of pieces called as classes and functions.


There are rich collections of existing classes and functions in the C+
+ standard library available for everyone to use.

14
A Typical C++ Environment
Program is created in the
 Phases of C++ Editor Disk
editor and stored on disk.

Programs Preprocessor program


Preprocessor Disk processes the code.

 Edit Compiler Disk Compiler creates


object code and stores it
on disk.
 Preprocess
Linker Disk Linker links the object
code with the libraries and
 Compile Primary stores it on disk
Memory

 Link Loader

 Load Loader puts program


in memory.
Disk ..

 Execute
..
..

Primary
Memory

CPU CPU takes each


instruction and
executes it, possibly
storing new data
.. values as the program
.. executes.
..

15
Once again

16
Introduction to C++ Programming
 The C++ language facilitates a structured and disciplined
approach to computer program design.

 Consider the following example:

17
Example
// The first C++ Program
Comments
Written between /* and */ or following a //
#include <iostream> Improve program readability and do not cause the
computer to perform any action
using namespace std;
A namespace tells the compiler the domain that is
being used. In this case, this program is using the
standard C++ namespace. Hence the functions to be
int main() preprocessor
used in this program would directive
be standard ones.
{ Message to the C++ preprocessor
Lines beginning with # are preprocessor directives
cout << “Welcome to C++\n”;
#include <iostream> tells the preprocessor to
return 0; include the contents of the file <iostream>, which
includes input/output operations (such as printing to
} Prints
C++theprograms
the string of characters in double quotes
screen). contain one or more functions, exactly
return is one way to exit a function. one ofThe entire
which line is called a statement. Every
Left brace { begins themust
bodybeofmain
every function and a
statement ends with a semicolon.
return 0, in this case, meansright
thatbrace } ends it used to indicate a function
Parenthesis
the program terminated normally. 18
int means that main "returns" an integer value.
More Explanation
 cout
 Standard output stream object
 Connected to the screen

 <<
 Stream insertion operator
 Value to the right of the operator (right operand) inserted into
output stream (which is connected to the screen)

 \
 escape character
 indicates that a special character is to be output.

19
Escape Sequences

Escape Sequence Description

\n Newline. Position the screen cursor to the


beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next
tab stop.
\r Carriage return. Position the screen cursor to the
beginning of the current line; do not advance to the
next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\" Double quote. Used to print a double quote
character.

20
C++ Multiple Lines Text
// The first C++ Program

#include <iostream>
using namespace std;

int main()
{
cout << “Welcome\n to C++\n”;
return 0;
}

21
C++ Multiple Lines Text
// The first C++ Program

#include <iostream>
using namespace std;

int main()
{
cout << “Welcome to”;
cout << “ C++”;
return 0;
}
22
C++ Data Types
 All high-level programming languages support the concept
of data types.

 A data type defines a set of values that a variable can


store along with a set of operations that can be performed
on that variable.

 Common data types are integer, character.

 C++ has basic built-in data types. These are char, int, float,
double, and void. However, C is not a strongly typed
language.
 Ex. Adding Integer with a Float in the same expression is
allowed.
23
Data Types
 Standard C++ does not define a minimum size or range
for the basic data types. Instead, this size is suggested
by the architecture of the execution environment.

 Consider the following table:

24
Data Types (Incomplete Table)
 Type Size in Bits Min. Range

 char 8 -127 to 127


 int 16 or 32 -32,767 to
32,767
 float 32 6 digits of precision
 double 64 10 digits of precision

 Type modifiers: signed, unsigned, short, long

25
Data Types (Incomplete Table)
 The term "unsigned" in computer programming indicates
a variable that can hold only positive numbers.

 The term "signed" in computer code indicates that a


variable can hold negative and positive values.

 Type modifiers: signed, unsigned, short, long

26
Data Types (Incomplete Table)

27
Identifier Names
 In C/C++, names of variables, functions, labels, and
other user-defined objects are called identifiers.

 The length of these identifiers can be from one to several


characters.

 The first character must be a letter or an underscore,


and the subsequent characters must either be letters,
digits, or underscores.

 Let us look at few examples:

28
Identifiers Names
 Count Correct
 count Correct
 1count Wrong
 test123 Correct
 _test123 Correct
 my.name Wrong
 student_name Correct
 studentname Correct
 StudentName Correct

29
Assignment :
1. Write a program to print Your Name on screen.

2. Write pseudo code that reads two numbers and


multiplies them together and print out their product.

3. Write a program to display the following output


using a single cout statement.
Subject Marks
Mathematics 90
Computer 77
Chemistry 69

30

You might also like