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

Prog Lecture 1

The document provides an introduction to programming concepts, focusing on C programming language fundamentals, including variables, data types, and control structures. It explains the differences between machine language, assembly language, compilers, and interpreters, as well as the benefits of high-level programming languages. Additionally, it covers procedural programming principles, modularity, and the significance of C in modern programming applications.

Uploaded by

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

Prog Lecture 1

The document provides an introduction to programming concepts, focusing on C programming language fundamentals, including variables, data types, and control structures. It explains the differences between machine language, assembly language, compilers, and interpreters, as well as the benefits of high-level programming languages. Additionally, it covers procedural programming principles, modularity, and the significance of C in modern programming applications.

Uploaded by

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

ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming

Learning Outcomes

By completion of this lecture, we expect to achieve the following learning outcomes:

1. Understanding the Basics: You will gain a solid understanding of the fundamental concepts
of C programming, including variables, data types, operators, control structures (such as
loops and conditionals), functions, and basic input/output operations. You will be able to
write simple C programs that demonstrate your comprehension of these foundational
elements.
2. Programming Logic: You will develop your programming logic and problem-solving skills
through various exercises challenges. You will learn how to break down problems into
smaller, manageable tasks and implement algorithms using C programming constructs.

What is Programming?

Computer programming (often shortened to programming or coding) is the process of


designing, writing, testing, debugging, and maintaining the source code of computer programs.

This source code is written in a programming language.

The purpose of programming is to create a program that exhibits a certain desired behavior.

The process of writing source code often requires expertise in many different subjects, including
knowledge of the application domain, specialized algorithms and formal logic.

What is Machine Language?

The lowest-level programming language (except for computers that utilize programmable
microcode) Machine languages are the only languages understood by computers.

While easily understood by computers, machine languages are almost impossible for humans to
use because they consist entirely of numbers. Programmers, therefore, use either a high-level
programming language or an assembly language.

An assembly language contains the same instructions as a machine language, but the instructions
and variables have names instead of being just numbers.

Page 1 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming

Programs written in high-level languages are translated into assembly language or machine
language by a compiler.

Assembly language programs are translated into machine language by a program called an
assembler.

Assembly Language?

A programming language that is once removed from a computer's machine language. Machine
languages consist entirely of numbers and are almost impossible for humans to read and write.
Assembly languages have the same structure and set of commands as machine languages, but they
enable a programmer to use names instead of numbers.

Each type of CPU has its own machine language and assembly language, so an assembly language
program written for one type of CPU won't run on another. In the early days of programming, all
programs were written in assembly language.

Now, most programs are written in a high-level language such as FORTRAN or C. Programmers
still use assembly language when speed is essential or when they need to perform an operation that
isn't possible in a high-level language.

What is a Compiler?

A program that translates source code into object code.

The compiler derives its name from the way it works, looking at the entire piece of source code
and collecting and reorganizing the instructions.

Thus, a compiler differs from an interpreter, which analyzes and executes each line of source code
in succession, without looking at the entire program.

The advantage of interpreters is that they can execute a program immediately. Compilers require
some time before an executable program emerges.

Page 2 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming

However, programs produced by compilers run much faster than the same programs executed by
an interpreter.

Every high-level programming language (except strictly interpretive languages) comes with a
compiler.

In effect, the compiler is the language, because it defines which instructions are acceptable.

Because compilers translate source code into object code, which is unique for each type of
computer, many compilers are available for the same language.

For example, there is a FORTRAN compiler for PCs and another for Apple Macintosh computers.
In addition, the compiler industry is quite competitive, so there are actually many compilers for
each language on each type of computer.

More than a dozen companies develop and sell C++ compilers for the PC.

What is an Interpreter?

A program that executes instructions written in a high-level language. There are two ways to run
programs written in a high-level language.

The most common is to compile the program; the other method is to pass the program through an
interpreter.

An interpreter translates high-level instructions into an intermediate form, which it then executes.
In contrast, a compiler translates high-level instructions directly into machine language.

Compiled programs generally run faster than interpreted programs.

The advantage of an interpreter, however, is that it does not need to go through the compilation
stage during which machine instructions are generated.

Page 3 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming

This process can be time-consuming if the program is long. The interpreter, on the other hand, can
immediately execute high-level programs.

For this reason, interpreters are sometimes used during the development of a program, when a
programmer wants to add small sections at a time and test them quickly.

In addition, interpreters are often used in education because they allow students to program
interactively.

Both interpreters and compilers are available for most high-level languages.

However, BASIC and LISP are especially designed to be executed by an interpreter. In addition,
page description languages, such as PostScript, use an interpreter.

Every PostScript printer, for example, has a built-in interpreter that executes PostScript
instructions.

What are High-Level Languages?

A programming language such as C, FORTRAN, or Pascal that enables a programmer to write


programs that are more or less independent of a particular type of computer.

Such languages are considered high-level because they are closer to human languages and further
from machine languages.

In contrast, assembly languages are considered low-level because they are very close to machine
languages.

The main advantage of high-level languages over low-level languages is that they are easier to
read, write, and maintain.

Ultimately, programs written in a high-level language must be translated into machine language
by a compiler or interpreter.

The first high-level programming languages were designed in the 1950s.

Now there are dozens of different languages, including Ada, Algol, BASIC, COBOL, C, C++,
FORTRAN, LISP, Pascal, and Prolog.

Classification of High-level Languages

1. Commercial – COBOL (Common Business Oriented Language) meant to build business


applications
2. Scientific – FORTRAN (FORmula TRANslation), ALGOL (ALGOrithmic Oriented
Language) meant to build scientific applications

Page 4 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming

3. Special Purpose – CSL for simulation apps, ADA for real-time apps, SQL (Sequential
Query Language) for databases
4. General Purpose – BASIC (Beginners All Purpose Symbolic Instruction Code), Pascal, C,
C++ - popular for training beginner of programming because is nearer to English language.
Java, Small talk – popular for object oriented programming.

What is C Programming?

C programming is a widely used general-purpose programming language that was originally


developed in the early 1970s by Dennis Ritchie at Bell Labs.

It is a procedural programming language, which means that programs written in C are composed
of a series of steps or procedures to be executed in order.

C is known for its efficiency, flexibility, and low-level control over computer hardware, making it
suitable for systems programming and embedded systems.

Key Features of C programming:

1. Efficiency: C allows for direct memory manipulation and provides low-level access to
computer hardware, enabling efficient execution of programs.
2. Portability: C programs can be written to be highly portable, meaning they can be compiled
and executed on different computer systems with little or no modification.
3. Modularity: C supports modular programming, allowing code to be organized into reusable
modules, functions, and libraries.
4. Syntax: The syntax of C is relatively simple and compact, making it easy to read and write
C code.
5. Standard Library: C comes with a standard library that provides a set of functions and
macros for common operations, such as input/output handling, string manipulation, and
memory allocation.

C has had a significant influence on the development of many other programming languages, and
it is still widely used today in various domains, including operating systems, embedded systems,
game development, and high-performance computing.

Why Use C?

There are several reasons why C is commonly used for programming:

1. Efficiency: C is a low-level language that provides direct access to memory and hardware
resources. This allows programmers to write highly efficient code with fine-grained control
over system resources. C programs can be optimized for speed and memory usage, making
it suitable for performance-critical applications.

Page 5 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming

2. Portability: C programs can be compiled and executed on different platforms with minimal
modifications. The C language is standardized, and compilers are available for various
operating systems and hardware architectures. This portability makes it easier to develop
software that can run on different devices and platforms.
3. Hardware Control: C allows programmers to directly manipulate hardware components,
such as memory addresses and I/O ports. This level of control is essential for system-level
programming, device drivers, and embedded systems, where interaction with hardware is
necessary.
4. Extensive Libraries: C has a rich set of libraries that provide pre-written functions and
modules for common tasks, such as file handling, string manipulation, and mathematical
operations. These libraries make development faster and more convenient by providing
ready-to-use solutions.
5. Procedural Language: C follows a procedural programming paradigm, which breaks down
programs into smaller, modular functions. This approach promotes code reusability,
maintainability, and readability. Procedural programming is well-suited for developing
structured and organized codebases.
6. Legacy Code: C has been around for several decades and has a vast codebase in many
industries. Many existing systems and libraries are written in C, making it necessary to use
C for maintaining and integrating with legacy code.
7. Learning and Understanding: C is often taught as an introductory language in computer
science and programming courses. Learning C helps in understanding fundamental
programming concepts, memory management, and the inner workings of a computer.

While C has many advantages, it's important to note that it is a lower-level language compared to
more modern languages.

This means that programming in C often requires more manual memory management and attention
to detail, which can make it more prone to certain types of errors if not handled carefully.

Nonetheless, the efficiency, control, and portability offered by C make it a popular choice for
various applications.

What compiler to be used in this class?

CodeBlock Version 3.XX and above

What is Procedural Programming?

Procedural programming is a programming paradigm that focuses on organizing code into a series
of procedures or functions.

Page 6 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming

In procedural programming, a program is composed of a collection of procedures or subroutines


that perform specific tasks.

These procedures are executed in a sequential manner, where one procedure calls another to
accomplish a particular goal.

Key characteristics of procedural programming include:

1. Procedures: Procedures are self-contained blocks of code that encapsulate a specific set of
instructions. They take input, perform operations, and produce output. Procedures can be
reused and called multiple times from different parts of the program.
2. Sequential Execution: Procedural programming follows a linear execution flow, where
instructions are executed in the order they appear in the code. The program starts from the
beginning and proceeds sequentially, with control flowing from one procedure to the next.
3. Modularity: Procedural programming emphasizes modularity and code reusability.
Breaking down a program into smaller procedures allows for easier maintenance,
debugging, and testing. It also promotes code organization and makes it more manageable
as the program grows.
4. Data and Procedure Separation: Procedural programming separates data and procedures.
Data is typically stored in variables, and procedures operate on this data by passing it as
arguments or returning values.
5. Limited Code Reuse: Procedural programming lacks some of the code reuse mechanisms
found in other paradigms, such as object-oriented programming. Reusability is mainly
achieved through the use of procedures, where common functionality can be extracted into
separate procedures and called from different parts of the program.
6. Global Data: In procedural programming, data can be shared globally across procedures,
allowing for communication and data exchange between different parts of the program.
However, excessive use of global data can lead to potential issues, such as code complexity
and difficulty in understanding and debugging.

Procedural programming is often used for developing small to medium-sized programs, command-
line utilities, and scripts.

It provides a straightforward approach to programming, making it easy to learn and understand. C,


Pascal, and Fortran are examples of programming languages that follow the procedural
programming paradigm.

However, it's important to note that procedural programming is just one of many programming
paradigms, and different paradigms have different strengths and weaknesses.

Procedures and modularity

Modularity is generally desirable, especially in large, complicated programs. Inputs are usually
specified syntactically in the form of arguments and the outputs delivered as return values.

Page 7 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming

Scoping is another technique that helps keep procedures strongly modular. It prevents the
procedure from accessing the variables of other procedures (and vice-versa), including previous
instances of itself, without explicit authorization.

Less modular procedures, often used in small or quickly written programs, tend to interact with a
large number of variables in the execution environment, which other procedures might also
modify.

Because of the ability to specify a simple interface, to be self-contained, and to be reused,


procedures are a convenient vehicle for making pieces of code written by different people or
different groups, including through programming libraries.

Comparison with imperative programming

The focus of procedural programming is to break down a programming task into a collection of
variables, data structures, and subroutines, whereas in object-oriented programming it is to break
down a programming task into classes with each "class" encapsulating its own methods
(subroutines).

The most important distinction is whereas procedural programming uses procedures to operate on
data structures, object-oriented programming bundles the two together so an "object", which is an
instance of a class, operates on its "own" data structure.

Nomenclature varies between the two, although they have similar semantics:

Comparison with functional programming

The principles of modularity and code reuse in practical functional languages are fundamentally
the same as in procedural languages, since they both stem from structured programming. So for
example:

 Procedures correspond to functions. Both allow the reuse of the same code in various parts
of the programs, and at various points of its execution.
 By the same token, procedure calls correspond to function application.
 Functions and their invocations are modularly separated from each other in the same
manner, by the use of function arguments, return values and variable scopes.

Page 8 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming

The main difference between the styles is that functional programming languages remove or at
least deemphasize the imperative elements of procedural programming. The feature set of
functional languages is therefore designed to support writing programs as much as possible in
terms of pure functions:

 Whereas procedural languages model execution of the program as a sequence of imperative


commands that may implicitly alter shared state, functional programming languages model
execution as the evaluation of complex expressions that only depend on each other in terms
of arguments and return values. For this reason, functional programs can have a freer order
of code execution, and the languages may offer little control over the order in which various
parts of the program are executed. (For example, the arguments to a procedure invocation
in Scheme are executed in an arbitrary order.)
 Functional programming languages support (and heavily use) first-class functions,
anonymous functions and closures.
 Functional programming languages tend to rely on tail call optimization and higher-order
functions instead of imperative looping constructs.

Many functional languages, however, are in fact impurely functional and offer
imperative/procedural constructs that allow the programmer to write programs in procedural style,
or in a combination of both styles. It is common for input/output code in functional languages to
be written in a procedural style.

Comparison with logic programming

In logic programming, a program is a set of premises, and computation is performed by attempting


to prove candidate theorems.

From this point of view, logic programs are declarative, focusing on what the problem is, rather
than on how to solve it.

However, the backward reasoning technique, implemented by SLD resolution, used to solve
problems in logic programming languages such as Prolog, treats programs as goal-reduction
procedures.

Experienced logic programmers use the procedural interpretation to write programs that are
effective and efficient, and they use the declarative interpretation to help ensure that programs are
correct.

What is Structured Programming?

Structured programming is programming that links control flow blocks (a function, an if


statement's block, etc.) to the scopes of variables.

A variable declared inside such a block is invisible outside it.

Page 9 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming

This leads to programs written with only the following code structures:

1. Sequence of sequentially executed statements.


2. Conditional execution of statements (i.e., "if" statements).
3. Looping.
4. Structured Subroutine calls (e.g., 'gosub' but not 'goto').
5. Stepwise Refinement

Structured Programming is commonly mistaken as Procedural Programming, although, it started


with Procedural Programming, but it can be applied to other Programming Paradigms (Example:
Object Oriented Programming).

Many applications described as coded with Structured Programming, are really built with a
Procedural Programming Language, that may be full, partially or none structured.

Structured Programming is a foundation of Modular Programming and Object Oriented


Programming, as it's assumed that individual methods are structured.

THE SUMMARY OF THE STEPS OF PRODUCING STRUCTURED PROGRAMS

1. Analyze the Problem

This involves specifying the program’s output, input, variables, constants and the general steps
(tasks) needed to transform the inputs into the outputs.

2. Design the Program using a Design Tool

A program’s design is a step-by-step description of how the above steps (in part 1) should be
exactly carried out so as to solve the problem. It represents the exact logic of the solution to the
problem. It includes all the appropriate formulae to be used in the solution. The design is also
known as an algorithm.

What are the qualities of a good algorithm?

 It should be clear, precise and easy to follow. This is important in avoiding errors, as well
as making the design modifiable by future programmers.
 A program’s design should also show the exact logic of the program. For the calculations
that need to be done, it should show the appropriate sequence of formulae.
 It should also be general i.e. not specific to a programming language, such that it can be
converted into a program using any programming language.

Page 10 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming

Two most used tools for designing a program are pseudo codes and flowcharts diagrams.

(a) Pseudo Codes – are English-like statements that look similar to many procedural-
programming languages.

e.g.

Input x

area = length * Width

(b) Flowchart Symbols – are symbols used to design a program. They are a diagrammatic way
of representing the program’s logic.

There are many flowchart symbols used in the design of programs.

Symbols Meaning

Begin, End

Process (e.g. calculations)

Input, Output

Shows logic flow

Decision making (Test condition inside. If true, follow the


down-ward arrow else follow the right-side arrow).

Page connectors. Used to connect diagrams to next


page.

3. Code the program

The algorithm produced in step (2.) above is then converted into actual program (code) using an
appropriate programming language e.g. C++.

The actual statements of the program are also known as the code of the program.

Page 11 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming

4. Compile (or convert it into a machine language program), Test and run

5. Maintain the program

This involves making changes to improve the functionality of the program (e.g. to improve on the
program), to remove possible previously undetected errors, to cater for newly discovered user
needs, or to cater for changed technology.

EXAMPLE

Write a program to compute the area and the circumference of a circle.

SOLUTION

(a) Analysis of the problem

(i) Outputs Variables

The area of a circle Area

The circumference of a circle Circumference

(ii) Inputs

The radius of a circle Radius

(iii) Tasks

 Input the radius


 Compute the area
 Compute the circumference
 Output the area and the circumference

Constant

The pi of a circle pi = 3.14

(b) The Program’s Design


Page 12 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming

(i) Pseudo code

BEGIN

pi=3.14

INPUT Radius

Area=pi*Radius*Radius

Circumference=2*pi*Radius

OUTPUT Area, Circumference

END

(ii) Flowchart Diagrams

Begin

pi=3.14

Input Radius

Area=pi*Radius*Radius

Circumference=2*pi*Radius

Output Area, Circumference

End

(iii) Code of the program

Example:

#include <stdio.h>

//Declare a constant for pi=3.14

#define PI 3.14

Page 13 of 14
ICS 1102 PROGRAMMING CONCEPTS Introduction to Programming

int main()

//Declare variable to hold the radius, area, circumference

double Radius, Area, Circumference;

//Input the radius

printf("\n Enter the radius of a circle: ");

scanf("%lf", &Radius);

//Compute the area and the circumference

Area = PI * Radius * Radius;

Circumference = 2 * PI * Radius;

//Outputs area, circumference

printf("\n Area of the circle: %lf Circumference of the circle is: %lf\n", Area, Circumference);

return 0;

Page 14 of 14

You might also like