0% found this document useful (0 votes)
9 views12 pages

Unit1 Part2 Notes

The document provides a comprehensive overview of algorithms, pseudo code, comments in C programming, and data types in C. It defines algorithms, outlines their development steps, and emphasizes properties like finiteness and definiteness. Additionally, it explains the purpose and types of comments in C, as well as the classification and characteristics of various data types.

Uploaded by

rajputlakshya101
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)
9 views12 pages

Unit1 Part2 Notes

The document provides a comprehensive overview of algorithms, pseudo code, comments in C programming, and data types in C. It defines algorithms, outlines their development steps, and emphasizes properties like finiteness and definiteness. Additionally, it explains the purpose and types of comments in C, as well as the classification and characteristics of various data types.

Uploaded by

rajputlakshya101
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

9.

ALGORITHMS
Definition
Algorithm: A set of computational steps that transform input into output. It is a set of
sequential steps, usually written in ordinary language, to solve a given problem.

Formal Definition: An algorithm can be defined as a complete, unambiguous, finite


number of logical steps for solving a specific problem.

Steps Involved in Algorithm Development


Step 1: Identification of Input - Input refers to quantities to be supplied to the algorithm
- These are fed externally - The input must be identified first for any specified problem -
Example: If finding average of numbers, input = numbers

Step 2: Identification of Output - From an algorithm, at least one quantity is produced


called output - Output must be clearly identified for the problem - Example: If finding
average, output = average value
Step 3: Identification of Processing Operations - All calculations to be performed to
lead from input to output - Must be identified in an orderly manner - Example: Sum all
numbers, divide by count

Step 4: Processing Definiteness - Instructions composing the algorithm must be clear -


There should NOT be any ambiguity - Each step should have only one meaning - Example:
“Add 2 to x” is clear; “Do something with x” is ambiguous

Step 5: Processing Finiteness - Algorithm should terminate after a finite number of steps
for ALL cases - Should not run infinitely - Must have a clear stopping point - Example:
Loop must have a termination condition
Step 6: Possessing Effectiveness - Instructions in the algorithm must be sufficiently
basic - Can be easily carried out in practice - Can be performed exactly in a finite amount
of time - Can be converted easily into program statements

Properties of an Algorithm (CRITICAL FOR EXAMS)


An algorithm must possess the following properties:

1. Finiteness - Algorithm must terminate in a finite number of steps - For any valid input,
algorithm will eventually stop - No infinite loops or infinite recursion

2. Definiteness - Each step of the algorithm must be precisely and unambiguously stated -
Every step must have exactly one interpretation - Clear, unambiguous instructions -
Example: Bad - “Do something”; Good - “Add x to y”

3. Effectiveness - Each step must be effective (can be performed) - Instructions should


be primitive (basic operations) - Can be easily convertible into program statements - Can be
performed exactly in finite time - Not theoretical or vague operations

4. Generality - Algorithm must be complete in itself - Can be used to solve problems of a


specific type for ANY input data - Should work for all valid inputs - Not limited to specific
test cases

5. Input/Output - Each algorithm must take zero, one, or more quantities as input data -
Must produce one or more output values - Clear definition of what goes in and what comes
out

Algorithm Representation
English-like Sentences (Pseudo Code): - Written in simple English sentences - Not
following strict programming syntax - Easy to understand and convert to actual code -
Most common for teaching algorithms
Example:
Read the numbers
Add them together
Divide by count
Print result

Algorithm Examples (IMPORTANT FOR EXAMS)

Example 1: Find Average of Three Numbers

Step 1: Read the numbers a, b, c


Step 2: Compute the sum of a, b, and c
Step 3: Divide the sum by 3
Step 4: Store the result in variable d
Step 5: Print the value of d
Step 6: End of the program

Analysis: - Input: Three numbers (a, b, c) - Processing: Sum them, divide by 3 - Output:
Average (d) - Properties: Finite, definite, effective, works for any three numbers

Example 2: Calculate Simple Interest

Formula: Simple Interest = (P × N × R) / 100


Where: - P = Principal Amount - N = Number of years - R = Rate of interest
Step 1: Read the three input quantities P, N, and R
Step 2: Calculate simple interest as SI = (P × N × R) / 100
Step 3: Print simple interest
Step 4: Stop

Analysis: - Input: Principal (P), Years (N), Rate (R) - Processing: Multiply and divide as
per formula - Output: Simple Interest value - Works for any values of P, N, R

Example 3: Find Area of Triangle

Formula: Area = (1/2) × b × c × sin(A)


Where: - b, c = Two sides of triangle - A = Included angle between sides
Step 1: Input the given elements of the triangle (sides b, c and angle A)
Step 2: Calculate Area = (1/2) × b × c × sin(A)
Step 3: Output the Area
Step 4: Stop

Analysis: - Input: Two sides and included angle - Processing: Apply area formula - Output:
Area of triangle - Definite and finite

Example 4: Find Largest of Three Numbers (X, Y, Z)

Step 1: Read the numbers X, Y, Z


Step 2: If X > Y then BIG = X else BIG = Y
Step 3: If BIG < Z then BIG = Z
Step 4: Print the largest number (BIG)
Step 5: Stop

Analysis: - Input: Three numbers (X, Y, Z) - Processing: Compare and find maximum -
Output: Largest number - Handles all possible input combinations
10. PSEUDO CODE
Definition
Pseudo Code: Neither an algorithm nor a program. It is an abstract form of a program. It
consists of English-like statements which perform specific operations.

Characteristics: - Not a formal programming language - Used to represent algorithms -


Does NOT use graphical representation (like flowcharts) - Represented in terms of words
and phrases - Programming syntax is not strictly followed - More formal than plain
English, less formal than actual code

Purpose
Bridge between algorithm (English-like) and actual program code
Easier to convert to actual programming code
Allows focus on logic rather than syntax

Advantages of Pseudo Code


1. Easy to Read
Written in simple, understandable language
No complex syntax rules to follow
Anyone can understand the logic
2. Easy to Understand
Clear, step-by-step representation
Focuses on logic and flow
No programming language specific knowledge needed
3. Easy to Modify
Can change steps quickly without rewriting entire program
Flexible structure
Quick to test different approaches

Pseudo Code Example


Problem: Perform basic arithmetic operations on two numbers
Read n1, n2
Sum = n1 + n2
Difference = n1 - n2
Product = n1 * n2
Quotient = n1 / n2
Print Sum, Difference, Product, Quotient
End

Analysis: - Clear English-like statements - No strict programming syntax - Easy to convert


to any programming language - Logic is immediately understandable

11. COMMENTS IN C PROGRAMMING


Definition
Comment: A programmer-readable explanation or annotation in the source code of a
computer program.

Key Feature: Comments are statements that are NOT executed by the compiler or
interpreter. They are only for human readers.
Purpose of Comments
1. Make Code More Readable - Provides descriptions and explanations
2. Help with Algorithm Understanding - Explains algorithm logic in code
3. Self-Documentation - Helps you understand your own code later
4. Team Communication - Explains intentions to other programmers
5. Debugging Help - Makes finding errors easier

Types of Comments in C
There are 2 types of comments in C:

1. Single-Line Comment
Representation: // (double forward slash)
Use: Denotes a single line comment, applies to only one line
Syntax:
// This is a single line comment
statement;

Examples:
#include <stdio.h>

int main() {
int age = 25; // Declare age variable

printf("Welcome"); // Print welcome message

return 0; // Return 0 to indicate success


}

Output: Welcome
Characteristics: - Starts with // - Extends to end of line only - Anything after // on that
line is comment - Next line is treated as new statement

2. Multi-Line Comment
Representation: /* ... */ (forward slash asterisk to asterisk forward slash)
Use: Denotes multi-line comment, can apply to multiple lines
Syntax:
/* This is a multi-line comment
It can span multiple lines
Everything between /* and */ is comment */

Also Called: C-Style comment (introduced in C programming)


Examples:
#include <stdio.h>

int main() {
/* This is a multi-line comment
written to demonstrate comments in C
Multiple lines are allowed */

printf("Welcome to C");

return 0;
}

Output: Welcome to C
Characteristics: - Starts with /* - Ends with */ - Can span multiple lines - Can span
partial lines - Cannot be nested in most C compilers
Important: - Cannot nest multi-line comments: /* /* nested */ */ - WRONG - Can
have /* inside // comment: // This /* is okay */ - OK - Cannot have // inside multi-
line affecting it: /* // */ - Entire thing is comment

Comment at End of Code Line


Use: Comments can be placed at the end of a code line
Syntax:
int age = 25; // age of the person
int salary; // salary in rupees

Note: Generally it’s better practice to put the comment BEFORE the line of code for clarity
Example - Better Practice:
// Declare variable for age
int age = 25;

// Declare variable for salary


int salary;

When and Why to Use Comments


1. Making Code More Readable
Provides context and explanation
Helps understand variable purpose
Clarifies complex logic
2. Describing Algorithms
Explains algorithm steps in code
Makes code logic understandable
Helpful for code review
3. Self-Documentation
Helps you understand your own code later
Useful when code is reused after long gap
Reduces time to get back into code context
4. Complex Logic Explanation
When code is not self-explanatory
When using unusual techniques
When optimizing for performance
5. TODO and FIXME Notes
Mark areas for future work: // TODO: Optimize this loop
Mark known issues: // FIXME: Bug in calculation

Comment Best Practices


1. Keep comments updated - Outdated comments are worse than no comments
2. Avoid obvious comments - Don’t comment: x = x + 1; // Add 1 to x
3. Explain WHY, not WHAT - Code shows what, comments show why
4. Use clear language - Avoid vague or ambiguous descriptions
5. Appropriate detail - Balance between too much and too little info

12. DATA TYPES IN C


Definition
Data Type: A keyword/word which tells the compiler which type of data can be stored in
a particular variable.
Purpose: - Specifies what kind of values a variable can hold - Determines memory space
needed - Determines operations allowed on variable

Classification of Data Types


Data Types
├── PRIMARY (Built-in) Data Types
│ ├── Integer (int)
│ ├── Real/Floating-point (float, double)
│ └── Character (char)
├── SECONDARY (Derived) Data Types
│ ├── Array
│ └── Pointer
└── USER-DEFINED Data Types
├── Structure (struct)
├── Union (union)
└── Enumeration (enum)

PRIMARY DATA TYPES

1. INTEGER DATA TYPE


Purpose: Store whole numbers (no decimal point)
Keyword: int
Range: -2,147,483,648 to 2,147,483,647
Bytes Reserved: 4 bytes (on most modern platforms)
Format Specifier: %d (for printing)
Example:
int age = 25;
int score = 98;
int temperature = -5;

Integer Subtypes (Signed vs Unsigned, Short vs Long)

A. Signed Short Integer

Keyword: short int or short


Range: -32,768 to 32,767
Bytes Reserved: 2 bytes
Format Specifier: %hd
Example:
short int age = 25;
short marks = 98;

B. Signed Long Integer

Keyword: long int or long


Range: -2,147,483,648 to 2,147,483,647
Bytes Reserved: 4 bytes (on most platforms)
Format Specifier: %ld
Example:
long int population = 1000000000;
long salary = 500000;

C. Unsigned Short Integer

Keyword: unsigned short int or unsigned short


Range: 0 to 65,535 (only positive)
Bytes Reserved: 2 bytes
Format Specifier: %hu
Example:
unsigned short count = 50000;
unsigned short age = 25;

D. Unsigned Long Integer

Keyword: unsigned long int or unsigned long


Range: 0 to 4,294,967,295 (only positive)
Bytes Reserved: 4 bytes
Format Specifier: %lu
Example:
unsigned long population = 4000000000;
unsigned long filesize = 2000000;

Important Notes on Integer Types


Platform Differences (Bytes):
Platform short int long
16-bit Turbo C DOS 2 2 4
32-bit Windows 2 4 4
64-bit Windows 2 4 4
64-bit Linux GCC 2 4 8

Key Notes: 1. int is signed by default (can store negative and positive) 2. Signed: Can
store both negative and positive values, including zero 3. Unsigned: Can only store non-
negative values (positive integers and zero)

Format Specifiers: %d vs %i
%d (Decimal): - Prints a signed decimal integer - Always treats the number as decimal
when printing - Most commonly used for printing integers
%i (Integer): - Prints a signed integer - Can interpret octal if prefixed with 0 - Can
interpret hexadecimal if prefixed with 0x when reading input - More versatile for input
operations
Key Differences:

Specifier Usage Input Interpretation


%d Printing signed integers Always decimal
%i More versatile Can interpret base (octal, hex)

For Most Cases: %d and %i produce same output when printing. When inputting with
scanf, %i can interpret different bases, while %d treats all as decimal.

2. REAL/FLOATING-POINT DATA TYPES


Purpose: Store numbers with decimal points (fractional numbers)

A. Float Data Type

Keyword: float
Range: 3.4E-38 to 3.4E+38 (very small to very large)
Bytes Reserved: 4 bytes
Format Specifier: %f or %e
Precision: 6 decimal places (approximate)
Example:
float pi = 3.14159;
float temperature = 37.5;
float salary = 50000.75;

B. Double Data Type

Keyword: double
Range: 1.7E-308 to 1.7E+308 (much larger range)
Bytes Reserved: 8 bytes
Format Specifier: %lf
Precision: 15-17 decimal places (higher precision)
Example:
double pi = 3.14159265359;
double distance = 1500000.123456;

C. Long Double Data Type

Keyword: long double


Range: 3.4E-4932 to 1.1E+4932 (extended range)
Bytes Reserved: 10 bytes (on some platforms; varies)
Format Specifier: %Lf
Precision: 18-19 decimal places (highest precision)
Example:
long double largeNumber = 123456789.123456789;

Format Specifiers for Floating-Point: %f vs %e


%f (Fixed-Point Decimal): - Regular decimal notation - Shows fixed number of decimal
places - Good for normal numbers
%e (Scientific Notation/Exponential): - Compact representation - Uses powers of 10 -
Good for very large or very small numbers

When to Use:

Specifier Use Case Example Output


%f Standard decimal numbers 12345.678900
%e Very large/small numbers 1.234568e+04

Example:
#include <stdio.h>

int main() {
float num = 0.00012345;

// Scientific notation
printf("Scientific notation: %e\n", num);
// Output: Scientific notation: 1.234500e-04

// Fixed-point decimal
printf("Fixed-point notation: %f\n", num);
// Output: Fixed-point notation: 0.000123

return 0;
}

3. CHARACTER DATA TYPE


Purpose: Store single character values
Keyword: char
Bytes Reserved: 1 byte (8 bits)
Format Specifier: %c

A. Signed Character

Keyword: signed char


Range: -128 to 127
Example:
signed char letter = 'A';
signed char digit = '5';

B. Unsigned Character

Keyword: unsigned char


Range: 0 to 255
Example:
unsigned char letter = 'A';
unsigned char code = 200;

Important Note on Char Sign


Whether char is signed or unsigned by default is COMPILER-DEPENDENT:

Compiler Default
Dev-C MinGW/GCC Signed by default
VS Code GCC/Clang Signed by default
VS Code MSVC Signed by default (unless /J flag)
Turbo C 16-bit Signed by default
Embedded ARM compilers Unsigned by default

Best Practice: Explicitly specify signed char or unsigned char if sign matters

13. L-VALUE AND R-VALUE


Definition
In C programming, l-value and r-value refer to expressions and values in an assignment
operation.

1. L-Value (Left-Value)
Definition: An l-value refers to an expression that represents a memory location or an
address. It can be on the LEFT-HAND SIDE of an assignment operation, where a value
can be stored.

Simple Terms: L-value = Something that can be assigned a value


Characteristics: - Represents a memory location - Can appear on left side of assignment -
Has a definite address in memory - Can be modified

Examples:
int x; // x is an l-value (memory location)
x = 42; // Assigning value to memory location

int arr[5]; // arr[0] is an l-value


arr[0] = 10; // Assigning to array element

struct point {
int x, y;
} p;
p.x = 5; // p.x is an l-value

2. R-Value (Right-Value)
Definition: An r-value refers to an expression that represents a VALUE, often a literal or
the result of an evaluation. It can be on the RIGHT-HAND SIDE of an assignment
operation, providing the value to be assigned to an l-value.

Simple Terms: R-value = A value that can be assigned to an l-value


Characteristics: - Represents a VALUE, not a location - Can appear on right side of
assignment - Cannot be modified directly - Usually temporary
Examples:
int y = 10; // 10 is an r-value

int z = x + y; // (x + y) is r-value (result of addition)

int result = (5 * 3); // (5 * 3) is r-value (result of multiplication)

int val = foo(); // foo() is r-value (function return value)

Summary Table: L-Value vs R-Value

Aspect L-Value R-Value


Represents Memory location A value
Position Left side of = Right side of =
Can be assigned Yes No
Has address Yes (known) May not have
Can be modified Yes No (directly)
Examples x, arr[0], p.x 42, x+y, foo()

Practical Examples
#include <stdio.h>

int main() {
int a = 5; // 5 is r-value, a is l-value
int b = 10; // 10 is r-value, b is l-value
int c = a + b; // (a + b) is r-value, c is l-value

// WRONG: c + 5 = a; // ERROR! (c+5) is r-value, cannot assign to it

c = 20; // 20 is r-value, c is l-value

printf("a = %d, b = %d, c = %d\n", a, b, c);

return 0;
}
EXAM PREPARATION SUMMARY (Part 2)
Key Points to Remember
1. Algorithm: Finite, definite, effective steps to solve problem
2. Algorithm Properties: Finiteness, Definiteness, Effectiveness, Generality, I/O
3. Pseudo Code: English-like, not strict syntax, abstract form of program
4. Comments: Not executed, // for single-line, /* */ for multi-line
5. Data Types: Primary (int, float, char), Secondary (array, pointer), User-defined
(struct, union)
6. Integer Types: signed (negative/positive), unsigned (positive only); short (2 bytes),
long (4/8 bytes)
7. Float Types: float (4 bytes), double (8 bytes), long double (10 bytes)
8. Format Specifiers: %d (int), %f (float), %c (char), %lf (double)
9. L-Value: Memory location (can assign), R-Value: Value (cannot assign)

Quick Reference Table (Part 2)

Type Keyword Range Bytes Specifier


short int short -32,768 to 32,767 2 %hd
int int -2.1B to 2.1B 4 %d
long int long -2.1B to 2.1B 4 %ld
unsigned short unsigned short 0 to 65,535 2 %hu
unsigned long unsigned long 0 to 4.2B 4 %lu
float float 3.4E-38 to 3.4E+38 4 %f, %e
double double 1.7E-308 to 1.7E+308 8 %lf
long double long double 3.4E-4932 to 1.1E+4932 10 %Lf
char char -128 to 127 1 %c
unsigned char unsigned char 0 to 255 1 %c

END OF UNIT 1 PART 2 STUDY NOTES

You might also like