0% found this document useful (0 votes)
2 views13 pages

EC - 09 Computer Programming

The document provides an overview of C and C++ programming languages, covering their basic structures, data types, control structures, and operators. It also discusses memory management, functions, pointers, arrays, strings, and advanced concepts like structures, unions, and linked lists. Additionally, it introduces database management systems (DBMS) and their components.

Uploaded by

lkanths2002
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)
2 views13 pages

EC - 09 Computer Programming

The document provides an overview of C and C++ programming languages, covering their basic structures, data types, control structures, and operators. It also discusses memory management, functions, pointers, arrays, strings, and advanced concepts like structures, unions, and linked lists. Additionally, it introduces database management systems (DBMS) and their components.

Uploaded by

lkanths2002
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

____________________________________________________________________________________

Introduction to C

●​ C is a general-purpose, procedural programming language developed in 1972 by Dennis Ritchie


at Bell Laboratories.
●​ It is known for its efficiency, low-level memory access, and structured language design, which
makes it ideal for system programming, embedded systems, and hardware-level programming.

Basic Structure of a C Program


A C program follows a specific structure that consists of various components
#include <stdio.h> // Preprocessor Directive

int main() { // Main function, starting point of any C program


printf("Hello, World!"); // Output function
return 0; // Return statement
}

●​ Header Files: These contain function declarations and macro definitions (e.g., <stdio.h> for
standard input/output).
●​ Main Function: The entry point of any C program (int main()).
●​ Statements: Program instructions (e.g., printf() to display output).
●​ Return 0: Indicates successful program termination.

Data Types
C provides several built-in data types
●​ int: Integer (e.g., int a = 5;)
●​ float: Floating-point numbers (e.g., float b = 5.75;)
●​ char: Characters (e.g., char c = 'A';)
●​ double: Double-precision floating-point numbers (e.g., double d = 5.6789;)

Variables and Constants


●​ Variables: Storage locations with a name and data type (e.g., int marks = 15;).
●​ Constants: Fixed values that cannot be altered during execution (e.g., const int days_in_Month =
30;).

Operators
●​ Arithmetic Operators: +, -, *, /, %
●​ Relational Operators: ==, !=, >, <, >=, <=
●​ Logical Operators: &&, ||, !
●​ Assignment Operators: =, +=, -=, *=, /=

Control Structures
Control structures determine the flow of execution in a C program
if-else: Used for decision-making.​
if (condition) {
// code block if condition is true
} else {
// code block if condition is false}
____________________________________________________________________________________

Switch Statement: For selecting one of many blocks of code based on the value of a variable.​
switch (expression) {
case constant1:
// code block
break;
case constant2:
// code block
break;
default:
// default code block
}

Loops
Loops execute a block of code multiple times based on a condition:
for loop: Iterates a specific number of times.​
for (int i = 0; i < 5; i++) {
printf("%d", i);
}

while loop: Executes while a condition is true.​


while (condition) {
// code
}

do-while loop: Executes the code block at least once.​


do {
// code
} while (condition);
____________________________________________________________________________________

Fundamentals of C++
C++ is a powerful, general-purpose programming language that builds on C by adding object-oriented
features. It is widely used for system software, game development, embedded systems, and applications
requiring high performance.
Key Concepts of C++ Fundamentals
1.​ Basic Syntax
○​ Header Files: Essential for including libraries (e.g., <iostream> for input-output
operations).
○​ Main Function: Every C++ program begins execution from the main() function.
○​ Statements: C++ statements are executed sequentially and must end with a semicolon
(;).

2.​ Variables and Data Types


○​ Variables are used to store data, and each variable must have a data type.
○​ Common data types include:
■​ int: Integer values.
■​ float: Single-precision floating-point numbers.
■​ double: Double-precision floating-point numbers.
■​ char: Single characters.
■​ bool: Boolean (true/false) values.​

3.​ Input/Output (I/O)


○​ Use cin to take user input and cout to output data.
Example​
int age;
a)​ cout << "Enter your age: ";
b)​ cin >> age;
c)​ cout << "Your age is: " << age << endl;

4.​ Control Structures


○​ If-else statements are used for conditional execution.
○​ Loops (for, while, do-while) allow repetitive execution of a block of code.

5.​ Functions
○​ A function is a block of code designed to perform a specific task.
○​ Functions can return values or may not return any value (denoted by void).

6.​ Classes and Objects (OOP)


○​ C++ is an object-oriented programming language, which means you can create
classes that define properties and behaviors.
○​ A class is a blueprint for creating objects

Operators in C++
Operators in C++ allow you to perform different operations, from arithmetic to logical decisions.
Types of Operators
1.​ Arithmetic Operators
○​ These are used to perform basic arithmetic operations such as addition, subtraction,
multiplication, division, and modulus.
____________________________________________________________________________________

○​ Operators: +, -, *, /, %

2.​ Relational Operators


○​ Used to compare two values.
○​ Operators: == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than
or equal to), <= (less than or equal to)

3.​ Logical Operators


○​ Used to combine conditional statements.
○​ Operators: && (logical AND), || (logical OR), ! (logical NOT)

4.​ Assignment Operators


○​ Used to assign values to variables.
○​ Operators: =, +=, -=, *=, /=, %=

5.​ Increment and Decrement Operators


○​ Increment (++) increases a variable's value by 1, while decrement (--) decreases it by 1.

6.​ Bitwise Operators


○​ These operators perform operations on individual bits of numbers.
○​ Operators: & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), >> (right shift)

7.​ Ternary Operator


○​ A shorthand for an if-else statement.
○​ Syntax: condition ? expression_if_true : expression_if_false;
Example​
int age = 18;
string result = (age >= 18) ? "Adult" : "Minor";

8.​ Pointer Operators


○​ &: Returns the address of a variable.
○​ *: Dereferences a pointer, i.e., accesses the value stored at the pointer's address.

9.​ Size of Operator


○​ The sizeof() operator returns the size, in bytes, of a data type or variable.

File Handling
C provides functions to handle files for reading and writing:
●​ Opening a file: fopen()
●​ Reading from a file: fscanf(), fgets()
●​ Writing to a file: fprintf(), fputs()
●​ Closing a file: fclose()

Dynamic Memory Allocation


●​ malloc(): Allocates memory dynamically.
●​ free(): Deallocates the dynamically allocated memory.
int *ptr = (int*)malloc(sizeof(int) * 5); // Allocating memory for 5 integers
free(ptr); // Deallocating memory.
____________________________________________________________________________________

Functions:
function in C can perform a particular task, and supports the concept of modularity.
Syntax of Function:
return_data_type function_name (data_type variable1, data_type variable2, ...)
{
function body
}

Parameter passing :-
Call by value: sum (a, b); → value of a, b is passed.
Call by reference: sum (&a, &b); → address of a, b is passed.

Operator Precedence:

Associativity
() Parenthesis Left-to-right
[] Brackets (array subscript)
. Member selection via object
→ Member selection via pointer
++-- Post increment/decrement
++-- Pre increment/decrement Right-to-left
+- Unary plus/minus
!~ Logical negation/bitwise complement
(type) Type casting
* Dereference
& Address of
Size of Determines size
*/% Multiplication/division/module Left-to-right
+- Addition/subtraction
≪≫ Bitwise shift left, bitwise shift right
< <= Relational less than or equal to
> >= Relational greater than or equal to
==!= Relational is equal/is not equal Left-to-right
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise inclusive OR
&& Logical AND
|| Logical OR
?: Ternary conditional
= Assignment Right-to-left
+=-= Addition/subtraction assignment
*=/= Multiplication/division assignment
____________________________________________________________________________________

Pointers:
A variable that stores memory address.

Printf (“%d”, a*b) – error (no operator in between a and *b)


Printf (“%U”, *c) – 1000
Printf (“%d”, a**b) – 400
Note:

Note:
1.​ Scanf (“%d”, b) here ‘b’ gives the address of variable ‘a’.
2.​ NULL Pointers: Uninitialized pointers start out with random unknown values.
3.​ ‘&’ =Address of operator.
4.​ ‘*’ = indirection operator (returns value at given address).
5.​ &i returns the address of the variable i.
6.​ *(&i) return the value stored at a particular address.

Array:
Collection of same data type elements
int a [5] = {23, 25, 27, 29, 31};
0 1 2 3 4 ← index value
23 25 27 29 31
1000 1002 1004 1006 1008 ← Memory address

Note:
1)​ a is a constant pointer which contains the base address of an array.
2)​ Pointer arithmetic
a)​ a + 1 → pointer move to next block(1000 + 2 = 1002)
b)​ b + 1 → pointer move to second 1 –D array(2000 + 4 × 2 = 2008)
____________________________________________________________________________________

8
c)​ 1008 − 𝑃2 1000 𝑃1 = 2
= 4 (No. of element from P1 to P2)
3)​ array index expansion
a)​ a[i] = *(a + i) = i[a]
b)​ a[i][j] = *(*(a + i) + j)
4)​ a = a + 3 → error (a is constant pointer)
5)​ few points
a)​ a = 1000
b)​ &a = 1000
c)​ a + 1 = 1002
d)​ &a + 1 = 1010 → Skip the whole array.
e)​ b + 1 → skip ‘1’ 1 – D array
f)​ &b + 1 → skip the whole 2 – D array.

String:
String is stored as array of character along with “\O” at end.
char a [] = “Testbook”
1000 1001 1002 1003 - - - - 1008
T E s t b o o k \O
0 1 2 3 4 5 6 7 8

𝑃𝑟𝑖𝑛𝑡𝑓 (%𝑠, 𝑎) 𝑃𝑟𝑖𝑛𝑡𝑓 (%𝑠, 𝑇𝑒𝑠𝑡𝑏𝑜𝑜𝑘) 𝑃𝑟𝑖𝑛𝑡𝑓 (%𝑠, 1000) }𝑇𝑒𝑠𝑡𝑏𝑜𝑜𝑘


Printf (“%s”, a + 3) – tbook
Printf (“%c”, a [1]) – e
Printf (“%c”, *(a + 3)) – t
Printf (“%c”, a + 3) – error
Printf (“%s”, *(a + 3)) – error.

Structure and Union:


used to encapsulate different data in one object.
𝑠𝑡𝑟𝑐𝑡 𝑛𝑜𝑑𝑒 { 𝑖𝑛𝑡 𝑎; 𝑓𝑙𝑜𝑎𝑡 𝑏; 𝑐ℎ𝑎𝑟 𝑐; } ]𝑑𝑎𝑡𝑎 𝑡𝑦𝑝𝑒 𝑐𝑟𝑒𝑎𝑡𝑒𝑑
Struct node d = {10, 20.5, ‘X’}; → memory created
Struct node *e = &d
d.c = ‘x’
(*e).c = ‘x’
e → c = ‘x’

Note:
1.​ Union is the same to structure except in case of union memory of only one data type is created which
is maximum.
2.​ only one of all data types is used.
____________________________________________________________________________________

3.​ malloc → used to create memory at runtime with garbage value (malloc (size)).
4.​ Calloc → used to create memory at runtime with clear memory (calloc (size)).

Extra Points:
1. Post increment/decrement: Increment or decrement is done after the value evaluated in expression
E.g., {𝑖 = 5 𝑥 = 𝑖 ++ ⇒ 𝑖 = 6 𝑥 = 5
2. Pre increment/decrement: Increment or decrement is done before the value is evaluated in expression.
E.g., {𝑖 = 5 𝑥 =++ 𝑖 ⇒ 𝑖 = 6 𝑥 = 6
3. Format specifiers of C language.
a)​ %c – character
b)​ %d – signed integer
c)​ %f – float values
d)​ %s – string
e)​ %u – unsigned integer
f)​ %L – long
g)​ %if – double.
[Link] = (cast-type*) malloc (byte-size)
ptr = (int *) malloc (size of (int))
d)​ malloc create memory of given size & return its address
e)​ address is stored in pointer & cast type used to give certain data type whatever required.
f)​ If space is not available it returns a NULL pointer.
g)​ The default pointer return type is void.

Linked Lists
____________________________________________________________________________________

Stack(FIFO)

●​ Push (Adding an element):​


O(1)
●​ Pop (Removing an element):​
O(1)
●​ Top/Peek (Accessing the top element):​
O(1)

Queue (FIFO)

●​ Enqueue (Adding an element at the rear):​


O(1)
●​ Dequeue (Removing an element from the front):​
O(1)
●​ Front/Peek (Accessing the front element):​
O(1)
●​ Circular Queue Formulas:
○​ Front pointer update:​
Front=(Front+1)mod Queue Size
○​ Rear pointer update:​
Rear=(Rear+1)mod Queue Size
____________________________________________________________________________________

Tree

Sorting Algorithms:
●​ Bubble Sort:​
Time Complexity: O(n^2)
●​ Selection Sort:​
Time Complexity: O(n^2)
●​ Insertion Sort:​
Time Complexity: O(n^2)
●​ Merge Sort:​
Time Complexity: O(n log⁡n)
●​ Quick Sort:
○​ Best and Average Case: O(nlog⁡n)
○​ Worst Case:O(n^2)
Searching Algorithms:
●​ Linear Search:​
Time Complexity: O(n)
●​ Binary Search (for sorted arrays):​
Time Complexity: O(log⁡n)
____________________________________________________________________________________

Introduction to DBMS
●​ Definition: A Database Management System (DBMS) is a software that allows users to store,
modify, and retrieve data efficiently while ensuring security, consistency, and availability of the
data.
●​ Components:
○​ Database: Collection of related data.
○​ DBMS Software: Manages the database (e.g., MySQL, Oracle, SQL Server).
○​ SQL: Structured Query Language used to interact with the database.

2. Data Models
●​ Hierarchical Model: Data is organized into a tree-like structure.
●​ Network Model: Data is represented as a graph, allowing multiple parent-child relationships.
●​ Relational Model: Data is stored in tables (relations), which consist of rows (tuples) and columns
(attributes).
●​ Entity-Relationship (ER) Model: A diagrammatic way to represent entities, attributes, and
relationships.

3. Keys in DBMS
●​ Primary Key: A unique identifier for each record in a table. It cannot contain NULL values.
●​ Candidate Key: A minimal set of attributes that can uniquely identify a tuple.
●​ Foreign Key: An attribute in one table that references the primary key of another table.
●​ Composite Key: A key that consists of two or more attributes to uniquely identify a tuple.
●​ Super Key: A set of one or more attributes that can uniquely identify a record.

4. Relational Algebra Operations


●​ Selection (σ): Selects rows based on a specified condition

●​Union (∪): Combines rows from two relations.


●​Intersection (∩): Returns common rows between two relations.
●​Difference (-): Returns rows from one relation that are not present in another.
●​Cartesian Product (×): Combines every tuple of one relation with every tuple of another
relation.
●​ Join (⨝): Combines tuples from two relations based on a condition.
SQL (Structured Query Language)
●​ DDL (Data Definition Language):
○​ CREATE TABLE table_name (column_name datatype, ...);
○​ ALTER TABLE table_name ADD column_name datatype;
○​ DROP TABLE table_name;
●​ DML (Data Manipulation Language):
○​ SELECT column_name FROM table_name WHERE condition;
○​ INSERT INTO table_name (columns) VALUES (values);
○​ UPDATE table_name SET column_name = value WHERE condition;
○​ DELETE FROM table_name WHERE condition;
____________________________________________________________________________________

●​ DCL (Data Control Language):


○​ GRANT permission TO user;
○​ REVOKE permission FROM user;

ER Model (Entity-Relationship Model)


●​ Entity: An object that can be distinctly identified (e.g., Student, Teacher).
●​ Attributes: Characteristics or properties of an entity (e.g., Name, Age).
●​ Relationships: Describes how two entities are related (e.g., A student enrolls in a course).

E-R Components:
____________________________________________________________________________________

●​ Types of Relationships:
○​ One-to-One (1:1): Each entity in set A is related to at most one entity in set B.
○​ One-to-Many: One entity in set A is related to multiple entities in set B.
○​ Many-to-Many: Entities in set A can be related to multiple entities in set B and vice
versa.

●​ Atomicity: Ensures that the transaction is either fully completed or not at all.
●​ Consistency: Ensures the database remains in a consistent state before and after a transaction.
●​ Isolation: Ensures that transactions are executed in isolation from one another.
●​ Durability: Once a transaction is committed, it remains permanent, even in the event of a system
failure.

Indexes
●​ Definition: Indexes improve the speed of data retrieval operations.
●​ Types of Indexes:
○​ B-tree Index: Suitable for range queries and sorted data.
○​ Hash Index: Suitable for equality queries.
●​ Indexing Formula:​
Index Size=Number of Entries × Index Entry Size

You might also like