Computorient 2013 slides

Page 1

COMPUTORIENT 2013 A free C Tutorial for incoming freshies


Introduction to Programming 

Some terminologies 

Program   

Set of instructions Written to perform a specific task To yield a specific output

Programming 

“Art of designing and implementing the operations to be performed by the computer to solve a particular problem to accomplish a specific task.”


Introduction to Programming 

Some terminologies 

Algorithm  

Series of steps Instructions

Code 

Program text


Introduction to Programming 

Some terminologies 

Pseudocode   

“pseudo” = fake or pretend Pseudocode = mix between the English language and code Intermediary

Library 

Collection of already defined functions


Introduction to Programming 

Some terminologies 

Compiler 

“used to convert programs from a source language to a target language

Human Language

Coding

(Pseudocode) Programming Language

Compiling

Machine Language


Introduction to Programming 

Some terminologies 

Bugs  

Program Error Things that cause the program to behave differently

Debugging 

Process of removing these errors


The C Programming Language (Perhaps) Your first programming language!


About the C Programming Language 

 

Imperative (procedural) Programming Language Influenced C++ and Java Initially used for Systems Programming


Program Structure #include <stdio.h>

Pre-processor Directive

int main () { printf(“\nHello World!!\n�); return 0; }


Program Structure 

Pre-processor Directive   

instructions to the computer before your code is compiled in the case in the previous slide, it tells the computer to get code from a certain library can also have: #define (usually used to define constants) 

Ex. : #define PI 3.14


Program Structure #include <stdio.h> Main Function Declaration int main () { printf(“\nHello World!!\n�); return 0; }


Program Structure 

Main Function Declaration   

“int” signifies the type of return value “main” signifies that the function is the main function “()” signifies an empty function parameter. It does not need any arguments.


Program Structure #include <stdio.h> int main () { printf(“\nHello World!!\n”); return 0; } Function Body


Program Structure 

Function Body   

Contains the program proper: executable statements, declarations, etc. The “return 0;” signifies the return value needed by the function. Take note that the whole program block is delimited by braces {}.


Basic Input-Output (User Interaction!)


“printf” 

 

Used to print to the standard output stream (console) From the <stdio.h> library Escape sequences: Name

Symbol

Newline

\n

Tab

\t

Backslash

\\

Question Mark

\?

Single Quote

\’

Double Quote

\”


“scanf”   

Used to get input from the user from the standard input stream (keyboard) From the <stdio.h> library Format:  scanf(“placeholder”, &address); The “address” in the format requires the name of the variable in which the value from the user is placed Placeholders: Whole Numbers

%d

Decimals

%f

Single Character

%c

Multi-characters

%s


Expressions (declarations, executable statements, etc.)


Variable Declarations 

Variables hold the data that your program needs! 

Global Variables:  

Local Variables  

declared outside of the main function accessible throughout the whole program

declared inside a specific block accessible only in that block

Syntax: 

<variable type> <variable name/s>


Variable Declarations 

Variable Types (data types) 

int  

char  

known as the whole numbers stores negative and non-negative values

single characters stores alphanumeric values and symbols

float 

stores numbers with fractional parts


Variable Declarations 

Valid Variable Names  

should start with either an underscore or a letter can be followed by digits, letters, underscores, or nothing at all 

Ex.: myVariable, a, _123, _, XYZ, helloPhowsz1234

Note: Variable names are case-sensitive in C, therefore, abc and ABC stand for two different variables!


Operators 

Logical  

usually used to compare two conditional statements returns either a true expression or a false one Logical Operators AND (&&)

Evaluates to true if both left hand side and right hand side of the expression evaluates to true. False, otherwise.

OR (||)

Evaluates to true if at least one of the sides evaluates to true. False, otherwise.

NOT (!)

Evaluates to true if the statement it negates is false. False, otherwise.


Operators 

Arithmetic Arithmetic Operations Addition

+

Subtraction

-

Multiplication

*

Division

/

Modulo

%

*modulo operation 

evaluates to the remainder when two numbers are divided 

Ex.: 10%3 results to 1


Operators 

Relational   

compares two values or expression usually used for conditional statements evaluates to true or false Relational Operators Greater than

>

Less than

<

Greater than or equal to

>=

Less than or equal to

<=

Equal to

==

Not equal to

!=


Operators 

Additional Notes:  

To signify a negative value, simply put a “-” before the value to be negated. (Unary operator) Do not confuse “==” with “=” 

“==”  

relational operator tests equivalence between two values

“=”  

assignment operators most often used to initialize variables or to change the value they hold


Operators 

Additional Notes: 

Comments exist in C!   

They are expressions that are part of your program code but are disregarded by the computer Multi-liner comment starts with /* and ends with */ single liner comment begins with a //


Operators 

Shortcuts 

x = x + y can be written as x+=y

x = x - y can be written as x-=y

x = x * y can be written as x*=y

x = x / y can be written as x/=y

x = x % y can be written as x%=y

x = x + 1 can be written as x++/++x

x = x - 1 can be written as x--/--x


Control Structures


3 types 

Sequential 

Selection 

code is executed in order. From top to bottom the flow of the program depends on the truth value of conditional statements

Repetition 

a code/block of code is repeated until a specific condition is met


Selection 

if statement 

Syntax: 

if (conditional expression) { *code* }


Selection 

else   

requires an if statement executes when the condition in the if statement is not satisfied Syntax: 

if (a > b) { *code* } else { *code* }


Selection 

switch statement  

used when a lot of cases are possible Syntax: 

switch (control) { case 1: *code* case 2: *code* ... default: *code* }


Repetition 

while statement 

Syntax: 

while (condition) { *code* }

do-while statement 

Syntax: 

do { *code* } while (condition) ;


Repetition 

for statement 

Syntax: 

for (ini; condition; *code* }

upd) {

* ini: initialization (loop counter) * upd: update of the counter


ď ˝

Flow chart

Start

Input base and height

Input number Input width and length

Output area Output area and draw rectangle End


Arrays


Definition 

A collection of multiple elements of the same type Parts: 

<data type> <name>[size(index)]  Ex.: int array[10];

Index - a number used to access an element in the array Element - each of the variable in an array


Definition 

Arrays are zero-indexed. Meaning you start accessing the elements with ‘0’. And you end with n-1, n being the size of the array. Syntax for declaring an array: 

data_type variable_name[size_of_array];


One Dimensional Array  

A simple collection of elements that can be accessed individually with one index value. Example array declaration:  

int x[50]; //50 integer variables accessed from x[0] to x[49] char name[10]; //10 character variables accessed from name[0] to name[9]


Multi-dimensional Array 

Can be thought to be “arrays of arrays” Sample declarations:  

int point [10][10][10] //3d array float scores[5][10] //2d array


Accessing Arrays  

Use the value of the index that corresponds to the cell that we want to access Ex. 

int x[8] = {20,40,67,89,15,23,0}  to access an element of an array, we use the value of the index that corresponds to it  

x[0] would give us 20 x[7] would give us 0


Strings


Definition 

An array of characters terminated by a null character ‘\0’ - signifies the special character meaning null. 

the last element in a string is always occupied by this special character!


Definition 

A string cannot be initialized like other variables 

char event[20] = “computorient”; 

this initialization is not possible

String should be initialized by initializing each character in the array Strings have a special library for strings named “string.h”


Initializing strings 

char name[6]; //this would allow a string of length 5      

name[0] name[1] name[2] name[3] name[4] name[5] 

= = = = = =

‘a’; ‘b’; ‘b’; ‘e’; ‘y’; ‘\0’;

this string would contain the name “abbey”


Initializing strings 

A more common way of initializing strings would be using the strcpy() function inside the string.h library 

char event[20]; strcpy(event,”computorient”);


Initializing strings 

Other commonly used string functions 

strcmp() - used for comparing strings 

strcmp(“banana”, “potato”) would return a negative value because banana comes first before potato (alphabetically)

strlen() - used for getting the length of a string 

char sample[20]; strcpy(sample, “youandme”); int x = strlen(sample); //x would have a value of 8


Functions


Definition 

 

A block of code that has a name and property It can be executed countless times. Ex.: 

int prod (int x, int y) { int ans = x*y; return ans;

} 

calling this function would return the product of two integers.


Syntax and Parts 

return_value function_name (parameters) { function_body }  return value - signifies the type of data returned by the function  name - used to call a function  body - contains the executable code  parameters - data needed in the body


Function Prototype 

 

A declaration of a function without a body Usually found before the main function Full declaration is found after the main function


Function Prototype int prod (int x, int y); //prototype int main () { prod (5,4); } int prod (int x, int y) { //function proper int ans = x*y; return ans; }


ME2 Flowchart

Start

Input string

Calculate using different functions

Output values

End


THE END Thank you very much! 


CREDITS Contributors: Abbey Quinones Rodolfo Kirong Camille Salazar Jasper Martija


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.