fundamentals of programming cs-114 · and structures in c++ 1 c3 clo 3 analyse real life problems...

48
Fundamentals of Programming CS-114 Lecture 1

Upload: others

Post on 01-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Fundamentals of ProgrammingCS-114

Lecture 1

Page 2: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

What is your level of expertise in

Programming?

1a) Never programmed before

1b) Have taken at least one course in

programming

1c) Have done significant

programming

1d) I am a master programmer

Page 3: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Mechanical Engineers –Programming & Software

• https://mechanicalc.com/posts/software-for-mechanical-

engineers

• https://www.youtube.com/watch?v=1_1H7G7DqG4

• https://www.youtube.com/watch?v=gHSZ1S9996U

Page 4: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

*Why C

*University of California

Page 5: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Back-end Programming Languages

Page 6: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Top Programming Languages

Page 7: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Top Languages wrt Jobs

Page 8: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

The Ideal Way to Do Computing

• The ideal way to ask computer to do something is

to order it in a natural language e.g.

- I want to view this webpage

- Calculate my annual tax

- etc.

• However, today’s computer’s are not intelligent

enough to understand our orders in natural

language completely.

Session 1 2

Page 9: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Where We Are in Computers?

• At the very basic level computers use the concept of an electrical pulse.

- Low voltage is represented as 0

- High voltage is represented as 1

• To instruct a computer we need ask the computer in the language of 0s and 1s commonly known as machine language.

• For instance 73 in a number in natural language in the language of 0s and 1s, it becomes 1001001

Session 1 5

Page 10: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Machine Language: Our First

Interaction with the Computer

• machine language. 10110011 00011001

01111010 11010001 10010100

10011111 00011001

01011100 11010001 10010000

10111011 11010001 10010110

– Finding an average of two numbers

• Not very intuitive way of working

• Not possible for humans to achieve a lot using machine

language

Session 1 6

Page 11: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

One Step Beyond - Assembly Language

• One level above machine language is assembly language

MOV 0, SUM

MOV NUM, AC

ADD SUM, AC

STO SUM, TOT

• More understandable but still very difficult for many of us.

• An assembler translates assembly language into machine language.

Session 1 7

Page 12: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Another Step - High-level

Languages

• High-level languages is another level above

machine language. X = (Y + Z) / 2

• Much more understandable.

• A compiler translates high-level language

into assembly language.

Session 1 8

Page 13: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Where are we going?

• The next step in computing is to use natural

language over a high-level language.

• But we are still away from it.

• A lot of research needs to be carried out

before we actually see this.

• Until then our task is to use high-level

languages in its best possible ways

Session 1 9

Page 14: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

What is Programming?

• When we say “programming” we are

actually referring to the science of

transforming our intentions in a high-level

programming language.

Session 1 10

Page 15: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Introduction to C++

• Where did C++ come from?

- Derived from the C language

• Why the ‘++’?

- ++ is an operator in C++

Page 16: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

C++ History

• C developed by Dennis Ritchie at AT&T

Bell Labs in the 1970s.

- Used to maintain UNIX systems

- Many commercial applications written in c

• C++ developed by Bjarne Stroustrup at AT&T

Bell Labs in the 1980s.

- Overcame several shortcomings of C

- Incorporated object oriented programming

- C remains a subset of C++

Page 17: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Object-Oriented Programming

• OOP: Now the dominant way to program, yet it is almost 40

years old! (Simula ’67 and Smalltalk ’72 were the first OOPLs)

- Dr. Alan Kay received ACM's Turing Award, the "Nobel Prize of

Computing,“ in 2003 for Smalltalk, the first complete dynamic OOPL

• It was slow to catch on, but since the mid-90’s everybody’s been doing it!

• OOP emphasizes objects, which often reflect real-life objects - have both properties and capabilities

- i.e., they can perform tasks: “they know how to...”

Session 1 15

Page 18: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

The Computer Onion

hardware

(PC)

Linux/DOS

Redhat/Windows

your C++ Program

Session 1 16

Page 19: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

How to Learn Programming

• Everybody learns programming at their own

pace.

• So do not be impressed by the person sitting

next to you because he coded a given

program in 20 minutes and you are taking

more than an hour.

• Speed programming does not necessarily

mean quality of the final output.

Session 1 19

Page 20: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

How to Learn Programming (cont’d)

1. Writing a good description of the problem.

2. Breaking down the given problem into small pieces.

3. Turning small pieces into pseudo-code.

4. Deciding the integration mechanism of the pieces.

5. Writing the program for each piece.

6. Integrating all the pieces together.

Session 1 20

Page 21: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Scare of Programming?

• Why most students are afraid of programming

- Paradigm Change

• Programming is totally different paradigm. You are working

on something and you cannot even touch the final output you

can only feel it. It is different then other subjects like Physics,

Chemistry, Biology, etc.

- Peer Pressure

• Some people are naturally good in programming so others

think that this is a natural ability and they cannot learn it.

Session 1 21

Page 22: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Scare of Programming? (cont’d)

- Lack of Understanding in Fundamental Concepts

• Some people start programming without a clue of what is

going on behind the scene in the computer. As a result they

have a flawed understanding from day one of their

programming experience

- Time Factor: Programming takes a lot of time

• Programming may take a lot of time at the start but once a

person is comfortable with the concepts and has mastered the

basic skills it is just like any other profession.

Session 1 22

Page 23: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

A Word of Advice

• Without good command on programming

any engineering qualification in current era

is almost

“worthless”.

• There is an acute shortage of programmers

in the global market and with time

this shortage is increasing.

Session 1 23

Page 24: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Class timings

• DE-41 Mechanical

– Lecture• CRM-01

• Tuesday 1400—1540

– Lab• DSP/DSD Lab

• Wed 0845—1125

– Office Hours• Tue 1540 – 1630

• Fri 1130 - 1230

– Course Material

http://biomisa.org/usman/fundamentals-programming-fall-2019.html

Page 25: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Course Outlines

• Introduction to Programming

• Introduction to C++

• Basic Structure of C++

• Basic input and output statements

• Operators

• Variables

• Loops

• Decision operators

• Functions

• Arrays & Strings

• Structures

• Pointers

• Files I/O

Page 26: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Course Book

• Object Oriented Programming in C++ by Robert Lafore

• C++ How to Programme by Dietel and Dietel

• References:

• A Structured Programing Approach Using C++ by Behrouz A. Forouzan

• www.cplusplus.com

Page 27: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Grading

• Sessional Exams: 20%

• Quizzes (4-6): 10%

• Computer assignments: 05%

• Lab Work: 15%

• Final Project: 10%

• Open Lab: 08%

• Final Exam: 32%

Page 28: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

CODE OF ETHICS

• All students must come to class on time (Attendance will be taken in first 5 to 10 mins)

• Students should remain attentive during class and avoid use of Mobile phone, Laptops or any gadgets

• Obedience to all laws, discipline code, rules and community norms

• Respect peers, faculty and staff through actions and speech

• Student should not be sleeping during class

• Bring writing material and books

• Class participation is encouraged

Page 29: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Policies• No extensions in assignment deadlines.

• Quizzes will be unannounced.

• Exams will be closed book.

• Never cheat.

– “Better fail NOW or else will fail somewhere LATER in life”

• Plagiarism will also have strict penalties.

Adapted from What is Plagiarism PowerPoint

http://mciu.org/~spjvweb/plagiarism.pptCourtesy Dr. Khawar

Page 30: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Learning Outcomes

• Enabling Knowledge: The process of designing

algorithmic solutions to computable problems; the syntax

and control structures of a programming language i.e. C++,

which enable you to code these algorithmic solutions using

standard coding conventions

• Critical Thinking and Analysis: Ability to analyze the

requirements for solving simple algorithmic problems.

Page 31: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Learning Outcomes (cont’d)

• Problem Solving: Ability to design and implement

programs to solve simple algorithmic computing problems,

based on analysis of the requirements.

• Communication: Ability to explain key concepts of

algorithmic design, in written form, to IT specialists.

Page 32: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Course Learning Outcomes

Course Learning Outcome (CLOs) PLOs

Learning

Level

CLO 1 Understanding fundamental concepts of computer

programing to solve a problem 1 C2

CLO 2 Implementing and applying the concepts of functions

and Structures in C++ 1C3

CLO 3 Analyse real life problems and relate their knowledge

of programing and algorithms to solve those complex

problems using arrays, functions and pointers

3 C4

CLO 4 To code, document, test and implement a well-

structured, robust computer program in Visual Studio

using C++ programming language

5 P2

Page 33: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Programming Languages

• Programming Language– A set of rules, symbols, and special words used to

construct a computer program.• Machine Language

– The binary representation of the instructions that a computer's hardware can perform.

• Assembly Language – A low-level programming language in which a mnemonic is used to represent

each of the machine language instructions for a specific computer.

• High-Level Language– A computer language that is more understandable and closer to standard

notations than assembly language. It is more close to plain English. C/C++ are high-level languages.

Page 34: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Programming language rules

• Rules of Syntax which specify how valid instructions are written in the language.

– They deal with the structure of an instruction

• Rules of Semantics which determine the meaning of the instructions (what the computer will do in response to the given instructions).

– They deal with the content of an instruction

Page 35: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Bugs and Debugging

• Programming errors are called bugs

• The process of tracking bugs and correcting them is called debugging.

• Three kinds of errors can occur in a program:

– syntax errors– semantic errors– runtime errors

• It is useful to distinguish between them in order to track them down more quickly

Page 36: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Syntax errors

• Most languages including c++ can only execute a program if the program is syntactically correct; otherwise, the process fails and returns an error message.

– Syntax refers to the structure of a program and the rules about that structure.

– For example, in English, a sentence must begin with a capital letter and end with a period.

this sentence contains a syntax error.

So does this one

Page 37: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Semantic Errors

• Semantic error is an error in the content of a code.

• If there is a semantic error in your program, it will run successfully, i.e. the computer will not generate any error messages, but it will not do the right thing.

• The problem is that the program you wrote is not the program you wanted to write. The meaning of the program (its semantics) is wrong.

• Identifying semantic errors can be tricky because it requires you to work backward by looking at the output of the program and trying to figure out what it is doing.

Page 38: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Runtime Errors

• The third type of error is a runtime error, socalled because the error does not appear untilyou run the program.

• These errors are also calledexceptions because they usually indicate thatsomething exceptional (and bad) hashappened.

• Runtime errors are rare in the simpleprograms so it might be a while before youencounter one.

Page 39: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Types of Program

• Source Program

– A program written in a human readable version, which you will write.

• Object Program

– The machine language version of a source program in 0s and 1s.

• EXE Program

– It is an executable program

Page 40: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Basics of a Typical C++ Environment

Phases of C++ Programs:

1. Edit

2. Preprocess

3. Compile

4. Link

5. Load

6. Execute Loader

Primary

Memory

Program is created in

the editor and stored

on disk.

Preprocessor program

processes the code.

Loader puts program

in memory.

CPU takes each

instruction and

executes it, possibly

storing new data

values as the program

executes.

Compiler

Compiler creates

object code and stores

it on disk.

Linker links the object

code with the libraries,

Creates an executable

file and stores it on disk

Editor

Preprocessor

Linker

CPU

Primary

Memory

.

.

.

.

.

.

.

.

.

.

.

.

Disk

Disk

Disk

Disk

Disk

Page 41: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

• Functions

– Function name

– Braces and the function body

– Main function

• Program Statements

• Output using cout

• Directives

– Preprocessor Directives #

– Header Files

– The ‘using’ Directive

– comments

Page 42: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

A basic program// ------------------------------------------------------------/ *hello.cpp is a demonstration programWelcome to C++*/

#include <iostream>using namespace std;

void main ( ) {

cout << "Hello C++ World ! \n";}

Preprocessor Directives/ Header File

The using Directive

Comments

Page 43: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

A basic program// ------------------------------------------------------------/ *hello.cpp is a demonstration programWelcome to C++*/#include <iostream>using namespace std;

void main ( ) {

cout << "Hello C++ World ! \n";}

The main() function

Standard output stream

Insertion Operator

String Constant

Escape Sequence

End of a statement

Page 44: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

A basic program

• Programs typically contain the following

elements:– Descriptive comments (double forward slash // or /* … */)

– Include files

– Functions including exactly one main function

• The main function– Controls the execution of the program

Page 45: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Output Statements

cout << “Hello C++ World”;

•cout is an object in c++, predefined to display the standard output stream

•<< insertion operator

– It directs the contents of the variable on its right to the object on its left

•The output of this program – Hello C++ World

Page 46: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Output

Page 47: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

Escape Sequences

Escape Sequence Character

\a Bell (beep)

\b Backspace

\n Newline

\r Return

\t Tab

\\ Backslash

\‘ Single quotation mark

\“ Double quotation marks

Page 48: Fundamentals of Programming CS-114 · and Structures in C++ 1 C3 CLO 3 Analyse real life problems and relate their knowledge of programing and algorithms to solve those complex problems

48

Acknowledgements

1. Deitel and Deitel: C++ How to Program, 7th Edition, Prentice Hall Publications

2. Robert Lafore: Object-Oriented Programming in C++, Fourth Edition,

December 2001,Sams Publishing .

3. A Structured Programing Approach Using C++ by Behrouz A. Forouzan

4. www.cplusplus.com

Mat

eria

l in

th

ese

slid

es h

as b

een

tak

en f

rom

, th

e fo

llow

ing

reso

urc

es