Professional Documents
Culture Documents
Lecture 1 - Introduction
Types of Computer Languages:
1. Machine language computers natural language w/c is in binary string of 0s & 1s that specifies
an operation.
2. Assembly languages English-like abbreviations
3. High-level languages everyday English and contain commonly used mathematical notations.
(C, C++,Fortran, BASIC, COBOL)
OOP (Object-Oriented Programming)
A type of programming in which programmers define not only the data type of a data structure,
but also the types of operations (functions) that can be applied to the data structure. In this way, the
data structure becomes an object that includes both data and functions.
Lecture 2 Basic Functions
Steps in Preparing a Program for Execution
1. Use an editor or a word-processor program to enter each line of the source program into
memory & save (filename.c).
2. Use a compiler program to translate the source program into machine language. If there are
syntax errors, they are displayed in the monitor. Use editor to correct the errors.
3. When the source program is free of syntax errors, the compiler then saves it as an object
program (filename.o).
4. The linker program combines your object program w/ additional object files that may be needed
to the program.
5. The loader program places the executable file in memory, ready for execution (filename.exe).
The C standard library provides the following:
macros
type definitions
functions
string handling
mathematical computations
input/output processing
memory allocation
Preprocessor Directives
o #include - Insert specified files <header filename>,etc. (<stdio.h>,<math.h>,<stddef.h>)
o #define - permanently assign a value for a particular variable within a C program.
o #ifdef, #ifndef - Conditionally compile code
*Preprocessor directives can appear anywhere in a source file, but they apply only to the remainder of
the source file
Comments are sequence of characters ignored by the compiler; used for documentation of the
program; cannot be nested (/*comments*/ or //comment line)
The main () function marks the beginning and end of program execution. The main function
usually controls program execution by directing the calls to other functions in the program.
Declaration syntax: int main ( )
o Restrictions:
note: {} Braces
[] Brackets
()Parentheses
Algorithms -a procedure for solving a problem in terms of 1.) the actions to be executed, and 2.)
the order in which these actions are to be executed
Program Control -specifying the order in which statements are to be executed in a computer
program
Pseudocode is an artificial and informal language that helps you develop algorithms
Compound Statements set of statements within a pair of braces {}.
Control Structures enable the programmer to control the flow of the program and the
sequence by which the inputted commands are to be executed
*Means of Repetition
1. Counter-Controlled (Definite) Counter-controlled variable counts the number of specified
repetitions
2. Sentinel-Controlled (Indefinite) Sentinel serves as the lookout/guard since the no. of
repetitions is unknown or is dependent on data that will be inputted by the user (sentinel value).
Repetition Structures
1. For -repeats a statement or compound statements a specified number of times
Syntax: for (initialization; test condition; increment)
Statements
2. While -executes a statement or a block of statements until a specified expression evaluates to
false
Syntax: initialization;
while(test condition)
Statements
3. do-while -similar with while except that the condition is evaluated after each execution of the
loop; executes the statement at least once
Initialization;
do
{
statements;
Decrement/increment of counter;
}
while (condition);
LECTURE 4 C FUNCTIONS
Stacks are known as last-in, first-out (LIFO) data structuresthe last item pushed (inserted)
on the stack is the first item popped (removed) from the stack. The program execution stack
contains the memory for the local variables used in each invocation of a function during a
programs execution.
Base Case- simplest case
A function prototype declares the return type of the function and declares the number, the types,
and order of the parameters the function expects to receive.
Three ways a function can return control to the caller
1. Function does not return a value, control is returned when the function-ending right brace is
reached
2. By executing the statement return;
3. If the function does return a value, the statement return expression; returns the value of
expression.
The arguments passed to a function should match in number, type and order with the
parameters in the function definition
LECTURE 5 ARRAYS
An array is a collection of variables of the same data type that are referenced by the same name.
The individual elements of the array are referenced by appending a subscript to the array name
(array index) defines the position of the individual element in the array. A subscript must be an
integer or an integer expression.
The elements of an array can be initialized when the array is defined by following the definition
with an equals sign and braces, {}, containing a comma-separated list of initializers Array
declaration:
type name[ size ] = {values,} ; not defined = 0
The statement int n[10] = {0}; explicitly initializes the first element to zero and initializes the
remaining nine elements to zero
Integer constant or constant expressions are used to declare the size of an array
Array elements are indexed from 0 to size-1. The first element in every array is the zeroth element.
We cannot assign an entire array to another array, but we can assign individual elements of an
array to Arrays and Loops individual elements of another array. (Use for and while loops) The loop
control variable is used as the array subscript.
Sorting data (i.e., placing the data into a particular order such as ascending or descending) is one
of the most important computing applications. Bubble sort or the sinking sort is a commonly used
sorting method. (compares array values and uses a temporary variable to swap them)
In declarations of multidimensional arrays that have an initializer-list, the constant-expression that
specifies the bounds for the first dimension can be omitted. double matrix2[ ][3] = {}
A string is an array of characters. char name[int]; %s format. A string literal is a character
sequence enclosed within double quotes.
Every string contains a special string-termination character called the null character. The character
constant representing the null character is '\0'. A character array representing a string should
always be defined large enough to hold the number of characters in the string and the terminating
null character.
An array name is the address of the start of the array; therefore, the & is not necessary
LECTURE 6 POINTERS
Pointer a variable which holds the address of the memory block which contains the value of a
variable in the computer; Declaration: pointer_type *pointer_name;
Functions of a pointer
1. Fast reference to array elements
2. Provides another means to call functions
3. Dynamic Memory Allocation
Address-of Operator (&) - returns the memory address of its operand; the address-of operator
can be applied only to the following: 1.) L-values and 2.) Functions.
Indirection Operator (*) - returns the value pointed by the operand
A pointer can be assigned to another pointer if both have the same type. The exception to this is the
pointer to void (i.e., void *), which is a generic pointer that can represent any pointer type
POINTER ARITHMETIC. A pointer may be incremented (++) or decremented (--), an integer may
be added to a pointer (+ or +=), an integer may be subtracted from a pointer (- or -=) and one
pointer may be subtracted from another.
A pointer to a function contains the address of the function in memory.
Principle of least privilege: Always award a function enough access to the data in its parameters
to accomplish its specified task, but no more.
Use pointers and the indirection operator to simulate call-by-reference
The const qualifier enables you to inform the compiler that the value of a particular variable should
not be modified. (compilation error)
The name of an array can be considered as a pointer to the beginning of a memory block as big as
the array
A string is accessed using a pointer to its first element; The value of the string can be accessed
through the address of its first character
We can access the contents of the array through the pointer as if the pointer itself was that array.
Ex. *p2=4;