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

COMPUTER PROGRAMMING

The document provides an overview of programming design tools, including narratives, pseudocode, HIPO charts, and flowcharts, emphasizing the importance of logical flow in programming. It also details the C programming language, covering its structure, syntax rules, data types, and various operators, along with input/output operations and string handling techniques. Additionally, it discusses typecasting, predefined functions, and the significance of comments and constants in coding.

Uploaded by

Liam James Yu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

COMPUTER PROGRAMMING

The document provides an overview of programming design tools, including narratives, pseudocode, HIPO charts, and flowcharts, emphasizing the importance of logical flow in programming. It also details the C programming language, covering its structure, syntax rules, data types, and various operators, along with input/output operations and string handling techniques. Additionally, it discusses typecasting, predefined functions, and the significance of comments and constants in coding.

Uploaded by

Liam James Yu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

COMPUTER

PROGRAMMING
𝓸 𝓭𝓲𝓫𝓪, 𝓷𝓪𝓴𝓪𝓴𝓪𝓹𝓾𝓽𝓪𝓷𝓰𝓲𝓷𝓪

Programming Design Tools


Narratives
● Program logic is described and communicated through the use of words
● Procedures/steps may be narrated via complete sentences or phrases, sometimes numbered
Pseudocode
● Describes the logical flow of the solution to the problem through English-like code that closely resembles the
actual programming language to be used.
● There are no rigid syntax rules in pseudocode.
● More emphasis is placed in the logic of the solution rather than the syntax of the language.
HIPO Chart
● Stands for Hierarchical Input Process Output.
● It is a tool used for Problem Definition and Problem Analysis
● Helps clarify ambiguous portions of the specifications
Questions Asked
● What are the outputs required?
● What are the inputs needed to produce the outputs?
● What are the processes needed to transform the inputs to the desired outputs?
Guidelines
● Outputs should be determined first before inputs or processes. This establishes the objective and scope of the
solution.
● Limit the outputs to those that are required in the specifications.
● Outputs are easy to identify because they usually follow verbs like “display”, “print” or “generate”.
● The inputs clearly lead to the outputs
● Processes contain the actions taken to transform the inputs to outputs. This can be stated in English and does not
have to conform to any programming language.
● Actions on processes are executed sequentially, e.g., from top to bottom.
● Numbering the steps signifies the order of execution.
Flowchart
● Is used to graphically present the solution to a problem.
● It uses flowcharting symbols linked together in a “flow” that will arrive at the solution.
● The process of drawing a flowchart for an algorithm is known as “flowcharting”
Symbols
● Terminal - the oval symbol indicates Start, Stop and Halt in a program’s logic flow; the first and last symbols in
the flowchart
● Input/Output - a parallelogram denotes any function of input/output type; program instructions that take input
from input devices and display output on output devices

● 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)

● Flowlines - indicate the exact sequence in which instructions are executed

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.

Programming Language Overview


C Programming
Made in the 1970s by Dennis Ritchie at Bell Laboratories. C is a fundamental language for system programming, embedded
systems, and high-performance applications.
Basic Structure
● Header Files - include statements for accessing external code in libraries.
○ <stdio.h> - standard input/output; provides input and output functions like printf and scanf
○ <stlib.h> - standard library; contains functions involving memory allocation, rand function, and other
utility functions
○ <string.h> - string; includes functions for manipulating strings, such as strcpy, strlen, etc
○ <math.h> - math; includes mathematical functions like sqrt, sin, cos, etc
○ <ctype.h> - c type; functions for testing and mapping characters, like isalpha, isdigit, etc
● Global Declarations - variables and functions accessible throughout the program.
● main() Function - entry point where the execution begins.
● Local Declarations - variables and functions used within a specific block or function.
● Statements - commands to be executed, enclosed within functions.
Syntax Rules
● Case Sensitivity - C is case-sensitive
● Semicolons - Each statement ends with a semicolon
● Braces [ ] - Used to group statements in functions or control structures
Data Types
● Basic Types
○ Int - numbers with positive/negative value
○ Char - used for alphanumeric and other characters
○ Float - numbers with decimals
○ Double - also has decimals, more storage than int or float
● Derived Types
○ Pointers, arrays, structures, unions
● Enumeration Types
○ User-defined data type (enum)
Comments
● Comments are used to describe what the code is doing and are ignored by the compiler. Single-line comments
start with //, and multi-line comments are enclosed between /* and */.
Constants
● Constants are variables whose values cannot be altered during the program's execution. They can be defined
using #define preprocessor or const keyword (ex: const int num1;).
Reserved Words
● Reserved words or keywords have special meaning in C and cannot be used as identifiers (variables). Examples
include int, return, if, while, etc.
Variables
Declaring Variables
● Begin with a Letter or Underscore
● Alphanumeric Characters
○ After the initial letter, the variable name can consist of letters, digits, and underscores.
● No Reserved Words
○ Variable names cannot be the same as reserved keywords in C (like int, return, etc.).
● Case Sensitivity
Types of Variables
● Local Variable
○ Declared inside the function/block
○ Must be at the start of the block
○ Local variable must be initialized before it is used
● Global Variable
○ Declared outside the function/block
○ Any function can change its value
○ Available to all functions
○ Must be at the start of the block
● Static Variable
○ Retains its value between multiple function calls
● Automatic Variable
○ All variables in C that are declared in the block are automatic by default
○ Can be explicitly declared with the keyword “auto”
● External Variable
○ Variables in multiple C source files can be shared with this
○ Uses “extern” keyword
Usual C Program
C program

● #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

Actual Code Stuff


Output Operations
● printf - displays output
○ printf(“This is how you display something”);
Escape Sequences
● \n - newline; moves the cursor to the beginning of the next line.
● \t - tab; inserts a horizontal tab.
● \" - double quote; inserts a double quote character.
● \' - single quote; inserts a single quote character.
● \\ - backslash; inserts a backslash character.
● \b - backspace; moves the cursor back one position.
● \r - carriage return; moves the cursor to the beginning of the current line.
● \f - form feed; moves the cursor to the next logical page.
Placeholders/Format Specifiers
● %d or %i - Signed integer
● %u - Unsigned integer
● %f - Floating-point number
● %c - Character
● %s - String
● %p - Pointer address
● %x or %X - Hexadecimal number
Input Operations
● scanf - obtains input from the user
○ scanf(“%s”, &variablename);
● fgets - obtains string input from the user
○ fgets(variablename, sizeof(variablename), stdin);
Arithmetic Operators
● Addition (+) - adds two values together.
○ sum = num1 + num2;
● Subtraction (-) - subtracts one value from another.
○ difference = num1 - num2;
● Multiplication (*) - multiplies two values.
○ product = num1 * num2;
● Division (/) - divides one value by another.
○ quotient = num1 / num2;
● Modulus (%) - computes the remainder after division.
○ mod = num1 % num2;
● Increment (++) - increases the value of a variable by 1.
○ incre = num1 ++ num2;
● Decrement (--) - decreases the value of a variable by 1.
○ decre = num1 -- num2;
Assignment Operators
● Basic Assignment Operator - used to assign a value to a variable; it assigns the value on the right-hand side to
the variable on the left-hand side
○ int num1 = 420;
● Compound Assignment Operator (aka Relational Operators)
○ Addition (+=) - It adds the value on the right-hand side to the variable on the left-hand side and assigns
the result to the left-hand side variable.
■ num1 += num2;
○ Subtraction (-=) - It subtracts the value on the right-hand side from the variable on the left-hand side
and assigns the result to the left-hand side variable.
■ num1 -= num2;
○ Multiplication (*=) - It multiplies the variable on the left-hand side by the value on the right-hand side
and assigns the result to the left-hand side variable.
■ num1 *= num2;
○ Division (/=) - It divides the variable on the left-hand side by the value on the right-hand side and
assigns the result to the left-hand side variable.
■ num1 /= num2;
○ Modulus (%=) - It computes the remainder when the variable on the left-hand side is divided by the
value on the right-hand side and assigns the result to the left-hand side variable.
■ num1 %= num2;
Bitwise Operators
● Count from right to left
● AND (&) - compares two bits and returns 1 if both bits are 1, otherwise returns 0.
○ res = 1010 & 0110;
○ res = 0010;
● OR (|) - compares two bits and returns 1 if at least one of the bits is 1, otherwise returns 0.
○ res = 1010 | 0110;
○ res = 1110;
● XOR (^) - compares two bits and returns 1 if the bits are different (one 0 and the other 1), otherwise returns 0.
○ res = 1010 ^ 0110;
○ res = 1100;
● Complement (~) - flips the bits, turning 0 to 1 and 1 to 0.
○ res = ~1010;
○ res = 0101;
● Left/Right Shift (<< or >>) - shifts the bits of a number to the left and right by a specified number of positions,
effectively multiplying the number by a power of 2
○ res1 = 1010 <<2;
○ res1 = 101000;
○ res2 = 1010 >>2;
○ res2 = 0010;
String Handling
● fgets - obtains string input from the user
○ fgets(variablename, sizeof(variablename), stdin);
○ To remove the newline:
■ size_t len = strlen(name);
■ if(len > 0 && name [len - 1] == '\n') {
■ name[len - 1] = '\0';
■ }
● strcat- string concatenation; combining two or more strings into a single string
○ strcat (string1,string2);

● 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.

You might also like