You are on page 1of 6

CE26 ANALYTICAL AND COMPUTATIONAL METHODS IN CIVIL ENGINEERING I

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:

Cannot be declared as static.


Cannot have its address taken.
Cannot be called.
C Identifiers - are names that are used to refer to variables, functions, and various other user
defined objects.

The first character must be a letter or an underscore ( _ ); subsequent


characters being either letters, numbers, or an underscore (an exception is the dollar sign, $);
CASE-SENSITIVE
All variables must be declared prior to their use.
Data type modifiers: signed, unsigned, long, short
The modifiers signed, unsigned, long and short may be applied to characters and integer base
types. However, long may also be applied to double
Semicolon is called a statement terminator
printf function instructs the computer to print on the screen the string of characters (pointed by
format) marked by the quotation marks.
The (\) backslash is called an escape character; combined with the next character, creates an
escape sequence.
Common escape sequences \n (newline), \t (horizontal tab),\\ (insert backslash), \ (insert
double quote)
Table of Operators

Formatting using place holders, examples:


o %4d (occupy 4 spaces)
o %.2d (display up to 2 decimal places)
scanf function analogous to printf with input instead of output.
Example:
o scanf(%d, &int1);
o scanf("%c%c%c",&first, &middle, &last);

note: {} Braces

[] Brackets

()Parentheses

LECTURE 3 CONTROL STRUCTURES

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

3 Types of Control Structures


1. Sequence Structure
2. Selection Structure if, if-else, switch
3. Repetition/Recursive Structure for, while, do-while
Selection Structures
1. If (single-selection structure) allows a program to execute a (set of) command(s) if the
relational condition statement is satisfied (next line or statements in {})
2. If-else (double-selection structure) instructs the program what statements to execute if the
condition statement is satisfied and if it is not
3. Switch (multiple-selection structure) branches of decisions;
-substitute for nested if-else
-evaluates relational statements involving types int and char only (case sensitive)
-Commonly uses break and goto to terminate execution of statements

*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

Two Types of Functions


1. Standard Library Functions
2. User-defined Functions
Two-ways to call a function
1. Call by reference -used only with trusted called functions that need to modify the original
variable.
2. Call by value - should be used whenever the called function does not need to modify the
value of the callers original variable. (all calls in c are call-by-value)
Two Parts of a Function
1. Declaration introduces the name, return type, and parameters of a function; functions
must be first declared before being called.
2. Definition/Body -Contains the executable statements of the function and all its variable and
their declarations
Void keyword for functions without return values
Recursive Functions functions that calls itself directly or indirectly
Math Functions <math.h>
Scope and Lifetime of C Identifiers
1. File Scope - Such identifiers are known (i.e., accessible) in all functions from the point at
which the identifier is declared until the end of the file. Global variables, function
definitions, and function prototypes placed outside the main function all have file
scope
2. Body Scope - Block scope ends at the terminating right brace (}) of the block. Local
variables defined at the beginning of a function have block scope as do function
parameters (inputs), which are considered local variables by the function.
When blocks are nested, and an identifier in an outer block has the same name as an
identifier in an inner block, the identifier in the outer block is hidden until the inner block
terminates
Local Variables are variables know only inside the function they are called.
3. Function Scope - Labels (an identifier followed by a colon) are the only identifiers with
function scope. Labels are used in switch statements (as case labels) and in goto
statements; cannot be referenced outside the function body.
4. Function Prototype Scope The only identifiers with function-prototype scope are those used
in the parameter list of a function prototype.
Random Number Generation
1. rand () - defined within <stdlib.h>;- rand () generates an integer between 0 and RAND_MAX
(32767). Pseudorandom numbers
2. srand(seed); - function defined within <stdlib.h>; function rand actually generates
pseudorandom numbers, hence, the sequence repeats itself each time the program is
executed; seeds function rand to produce a different sequence of random numbers for
each execution of the program.
time() commonly used as seed of srand()

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)

ARRAYS AND POINTERS

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;

DYNAMIC MEMORY ALLOCATION reserving memory during execution

Makes use of malloc(), calloc(), realloc() and free() functions


Economical way of memory usage

You might also like