Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Lecture 2 & 3 - Basic C++ Structure

The document provides an overview of computer programming fundamentals, focusing on the Von Neumann architecture, programming languages, and the structure of C++ programs. It discusses key concepts such as the Stored Program Concept, the Von Neumann Bottleneck, and the role of different software types. Additionally, it highlights the features and applications of C++, along with the basic structure of a C++ program and the compilation process.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 2 & 3 - Basic C++ Structure

The document provides an overview of computer programming fundamentals, focusing on the Von Neumann architecture, programming languages, and the structure of C++ programs. It discusses key concepts such as the Stored Program Concept, the Von Neumann Bottleneck, and the role of different software types. Additionally, it highlights the features and applications of C++, along with the basic structure of a C++ program and the compilation process.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

SCHOOL OF ELECTRICL ENGINEERING &

COMPUTER SCIENCE (SEECS)

Computer Programming (CS 107)


Sara Tariq Sheikh
Recap!
Key Points

Fundamentals of Computer Programming 2


Components of the Von Neumann Architecture

Component Role
Executes instructions and processes data.
Central Processing Unit (CPU) Consists of the Arithmetic Logic Unit (ALU) and Control
Unit (CU).
Performs mathematical and logical operations (e.g.,
Arithmetic Logic Unit (ALU)
addition, subtraction, AND, OR).
Directs operations within the CPU, decodes instructions,
Control Unit (CU)
and coordinates data flow between components.
Memory Unit (RAM) Stores both instructions and data (as per the Stored
Program Concept).
Input Devices Allow users to enter data (e.g., keyboard, mouse).
Output Devices Display processed data (e.g., monitor, printer).
System Bus Transfers data and instructions between the CPU, memory,
and peripherals.

Fundamentals of Computer Programming 3


Fundamentals of Computer Programming 4
Key Takeaways

• The Stored Program Concept states that both instructions and data are
stored in the same memory and treated the same way by the CPU.
• Allows programs to be written, modified, and executed dynamically.
• For example, a C++ program can read input, modify its behavior, and store
results all in memory.

Fundamentals of Computer Programming 5


Key Takeaways

• The Von Neumann Bottleneck refers to the limited data transfer speed
between the CPU and memory.
• Since data and instructions share the same bus, the CPU often has to wait for
memory access, causing slowdowns.

Fundamentals of Computer Programming 6


Key Takeaways

• Registers are small, high-speed storage locations inside the CPU that
temporarily hold data and instructions during processing.
• Registers help reduce the Von Neumann bottleneck, but they do not
completely eliminate it.

Fundamentals of Computer Programming 7


Key Takeaways

• Instead of frequently accessing RAM, registers hold temporary data for


faster processing.
• Since registers are within the CPU, they operate at CPU speed, reducing
wait time.
• Keeps important data (e.g. arithmetic results) in fast-access storage.
• Speeds up instruction execution by holding intermediate results and
memory addresses.

Fundamentals of Computer Programming 8


Key Takeaways

• Only a few registers exist compared to large RAM sizes.


• Once a register is full, data must still be fetched from RAM.
• Increasing register count significantly doesn’t solve the core problem of
slow memory access (i.e. it is not a scalable solution)
• Other techniques like cache memory can then be incorporated (layer
between RAM and CPU, where frequently accessed data can be stored)

Fundamentals of Computer Programming 9


Components of
Computer - Software
• Programs that run on computers
• System programs control the computer e.g.
operating systems
• Application programs perform a specific task.
• The operating system handles the overall activity
of the computer and provides services. Some of
these services include memory management,
input/ output activities, and storage management.
• Word processors, spreadsheets, and games are
examples of application programs.
• The operating system is the program that runs
application programs.

Computer Programming 10
Types of Software Programs

• System software
• Operating System Applications
• e.g. Windows, Linux
• Utilities
System
• e.g. Antivirus, registry cleaner OS
software

Utilities
• Application software
• e.g. Photoshop, MS Word

Computer Programming 11
Why do Programming?
Amazon Forest in 1975 Amazon Forest in 1995

• How much rain forest has been destroyed?


• Computer program can measure the differences between two pictures
Computer Programming 12
Why do Programming?

• Create solutions to real-world problems by automating tasks and processes


(navigating systems)
• Write scripts that perform repetitive tasks, saving time and reducing human
error (automating payrolls)
• Develop applications that improve productivity, communication, and
entertainment (Instagram)
• Drives innovation by enabling the creation of new technologies and systems
(intelligent machines)

Computer Programming 13
How do we write a program?

Computer Programming 14
Computer Programming
Algorithm.....

A step-by-step solution for solving a Example : Determine an employee's


problem in a finite amount of time weekly wages

Computer Programming
16
What do Computers Understand?

Computer Programming 17
Machine Language – 1st Generation Language

• Base 2 Number System (Binary / Machine Code) Decimal Binary


0 0000
1 0001
• Computers can only understand 0s and 1s
2 0010
3 0011
• Data stored in memory is also in the form of 0s and 1s
4 0100
5 0101
• Inputs from devices (keyboard, mouse) are converted to
6 0110
0s and 1s before processing 7 0111
8 1000
9 1001

Computer Programming 18
Programming in Machine Language
• Task: Add two numbers
Decimal Binary
• Possible Steps? 0 0000
• Get first number 0001 1 0001
2 0010
• Get second number 3 0011
0110 4 0100
• Create a placeholder to keep the result 5 0101
6 0110
• Sum both numbers 0111 7 0111
8 1000
• Store the result in a placeholder 9 1001

Computer Programming 19
What are Programming Languages?

Computer Programming 20
What are Programming Languages?

• Directly writing machine code is very difficult

• Systematic set of rules to easily write instructions/algorithms that can be


translated into machine code

• Programming languages enable writing instruction using English-like words

• Programmer writes the instructions called source code

• A compiler/interpreter converts all the source code into machine code

Computer Programming 21
Assembly Language – 2nd Generation Language

Computer Programming 22
Assembly
Language –
2nd
Generation
Language

23
Computer Programming
Key Terms

Compiler Translates programs written in a


high-level language into
machine language
Source Program A program written in a high-
level programming language
Object Program The machine language version
of a source program

Computer Programming
24
Computer Programming 25
Characteristics of a Programming Language

• Syntax
• How to write instructions to a computer

• Keywords
• Vocabulary of special/reserved words
with special meaning
• Explanation of the functionality of these
keywords

Computer Programming 26
Characteristics of a Programming Language

• Data structures
• What data types are supported by the
language
• Which operations can be performed on them

Computer Programming 27
Programming Language Hierarchy

5 GL

4 GL

High-Level Language
(3 GL)

Assembly Language (2 GL)

Machine Language (1 GL)

Hardware (Computers)

Computer Programming 28
High-Level Languages (3 GL)
int x = 5;
• Codes like everyday English int y = 6;
• Use mathematical notations int sum = x + y;
system.out.println(sum);
• E.g., BASIC, C, C++, COBOL, FORTRAN, Java, …

• Easier to read, write, and maintain but still requires a considerable


amount of programming knowledge

• Mainly independent of a particular type of computer

Computer Programming 29
Fourth-Generation Languages (4GL)
• Above high-level languages

• Designed to be closer to human (natural) language

SELECT ALL RECORDS WHERE NAME IS “SMITH”

• Languages for accessing databases and scripting

• E.g., Perl, Python, Ruby, SQL

Computer Programming 30
Fifth-Generation Languages (5 GL)
• Designed to make the computer solve a given problem without
programming

• User only worry about what problems need to be solved and what
conditions need to be met

• Mainly used in Artificial Intelligence

• E.g., Mercury, OPS5, Prolog, LISP

Computer Programming 31
Integrated Development
Environment (IDE)

Computer Programming 32
What is Integrated
Development
Environment (IDE)?

A software application that


provides comprehensive
tools for software
development in one place
to optimize productivity and
efficiency

Computer Programming 33
Important Features of IDEs

Computer Programming
34

https://multiqos.com/blogs/guide-to-integrated-development-environment/
IDEs for C/C++ Development

Computer Programming 35
Next on the List…..

• What is C++?

• Features of C++

• Basic C++ Program Structure

• Errors and Error Handling

Getting Started with C++ 36


What is C++?
• C++ is a compiled, high/low-level, general-purpose
programming language

• Developed by Bjarne Stroustrup in 1979


• Initially called “C with Classes”
• Commercially released in 1985

• Extension of the C programming language


• Object-Oriented Programming (OOP) features

Bjarne Stroustrup
Getting Started with C++ 37
Features of C++ (1/4)

Object
Portable Rich Library Case Fast and
Oriented
Language of Functions Sensitive Efficient
Programming

Dynamic
Platform Exception Compiler
Memory
Independent Handling Based
Allocation

Getting Started with C++ 38


Features of C++ (2/4)

• Low-level Memory Control


• Allows direct manipulation of memory through pointers

• Manual Memory Management


• C++ uses manual memory allocation (e.g., new/delete)
• Reduces runtime overhead
• No automatic garbage collection

Getting Started with C++ 39


Features of C++ (3/4)
• Compile-time Optimizations
• C++ compilers perform extensive optimizations to enhance execution
speed
• E.g., removing redundant code

• Zero-cost Abstractions
• High-level abstractions like classes and templates do not add extra
runtime cost
• Efficient execution similar to lower-level languages is ensured

Getting Started with C++ 40


Features of C++ (4/4)
• Efficient Standard Library
• The C++ Standard Library provides optimized data structures (e.g.,
vectors, maps) and algorithms designed for performance

• Multiple Paradigms
• C++ supports procedural, object-oriented, and generic programming
paradigms
• Developers can choose the most efficient paradigm for each task

• Good Community Support

Getting Started with C++ 41


C++ Shortcomings

• Pointers in C/C++ are a relatively difficult


concept to grasp

• Security issues still exist due to the


availability of friend functions, global
variables, and pointers

• C++ lacks the feature of a garbage collector


to automatically filter out unnecessary data

Getting Started with C++ 42


Applications of C++

Getting Started with C++ 43


Basic C++ Program Structure

Getting Started with C++


Building Blocks of a Program
• Statement
• A statement is a unit of code that the C++ compiler can compile and execute

• Input
• Get data/value from a user via keyboard, mouse, or from other sources like file, other devices

• Output
• Display data/values on the screen or send them to a file, other devices

• Expression
• Combination of values, variables, operators, and calls to functions
• Need to be evaluated by performing calculations like addition, multiplication, etc.

• Conditional execution
• Check for certain conditions and execute the appropriate sequence of statements

• Repetition
• Perform some action repeatedly, usually with some variation
Getting Started with C++ 45
Basic C++ Program Structure
Preprocessor directives
Starts with #(hash)
Adds functionality – Let’s us work
#include <iostream> with I/O objects
Namespace Library - use names for
using namespace std; objects and variables from the standard
library
int main() Main Function Header
{ Block Opening
//Output on the screen Comments

cout<< "Welcome to NUST!"; Printing Statement

return 0; Main Function Terminator


} Block Closing

Getting Started with C++ 12 46


Basic C++ Program Structure
using namespace std line can be omitted and replaced with the std keyword, followed by
the :: operator for some objects as shown below:

#include <iostream>

int main() {
std::cout << “Welcome to NUST!";
return 0;
}

Getting Started with C++ 47


Behind the Scenes: The Compilation Process
Header Files
iostream.h
my_header.h
Generate expanded
C++ source code file not C++
Preprocessor normally visible Compiler
main.cpp

C++ library files Object code file Assembler code file


Systems library files main.o Assembler not normally visible

https://slideplayer.com/slide/13925899/
#include <iostream>
using namespace std;
int main()
Executable {
Linker main.exe //Output on the screen
cout<< "Welcome to NUST.\n";
return 0;
Getting Started with C++ } 48
Example 1.1
• Design an algorithm to find the perimeter and area of a rectangle.

1. Get the length of the rectangle.


2. Get the width of the rectangle.
3. Find the perimeter using the following equation: perimeter = 2 ·
(length + width)
4. Find the area using the following equation: area = length · width

Computer Programming 49
In C++…...
#include <iostream>
using namespace std;
int main() {
double length, width, perimeter, area;
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
perimeter = 2 * (length + width);
area = length * width;
cout << "Perimeter of the rectangle: " << perimeter << endl;
cout << "Area of the rectangle: " << area << endl;
return 0;
}
Computer Programming 50
Additional Resources

• Generation of Programming Languages (GeeksforGeeks)


• Programming Language Generations (Unstop)

• Programming and Problem Solving with C++, 7th Edition


• Chapter 1: Overview of Programming and Problem Solving
• Section 1.1 Overview of Programming
• Section 1.2 How Does a Computer Run a Program?
• Section 1.3 What’s Inside the Computer?

Computer Programming 51
Additional Resources
• C++ Introduction (W3schools)
• C++ Programming Language (GeeksforGeeks)

• 5 Types of Programming Languages (Coursera)


• Behind the Scenes of C++ | Compiling and Linking (DEV)

• Programming and Problem Solving with C++, 7th Edition


• Chapter 2: C++ Syntax and Semantics, and the Program Development
Process
• Section 2.1 The Elements of C++ Programs
• Section 2.2 Program Construction

Getting Started with C++ 52

You might also like