0% found this document useful (0 votes)
12 views15 pages

Act9 Sasi Raymart

The document provides an overview of programming, defining it as a collaboration between humans and computers through instructions. It covers key concepts such as programming languages, the distinction between software and hardware, and the structure of C programming, including its compilation process and data types. Additionally, it discusses computational thinking, algorithms, flowcharts, and basic input/output functions in C.

Uploaded by

Ray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views15 pages

Act9 Sasi Raymart

The document provides an overview of programming, defining it as a collaboration between humans and computers through instructions. It covers key concepts such as programming languages, the distinction between software and hardware, and the structure of C programming, including its compilation process and data types. Additionally, it discusses computational thinking, algorithms, flowcharts, and basic input/output functions in C.

Uploaded by

Ray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Raymart Sasi

BS AEE – 1 C

PROGRAMMING

What is Programming?

According to Codecademy (2023), it is a collaboration between humans and computers


in which humans provide a set of instructions for a computer to execute. It represents a mental
process of thinking up instructions to give a machine, like a computer. For example, in real-life
instances, it is like you are cooking a sinigang; the human is the computer, while the recipe’s
author is a programmer. In that manner, the recipe author provides you with a set of instructions
that you read and then follow.

PROGRAMMING LANGUAGE OVERVIEW

Programming Language is a set of instructions and syntax used to create software programs.
Some of the key features of programming languages include:

1. SYNTAX: the specific rules and structure used to write code in a programming
language.

Ex. How you write a basic "Hello, world!" program.


2. DATA TYPES: the type of values that can be stored in a program, such as numbers,
strings, and Booleans.
3. VARIABLES: named memory locations that can store values.
4. OPERATORS: symbols used to perform operations on values, such as addition,
subtraction, and comparison.
5. CONTROL STRUCTURES: statements used to control the flow of a program, such as if-
else statements, loops, and function calls.
6. LIBRARIES AND FRAMEWORKS: collections of pre-written code that can be used to
perform common tasks and speed up development.
7. PARADIGMS: the programming style or philosophy used in the language, such as
procedural, object-oriented, or functional.
SOFTWARE VS. HARDWARE

A computer system consists of two main parts: Hardware and Software

HARDWARE: refers to the physical components of a computer, such as the CPU, RAM,
storage, memory, and peripherals, that work together to support software and execute tasks.

SOFTWARE: a collection of programs and data that direct a computer’s operations, enabling
hardware functionality and user interactions such as Google Chrome, Microsoft Teams, and
other applications. This is also a set of instructions that tells a computer exactly what to do.

C LANGUAGE OVERVIEW

C is a general−purpose, high−level language that was originally developed by Dennis M.


Ritchie to develop the UNIX operating system at Bell Labs. It was originally first implemented
on the DEC PDP-11 computer in 1972.

In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of
C, now known as the K&R standard.

COMPUTATIONAL THINKING & LOGIC BUILDING

PROBLEM DECOMPOSITION: it is the process of decomposing a problem/program into


multiple subproblems/subprograms. It is also the basic building block of Parallel
Computing. This approach is fundamental to good programming practice, as it makes complex
tasks easier to understand, develop, and maintain. In the C programming language, problem
decomposition involves breaking down a large, complex problem into smaller, more
manageable sub-problems.

PATTERN RECOGNITION: it is a critical tool in computational thinking because it helps to


simplify problems and improve comprehension of intricacies. This uses the identification of
similarities within a particular data set or sequence to simplify the understanding and resolution
of a problem or goal. Pattern Recognition in C involves identifying recurring arrangements or
features within data. It is a fundamental concept in computer science, enabling programs to
classify, analyze, and interpret information based on these identified patterns.
ALGORITHMS IN DAILY LIFE

An algorithm is “any limited sequence of computer instructions or steps to solve a specified


problem logically to perform a computer task successfully.”

FLOWCHART: a graphical presentation of the specific process or algorithm. Instead, to solve


any everyday routine problem consciously, the structures or steps to solve the problem are
“conditional”. If A, then B. If B, then C. Therefore, in turn, only in this way can a complete
algorithm be developed to solve a specific everyday routine problem.

SYMBOL: Flowcharts use standardized symbols to represent different operations. Some of the
most common symbols include oval, diamond, rectangle, parallelogram, arrow, and circle.

RULES: There are some general rules to follow when drawing flowcharts to ensure they are
clear and easy to understand

1. Start and End: Every flowchart should have a clear start and at least one end point,
represented by the terminal symbol.
2. Direction of Flow: The flow of the algorithm should generally be from top to bottom or
left to right. Arrows should clearly indicate the direction.
3. Clarity: Use clear and concise descriptions within the symbols. Avoid ambiguity.
4. Single Entry, Single Exit (for processes and decisions): Generally, each process and
decision symbol should have one entry point and one exit point (except for the decision
symbol, which has multiple exit paths).
5. Avoid Crossing Lines: Try to arrange the flowchart in a way that minimizes or eliminates
the crossing of flow lines. If crossing is unavoidable, use connectors to maintain clarity.
6. Keep it Simple: For complex algorithms, it might be helpful to break them down into
smaller, more manageable flowcharts.

DRAWING TECHNIQUES:

1. Sequential Flow: Steps are executed in a linear order, one after the other. This is
represented by a straight line of process symbols connected by arrows.
2. Decision Making: The decision symbol is used to represent points where the flow of the
algorithm depends on a condition. There will be multiple paths (usually two,
representing "yes" or "no") leading out of the decision symbol.
3. Looping (Iteration): To repeat a set of steps, flowcharts use loops. This can be
represented by directing the flow back to an earlier point in the flowchart based on a
condition.
4. Connectors: When a flowchart becomes large or needs to be split across pages,
connectors (circles with labels) are used to link different parts. An "on-page connector"
links parts within the same page, while an "off-page connector" links parts on different
pages.
5. Subroutines (or Functions): For more complex algorithms, a process might represent a
call to another, more detailed flowchart (a subroutine or function). This helps in
modularizing the algorithm.

CREATING FLOWCHARTS FOR SIMPLE PROBLEMS

PSEUDOCODE: a term which is often used in programming and algorithm-based fields. It is a


methodology that allows the programmer to represent the implementation of an algorithm. It
has no syntax like any of the programming languages and thus can’t be compiled or interpreted
by the computer.

SIX MAIN CONSTRUCTS OF PSEUDOCODE

1. SEQUENCE: represents linear tasks sequentially performed one after the other.
2. WHILE: a loop with a condition at its beginning.
3. REPEAT-UNTIL: a loop with a condition at the bottom.
4. FOR: another way of looping.
5. IF-THEN-ELSE: a conditional statement that changes the flow of the algorithm.
6. CASE: the generalization form of IF-THEN-ELSE.

NOTE: When writing pseudocode, everyone has their own style of presenting since humans
are reading it and not a computer; pseudocode’s rules are less rigorous than those of a
programming language. However, there are some simple rules that help make pseudocode
more universally understood.

1. Always capitalize the initial word (often one of the six main constructs).
2. Make only one statement per line.
3. Indent to show hierarchy, improve readability, and show nested constructs.
4. Always end multi-line sections using any of the END keywords (ENDIF, ENDWHILE, etc.).
5. Keep your statements programming language independent.
6. Use the naming domain of the problem, not that of the implementation. For instance:
“Append the last name to the first name” instead of “name = first+last.”
7. Keep it simple, concise, and readable.
CONVERTING FLOWCHARTS TO PSEUDOCODE

NOTE: Converting a flowchart to pseudocode involves translating the visual representation of


an algorithm into a plain English-like description.

a. Understand Flowchart Elements:


▪ Start/End: Represented by ovals, signify the beginning and end of the algorithm.
▪ Process: Rectangles denote actions or calculations.
▪ Decision: Diamonds indicate conditional statements.
▪ Connector: Arrows show the flow of execution.
▪ Input/Output: Parallelograms represent input from the user or output to the user.
b. Translate Each Element:
▪ Start/End:
o Start: BEGIN or START
o End: END or STOP
▪ Process:
o Translate the action within the rectangle into a simple statement.
o Examples:
❖ "Calculate the sum of a and b" becomes sum = a + b
❖ "Read the value of x" becomes INPUT x
▪ Decision:
o Translate the condition within the diamond into an IF statement.
o Examples:
❖ "Is x greater than 10?" becomes IF x > 10 THEN
▪ Connector:
o Indicate the flow of execution using indentation or other visual cues.
o If there are multiple paths from a decision, use ELSE or ELSE IF
▪ Input/Output:
o Translate the input/output actions into INPUT or OUTPUT statements.
c. Example:

Flowchart:

1. Start
2. Read the value of x
3. If x > 10
4. Output "x is greater than 10"
5. Else
6. Output "x is not greater than 10"
7. End

STRUCTURE OF THE C PROGRAM

The basic structure of a C program is divided into 6 parts, which makes it easy to read, modify,
document, and understand in a particular format. A C program must follow the following
outline in order to successfully compile and execute. Debugging is easier in a well-structured
C program.

Sections of the C Program

1. DOCUMENTATION: this section consists of the description of the program, the name of
the program, and the creation date and time of the program. It is specified at the start
of the program in the form of comments.

EX. // description, name of the program, programmer name, date, time etc.

2. PREPROCESSOR SECTION: All the header files of the program will be declared in the
preprocessor section of the program. Header files help us to access others’ improved
code in our code. A copy of these multiple files is inserted into our program before the
process of compilation.

EX. #include<stdio.h>

#include<math.h>

3. DEFINITION: Preprocessors are the programs that process our source code before the
process of compilation. There are multiple steps which are involved in the writing and
execution of the program. Preprocessor directives start with the ‘#’ symbol. The #define
preprocessor is used to create a constant throughout the program. Whenever this name
is encountered by the compiler, it is replaced by the actual piece of defined code.

EX. #define long long ll

4. GLOBAL DECLARATION: The global declaration section contains global variables,


function declarations, and static variables. Variables and functions that are declared in
this scope can be used anywhere in the program.

EX. int num = 18;


5. MAIN() FUNCTION: Every C program must have a main function. The main() function
of the program is written in this section. Operations like declaration and execution are
performed inside the curly braces of the main program. The return type of the main()
function can be int as well as void. void() main tells the compiler that the program will
not return any value. The int main() tells the compiler that the program will return an
integer value.

EX. void main()

6. SUB PROGRAMS: User-defined functions are called in this section of the program. The
control of the program is shifted to the called function whenever they are called from
the main or outside the main() function. These are specified as per the requirements of
the programmer.

EX. int sum(int x, int y)

return x+y;

WRITING YOUR FIRST PROGRAM (HELLO, WORLD!)

“HELLO WORLD” PROGRAM: a first step towards learning any programming language, and is
also one of the most straightforward programs you will learn. It is the basic program that
demonstrates the working of the coding process. All you have to do is display the message
“Hello World” on the output screen.
COMPILATION AND EXECUTION PROCESS

COMPILATION: the process of converting the source code of the C language into machine
code. As C is a mid-level language, it needs a compiler to convert it into an executable code
so that the program can be run on our machine.

The C program goes through the following phases during compilation:

1. Source File
2. Preprocessor
3. Compiler
4. Assembler
5. Linker

What goes inside the compilation process?

A compiler converts a C program into an executable. There are four phases for a C program
to become an executable:

1. PRE-PROCESSING: the first phase through which source code is passed. This phase
includes the Removal of Comments, Expansion of Macros, Expansion of the included
files, and Conditional compilation.
2. COMPILATION: the next step is to compile filename.i and produce an intermediate
compiled output file filename.s. This file is in assembly-level instructions.
3. ASSEMBLY: In this phase, the filename.s is taken as input and turned into filename.o by
the assembler. This file contains machine-level instructions. At this phase, only existing
code is converted into machine language, and the function calls like printf() are not
resolved.
4. LINKING: This is the final phase in which all the linking of function calls with their
definitions is done. Linker knows where all these functions are implemented. Linker does
some extra work also: it adds some extra code to our program, which is required when
the program starts and ends. For example, there is a code that is required for setting
up the environment, like passing command line arguments. This task can be easily
verified by using the size filename.o and size filename. Through these commands, we
know how the output file increases from an object file to an executable file. This is
because of the extra code that the Linker adds to our program.
a. STATIC LINKING: All the code is copied to a single file, and then an executable
file is created.
b. DYNAMIC LINKING: Only the names of the shared libraries are added to the
code, and then it is referred to during the execution.

C DATA TYPES

INT (INTEGER): to store whole numbers without decimals (e.g., -10, 0, 100).

FLOAT (FLOATING POINT): store numbers with decimal points (e.g., 3.14, -2.71).

CHAR (CHARACTER): to store single characters, such as letters, numbers, symbols, or spaces
(e.g., 'A', '5', '*').

DOUBLE (DOUBLE PRECISION FLOATING POINT): similar to float, but provides more precision
for decimal numbers.

BOOL (BOOLEAN): to represent Boolean values (true or false).

DECLARING AND USING VARIABLES

VARIABLE DECLARATION: a fundamental concept in programming, where programmers define


variables to store data within a program. It is a statement that provides the variable name and
its type, allowing the program to allocate memory for storing values.

FOR C++

FOR C
CONSTANTS AND BASIC I/O (SCANF, PRINTF)

In C programming, printf() is one of the main output functions. The function sends formatted
output to the screen. There are many functions used for input and output in different situations,
but the most commonly used functions for Input/Output are scanf() and printf(), respectively.

Basic Output in C

The printf() function is used to print formatted output to the standard output stdout (which is
generally the console screen). It is one of the most commonly used functions in C.

The scanf() function is used to read user input from the console. It takes the format string and
the addresses of the variables where the input will be stored.

C Programming Operators

OPERATOR: a symbol that operates on a value or a variable.

EX. + is an operator to perform addition.

TYPES OF PROGRAMMING OPERATORS

C ARITHMETIC OPERATOR: arithmetic operator performs mathematical operations such as


addition, subtraction, multiplication, division etc, on numerical values (constants and variables).

C INCREMENT AND DECREMENT OPERATORS: C programming has two operators, increment


++ and decrement --, to change the value of an operand (constant or variable) by 1. Increment
++ increases the value by 1, whereas decrement -- decreases the value by 1. These two
operators are unary operators, meaning they only operate on a single operand.

C ASSIGNMENT OPERATORS: used for assigning a value to a variable. The most common
assignment operator is = .

C RELATIONAL OPERATORS: checks the relationship between two operands. If the relation is
true, it returns 1; if the relation is false, it returns value 0. Relational operators are used in
decision making and loops.

C LOGICAL OPERATORS: an expression containing a logical operator returns either 0 or 1


depending upon whether the expression results in true or false. Logical operators are
commonly used in decision-making in C programming.
C BITWISE OPERATORS: During computation, mathematical operations like: addition,
subtraction, multiplication, division, etc are converted to bit-level which makes processing faster
and saves power. Bitwise operators are used in C programming to perform bit-level operations.

PRECEDENCE AND ASSOCIATIVITY

Operator precedence and associativity are rules that decide the order in which parts of an
expression are calculated.

OPERATOR PRECEDENCE: determines which operation is performed first in an expression with


more than one operator with different precedence.

OPERATOR ASSOCIATIVITY: used when two operators of the same precedence appear in an
expression. Associativity can be either from Left to Right or Right to Left.

DECISION MAKING IN C

In C, programs can choose which part of the code to execute based on some condition. This
ability is called decision making, and the statements used for it are called conditional
statements. These statements evaluate one or more conditions and make the decision whether
to execute a block of code or not.

Types of Conditional Statements in C

1. IF STATEMENT: a simplest decision-making statement. It is used to decide whether a


certain statement or block of statements will be executed or not i.e, if a certain condition
is true, then a block of statements is executed otherwise, not.
2. IF-ELSE STATEMENT: the if statement alone tells us that if a condition is true, it will
execute a block of statements, and if the condition is false, it won’t. But what if we want
to do something else when the condition is false? Here comes the C else statement. We
can use the else statement with the if statement to execute a block of code when the
condition is false. The if-else statement consists of two blocks, one for the false
expression and one for the true expression.
3. NESTED IF-ELSE STATEMENT: an if statement that is the target of another if statement.
It means an if statement inside another if statement. Yes, C allows us to nested if
statements within if statements, i.e, we can place an if statement inside another if
statement.
4. IF-ELSE-IF LADDER STATEMENT: used when the user has to decide among multiple
options. The C if statements are executed from the top down. As soon as one of the
conditions controlling the if is true, the statement associated with that if is executed, and
the rest of the C else-if ladder is bypassed. If none of the conditions are true, then the
final else statement will be executed. if-else-if ladder is similar to the switch statement.

SWITCH CASE

SWITCH STATEMENT: a control flow structure that allows you to execute one of many code
blocks based on the value of an expression. It is often used in place of the if-else ladder when
there are multiple conditional codes.

LOOPS IN C

In programming, loops are used to repeat a block of code until a specified condition is met.

WHILE LOOP: runs as long as a condition is true. It is used when you don’t know in advance
how many times you want to execute the block of code. It continues to execute as long as the
specified condition is true.

DO-WHILE LOOP: runs at least once and then continues if a condition is true. It is similar to the
while loop, but with one key difference: it guarantees that the block of code will execute at least
once before checking the condition.

LOOP CONTROL

BREAK STATEMENT: used to "jump out" of a switch statement. It can also be used to jump out
of a loop.

CONTINUE STATEMENT: it breaks one iteration (in the loop) if a specified condition occurs, and
continues with the next iteration in the loop.

FUNCTION IN C

A function in C is a set of statements that, when called, perform some specific tasks. It is the
basic building block of a C program that provides modularity and code reusability. The
programming statements of a function are enclosed within { } braces, having certain meanings
and performing certain operations. They are also called subroutines or procedures in other
languages.
FUNCTION DECLARATION: it must provide the function name, its return type, and the number
and type of its parameters. It also tells the compiler that there is a function with the given name
defined somewhere else in the program.

FUNCTION DEFINITION: consists of actual statements which are executed when the function is
called (i.e. when the program control comes to the function).

FUNCTION CALL: a statement that instructs the compiler to execute the function. We use the
function name and parameters in the function call.

C FUNCTION PARAMETERS

PARAMETER: specified after the function name, inside the parentheses. It can add as many
parameters as you want, just separate them with a comma.

RETURN VALUE: the output or result that a function produces and sends back to the code that
called the function.

SCOPE OF VARIABLES

SCOPE OF VARIABLES: it defines the region of the code where a particular variable can be
referenced or used. Scope holds the current state of variables along with their respective values.
Scope of a variable is depends on where the value is declared in the program. Once, a variable
is declared, we can access and manipulate its data according to our needs. Understanding how
the variable's value is accessed and used within its scope is essential to find out the program's
behavior.

Variables in C can have two types of scopes:

• Local Scope or Block Scope: The variables declared within the local scope are called
local variables. Local variables are visible in the block they are declared in and other
blocks nested inside that block.

• Global Scope or File Scope: The variables declared in the global scope are called global
variables. Global variables are visible in every part of the program.

INTRODUCTION TO 1D ARRAYS

In C, an array is a collection of elements of the same type stored in contiguous memory


locations. This organization allows efficient access to elements using their index. Arrays can
also be of different types depending upon the direction/dimension they can store the elements.
It can be 1D, 2D, 3D, and more. We generally use only one-dimensional, two-dimensional, and
three-dimensional arrays.

ONE-DIMENSIONAL ARRAYS IN C: can be viewed as a linear sequence of elements. We can


only increase or decrease its size in a single direction.

ACCESSING AND MODIFYING ARRAY ELEMENTS

ARRAY: a fixed-size collection of similar data items stored in contiguous memory locations. It
can be used to store the collection of primitive data types such as int, char, float, etc., and also
derived and user-defined data types such as pointers, structures, etc.

ARRAY DECLARATION: It can declare an array by specifying its name, the type of its elements,
and the size of its dimensions. When we declare an array in C, the compiler allocates the
memory block of the specified size to the array name.

ARRAY INITIALIZATION: the process of assigning an initial value to the variable. When the array
is declared or allocated memory, the elements of the array contain some garbage value. So,
we need to initialize the array to some meaningful values by using an initializer list is a list of
values enclosed within braces { } separated by a comma.

ACCESSING: can access any element of the array by providing the position of the element,
called the index. Indexes start from 0. We pass the index inside square brackets [] with the
name of the array.

ARRAY OF STRINGS IN C

In C, an array of strings is a 2D array where each row contains a sequence of characters


terminated by a ‘\0’ NULL character (strings). It is used to store multiple strings in a single array.

Syntax of Array of Strings

• arr_name: Name of the variable.

• r: Maximum number of strings to be stored in the array

• m: Maximum number of character values that can be stored in each string.

• s1, s2, …., sn: Strings to be stored.

You might also like