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

Lecture 1

c++

Uploaded by

tareq3174
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture 1

c++

Uploaded by

tareq3174
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

CSCI131

Programming I

Lecture 1:
Admin and Introduction

Rabe Abdalkareem, PhD


Research Interests
Software engineering

Mining Software Repositories

Software Maintenance

Empirical Software Engineering

Software Ecosystems

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 2


How much programming do you
know?
• C++?
• Coded something in C++, Java ..?
• Coded something in any programming
language?
• Know what is an int and a String?
• Know what an object is?

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 3


What is a software program?

• A step-by-step process used to solve a


problem using a specific programming
language…

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 4


Problem Solving
• The general steps in problem solving are:
▪ Understand the problem

▪ Design a solution (find an algorithm)

▪ Implement the solution (write the program)

▪ Test the program and fix any problems

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 5


Why learn to program?

• Solving most real-world problems


requires programming, or at least
knowledge of programming

• You have to as part of your degree!


‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 6
This Course

• Allows you to understand the basic principles


of programming

• Write C++ programs


• Develop algorithms to solve simple problems
• Be able to understand others’ code
• Think of the efficiency of programs
‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 7
Course Format
• Class and location:
• Saturday 8:00 AM – 10:00 PM at room 1.10

• Classes will explore one or more topics


• We will have a simple QUIZ at the end of
some classes.

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 8


Required Text Book

• Absolute C++ by Walter Savitch, 5th Edition or later,


Addison Wesley.

• The book is available in


• Digital Copy: ISBN: 978-0-13-283071-3

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 9


Course Format
• Labs

• will help you gain hands-on experience with the


topics covered in class.
• Read course outline

• will provide examples and exercises to reinforce


materials from class.

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 10


Evaluation

Practical exam 8%
Quizzes 7%
Midterm exam 30%
Attendance & Participation 5%
Final exam 50%

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 11


Evaluation
• ‫ → االمتحان الجزئي‬13th January 2024 Saturday

• ‫ → عملي‬There will be a practical exam.

• ‫ → اختبار‬There will be 1 or 2 Quizzes.

• ‫ → النهائي‬There will be a final exam.

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 12


Course Expectations
• Attend lectures and be punctual.

• Bring your ideas and participate in


discussions.

• Attend labs and do the assigned exercises


and assignments.

• Study for exams.


‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 13
‫”‪“Get your hands dirty‬‬

‫اعداد وتجميع‪ :‬د‪ .‬ربيع مفتاح العلواني‬ ‫‪14‬‬


Lateness Policy for All Course Deliverables

NO LATE
DELIVERABLES!!

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 15


Academic Integrity and Cheating
• Cheating, plagiarism and other forms of academic fraud are
taken very seriously by the University, the Faculty, and the
teaching staff.

• Example:
• Submitting the work of another person as your original work.
• Incorporating others work in your work and not referencing it.

• It is permitted and encouraged to discuss with your peers on


the whiteboard but NOT permitted to copy their solutions as
they talk to you. Both parties will be penalized.

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 16


Read the course outline!

CSCI131– Programming I
Course Outline – Fall 2023

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 17


‫?‪Questions about class logistics‬‬

‫اعداد وتجميع‪ :‬د‪ .‬ربيع مفتاح العلواني‬ ‫‪18‬‬


‫‪Introduction to programming‬‬

‫‪19‬‬
‫اعداد وتجميع‪ :‬د‪ .‬ربيع مفتاح العلواني‬ ‫‪19‬‬
Why do we need programs?
• Humans communicate in a natural language
• Large vocabulary (10 000s words)
• Complex syntax
• Semantic ambiguity

• Machines communicate in binary code /


machine language
• Small vocabulary (2 words… 1, 0)
• Simple syntax
• No semantic ambiguity

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 20


Translation of communication
Humans -- natural language Machines -- binary language
Large vocabulary Small vocabulary
Complex syntax Simple syntax
Semantic ambiguity No semantic ambiguity

Programming Compiler +
Interpreter

Programming language
Ex: ???

Vocabulary: restricted
Syntax: small and restricted
‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ Semantic: no ambiguity (almost) 21
Our Choice: C++
• Widely used.

• C++ economy: Adobe, Microsoft, Apple, MySQL

• Widely available.

• Continuously under development since early 1990s.

• Embraces full set of modern abstractions.

• Is an object-oriented programming (OOP)


language.

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 22


C++

• Low-level languages
• Machine, assembly
• High-level languages
• C, C++, ADA, COBOL, FORTRAN …

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 23


It’s not about the language!

Our approach
• Use C++.
• Develop general programming skills that
are applicable to many languages.

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 24


Compilers
• A compiler is a software tool that translates a
high-level-language program (e.g., C++
program) into an equivalent low-level-language
program.

• In the C++ language, the compiler


translates directly into machine language.

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 25


C++ Compiler

int main() 11100010010101001


{ 01010101010101010
// code required 10100000000100111
int a=2, b=4,s; C++ Compiler 11000011100001100
s = a + b; 01100011000011111
return 0; 11111100010101010
} 00101111111110000

File.cpp File.exe

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 26


Some definitions
• Algorithm:
• a step-by-step process for solving a problem
expressed in natural language

• Program:
• An algorithm expressed in a programming
language that follows a specific syntax

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 27


C++ Program Structure
• A simple C++ program:
• is made up of one main functions

• contains non or one / more functions (action)

• a function contains program


statements/instructions

• A C++ program always contains a function/method


called main
‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 28
C++ Program Structure
// comments about the program...

int main () Main method


{

// code required to be executed...

return 0;
}
MyProgram.cpp

file extension
What will be the output here??
‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 29
C++ Program Structure

C++ is case
Hello to C++ sensitive!
‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 30
‫)‪A Sample C++ Program (1 of 2‬‬

‫اعداد وتجميع‪ :‬د‪ .‬ربيع مفتاح العلواني‬ ‫‪31‬‬


‫)‪A Sample C++ Program (2 of 2‬‬

‫اعداد وتجميع‪ :‬د‪ .‬ربيع مفتاح العلواني‬ ‫‪32‬‬


Syntax and Semantics
• Syntax rules
• define how we can put together symbols, reserved
words, and identifiers to make a valid program.

• Semantics
• define what a statement means.

• A program that is syntactically correct is not necessarily


logically (semantically) correct.

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 33


Three (or Two) Types of Errors
• Compile-time (syntax) errors

• The compiler will find syntax errors and other basic


problems
• An executable version of the program is not
created

• Run-time errors
• A problem can occur during program execution
• Causes the program to terminate abnormally

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 34


Three types of errors …

• Logical (semantic) errors


• A mistake in the algorithm
• Compiler cannot catch them
• A program may run, but produce incorrect results

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 35


Just checking...

The hardest kind of error to detect in a program is a:

Syntax error
Run-time error
Logic error day
All of the abovehours?

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 36


Program Style

Bottom-line: Make programs easy to read and


modify...

• Comments
• Indentation

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 37


Comments
• also called inline documentation
• used to explain the purpose of the program and describe
processing steps (the algorithm)
• do not affect how a program works (are ignored by the
compiler)
• can take 2 forms:

// this comment runs to the end of the line Line

/* this comment runs to the terminating


symbol, even across line breaks */ Block

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 38


‫‪Comments - example‬‬

‫اعداد وتجميع‪ :‬د‪ .‬ربيع مفتاح العلواني‬ ‫‪39‬‬


When to comment
• Very difficult to determine, however, there are a few rules
of thumb:
• When you know your code needs to be explained
• You know others are not/less experienced with what
you did

• Note: make your comments worthwhile (don’t over crowd


the code with useless comments)

• Always strive for self documentation ☺

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 40


Indentation

• Spaces, blank lines, and tabs are called white space

• White space is used to separate words and symbols in a


program

• Extra white space is ignored

• Programs should be formatted to enhance readability,


using consistent indentation

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 41


‫‪Example 1 of bad indentation‬‬

‫اعداد وتجميع‪ :‬د‪ .‬ربيع مفتاح العلواني‬ ‫‪42‬‬


‫‪Example 2 of bad indentation‬‬

‫اعداد وتجميع‪ :‬د‪ .‬ربيع مفتاح العلواني‬ ‫‪43‬‬


Example 3 of good indentation

Note: Fonts, color, comments, and extra space are not


relevant in C++ language.
‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 44
Basic Program Development
Fix
Edit and Run-time & Logical errors
save program

Fix
Compilation
errors

Execute program and


Compile program
evaluate results

‫ ربيع مفتاح العلواني‬.‫ د‬:‫اعداد وتجميع‬ 45


‫‪Next class‬‬

‫‪C++ fundamentals‬‬

‫اعداد وتجميع‪ :‬د‪ .‬ربيع مفتاح العلواني‬ ‫‪46‬‬

You might also like