COMPUTER PROGRAMMING
COMPUTER PROGRAMMING
PROGRAMMING
𝓸 𝓭𝓲𝓫𝓪, 𝓷𝓪𝓴𝓪𝓴𝓪𝓹𝓾𝓽𝓪𝓷𝓰𝓲𝓷𝓪
● Processing - a box represents arithmetic instructions such as adding, subtracting, multiplication and division
● Decision - decision based operations such as yes/no question or true/false are indicated by diamond in
flowchart
● Connectors - used whenever flowchart becomes complex or it spreads over more than one page
○ On-page connectors use numbers (1. 2. etc) while Off-page connectors use capital letters (A. B. C. etc)
Rules
● Flowchart opening statement must be the ‘start’ keyword.
● Flowchart ending statement must be ‘end’ keyword.
● All symbols in the flowchart must be connected with an arrow line.
● The decision symbol in the flowchart is associated with the arrow line.
● Flow lines must be top to bottom and left to right.
● #include <stdio.h>
○ stdio.h = standard input output . header
○ printf function is defined in stdio.h
● int main()
○ main: to instruct that you are starting the program
● printf
○ Used to print data on the console
● return0
○ Returns execution status to the operating system
○ The 0 value is used for successful execution and 1 for unsuccessful execution
● {
○ Begin
● }
○ End
● Syntax
○ How the command is used
● scanf()
○ input
○ Reads input data from the console
● printf()
○ output
○ The format string can be %d (integer), %c (character), %f (float), etc.
● ;
○ signals the end of each line
Conversion
Binary to Decimal
Repetitive Division
● Example: 69
● 69/2 = 34.5 (1)
● 34/2 = 17 (0)
● 17/2 = 8.5 (1)
● 8/2 = 4 (0)
● 4/2 = 2 (0)
● 2/2 = 1 (0)
● 1/2 = 0.5 (1)
● Read from bottom to top, the answer is 1000101
Decimal to Binary
Example: 1000101
Powers Method
● (1*2^6) + (0*2^5) + (0*2^4) + (0*2^3) + (1*2^2) + (0*2^1) + (1*2^0)
● (1*2^6) + (1*2^2) + (1*2^0) [remove all with 0]
● 64 + 4 + 1 = 69
● The answer is 69
Partition Method (i think)
● 64 32 16 8 4 2 1
● 1 0 0 0 1 0 1
● 64 + 4 + 1 = 69
● The answer is 69
● strcmp- string comparison; it returns a value less than, equal to, or greater than zero based on the comparison
result
○ result = strcmp(string1,string2);
● strcpy- string copy; involves copying the contents of one string to another
○ strcpy(string1,string2);
Typecasting
● Explicit typecasting - requires the programmer to explicitly specify the desired data type
○ int var1 = (int) var2;
● Implicit typecasting - also known as automatic type conversion
○ int var1 = var2;
● String to integer or float - requires the header <stlib.h>
○ #include <stdlib.h>
○ char var2
○ int var1 or float var1
○ var2 = atoi(var2); or var2 = atof(var2);
Predefined Functions
● String
○ strlen - used to determine the length of a string; returns the number of characters in the string,
excluding the null terminator
■ length = strlen(str);
○ strchr - used to search for a character in a string; it returns a pointer to the first occurrence of the
character in the string, or NULL if the character is not found.
■ result = strchr(str,c);
○ strstr - used to search for a substring in a string; it returns a pointer to the first occurrence of the
substring in the string, or NULL if the substring is not found.
■ result = strstr(str,substring);
● Character
○ isalpha - checks whether a given character is an alphabetic character; returns a non-zero value if the
character is alphabetic, otherwise it returns zero
■ isalpha(c);
○ isdigit - function checks whether a given character is a digit; returns a non-zero value if the character is
a digit, otherwise it returns zero
■ isdigit(num1);
○ toupper/tolower - convert a character to uppercase and lowercase, respectively
■ toupper(a);
■ tolower(A);
○ isalnum - checks whether a given character is an alphabetic or digit character; returns a non-zero value
if the character is alphanumeric, otherwise it returns zero
■ isalnum(variable);
○ iscntrl- checks whether a given character is a control character (escape sequence)
■ iscntrl(‘\n’);
○ islower/isupper- determine whether a given character is a lowercase/uppercase letter; returns a non-
zero value if the character is in lowercase/uppercase, otherwise it returns zero
■ iupper(c);
■ islower(c);
○ isspace- checks whether a given character is a white space character (space, tab, newline, etc.); returns a
non-zero value if the character is a white space character, otherwise it returns zero
■ ispace(‘\n’);
○ isxdigit - used to determine whether a given character is a hexadecimal digit (1-5, A-F)
■ isxdigit(c);
○ ispunct- used to determine whether a given character is a punctuation character
■ ispunct(’?’);
○ isprint - used to determine whether a given character is a printable character; printable characters are
those that can be displayed on the screen or printed
■ ispunct(’A’);
● Math
○ sin and cos - used to calculate the sine and cosine of an angle, respectively; these functions take the
angle in radians as an argument and return the corresponding sine or cosine value as a double value.
■ double sinresult = sin(angle);
■ double cosresult = cos(angle);
○ pow - used to raise a number to a power; it takes two arguments: the base number and the exponent.
It returns the result as a double value.
■ result = pow(base,result);
○ sqrt - used to calculate the square root of a number; it takes a single argument and returns the square
root as a double value.
■ double result = sqrt(num);
Selection Structure
● If - allows you to execute a block of code only if a specified condition is true
○ if (condition) { }
● If else - extends the if statement by providing an alternative code block to be executed when the condition is
false
○ if (condition) { }
○ else { }
● Else if - allows you to evaluate multiple conditions in a sequential manner
○ if (condition) { }
○ else if { }
○ else { }
● Switch - allows you to select one of many code blocks to be executed based on the value of a variable or an
expression; provides an alternative to multiple if-else if statements when there are multiple possible values to
compare (does not have ranges)
○ switch (expression) {
○ case value1:
○ case value2:
○ break;
○ default: }
Conditional Statements
● == operator - output the result of the comparison, indicating whether a and b are equal (1 for true or 0 for false)
○ result = (a == b);
● != operator - output the result of the comparison, indicating whether a and b are not equal (1 for true or 0 for
false)
○ notzero = (a != b);
● > or < operator - output the result of the comparison, indicating whether a greater/less than b (1 for true or 0 for
false)
○ less = (a < b);
○ greater = (a > b);
Boolean Operators
● Logical operators - used to combine multiple conditions and create complex boolean expressions. They are
typically used within conditional statements such as if, while, and for to determine the flow of the program based
on the evaluation of these conditions.
○ AND (&&) - all statements must be true
■ answer = (num1 > num2 && num3 > num4);
○ OR (||) - one of the statements must be true
■ answer = (num1 >= num2 || num3 <= num4);
○ NOT (!) - statements must be false
■ answer = !(num1 > num2);
● Short-circuit evaluation - the second operand of an && or II operator is not evaluated if the outcome can be
determined solely by evaluating the first operand. This behavior can be useful in improving performance and
preventing unnecessary evaluations.