COMPROG - Arrays to File Handling User-defined functions - are programs created by the
Types of Array programmer. These programs are created for a special
One-Dimensional Array - A one-dimensional array is a purpose.
data structure that stores elements of the same data type Function Prototyping - A function must be declared before
in a linear arrangement. It's a they can be used in a program. Function prototyping is like
simple way to store and access data, and is used in many introducing the function to the program. It tells the compiler
programming applications. the number and type of arguments that are to be passed to
Format <data type> <variable name> [array size]; the function and the type of the value that is to be returned
Example: int x[5]; by the function.
Array elements can be accessed thru the array index. An
array index starts at index 0. TYPES OF USER-DEFINED FUNCTIONS
Two-Dimensional Array - is like a table of elements. Call by Value - When a Call by Value is initiated by a calling
Format : <data type> <variable name> [array size] [array function, actual value/s (argument/s) are passed to the
size]; called function. The called function accepts the actual
Example: int x[3][3]; // 3 x 3 array having 9 elements parameter/s via the formal parameter/s list and then
Three-Dimensional Array - is like a table within a table. processing is done within the function until a return
Format: <data type> <variable name> [array size][array statement is executed to mean that a value was returned to
size][array size]; the calling function.
Example: int x[2][3][3]; // 2 tables with 3 x 3 array each Call by reference - When a Call by Reference is initiated by
a calling function, the address/es of the actual parameter/s
SORTING are passed to the called function. The called function
Bubble Sort - is a sorting algorithm that starts from the first accepts the actual parameter/s via the formal parameter/s
element of an array and compares it list (i.e. should be declared as pointer variables) and then
with the second element. If the first element is greater than processing is done within the function until a return
the second, we swap them. It continues this process until statement is executed to mean that a value was returned to
the end of the array, with the largest elements “bubbling” to the calling function. When a call by reference is initiated,
the top. any changes made to the variables in the called function is
Shell Sort - in data structure is a way to sort a list of carried over to the calling function. The reason for this is
numbers by comparing elements that are far because the address of the actual parameter was passed
apart and then gradually reducing the gap between them. and so any changes made to the value in the address will be
It’s an improved version of balloon carried over to the calling function.
sort, which sorts numbers one by one but can be slow if the
numbers are far from their correct String Functions : #include<string.h>
positions. A. Copy functions
Balloon Sort - Elements "inflate" (move) toward their 1. strcpy(string1, string2) – copies the content of string2 to
correct position faster than Bubble Sort. Instead of string 1.
adjacent swaps, it might move larger elements directly 2. strncpy(target, source,count) – copies count characters
toward their final position (like a balloon rising quickly). from string source
into string target.
SEARCHING
Linear Search - is a sequential searching algorithm where B. Concatenate
we start from one end and check every element of the list 1. strcat(string 1, string2) – concatenates the content of
until the desired element is found. string1 and string2;assigns the concatenated string to
Binary Search - is an interval searching algorithm that string1 and leaves string2 untouched.
searches for an item in the sorted list. It works by repeatedly String1 = “Hello ”; = Hello
dividing the list into two equal parts and then searching for World String2 = “World”; =
the item in the part where it can possibly exist. World
Function is a program that will return a value unless strcat(String1,String2);
declared as void. A C program may consist of one or more 2. strncat(string1, string2, count) – concatenates no more
functions; one of which is the function main(). Once a than count characters of string2 to string1 and assigns the
program ( with multiple functions) is executed, control first concatenated string to
checks the main() function and then branches out to the string1. strncat(String1, String2, 3);
other functions specified in the program. String1 = Hello Wor
String2 = World
Types of Function
Pre-defined functions - are programs imbedded in the C C. Compares
Programming Language. These are ready to use programs. 1. strcmp(string1, string2) – compares string1 and string2
and returns an integer
based on the following: 5. fmod() – returns the remainder of x/y Syntax: double
Value Meaning fmod(double x, double y);
< 0 string1 is less than string2 6. pow() – returns base raised to the exp power(base, exp).
0 string 1 is equal to string2 Syntax: double pow(double base, double exp)
>0 string1 is greater than string2 7. pow10() – returns 10 raised to the power
2.stricmp(string1, string2) – compares two strings while of n. Syntax: double pow10(int n)
ignoring cases. 8. sqrt() – returns the square root of
3.strncmp(string1, string2, count) or strnicmp(string1, num. Syntax: double sqrt(double
string2, count) or strncmpi(string1, string2, count) – num);
compares no more than count characters for the two
strings, strncmpi() and strnicmp() ignore cases Declaring a POINTER
A pointer is declared by specifying its name and type, just
D. Cases like simple variable declaration but with an asterisk
1. strlwr(string) – converts the string to lowercase. (*) symbol added before the pointer’s name.
2. strupr(string) – converts the string to uppercase.
E. Set Format: <data type>*<pointer variable name>;
1. strnset(string,c,count) – sets the first count character of Example: int *p;
the string to the value of c int *ptr;
2. strset(string,ch) –sets all characters in the string to the // a pointer variable is declared by an asterisk before the
value of ch pointer variable name
F. Others Linked List of Structures
1. strchr(string,c) – returns a substring of string beginning at
the first What is Linked list?
occurrence of character c up to the end of the string.
2. strlen(string) – returns the length of the string. *** null is A linked list is a dynamic data structure used to store a
not counted sequence of elements. Unlike arrays, linked lists do not
3. strrev(string) – reverses all characters except the null store elements in contiguous memory locations. Instead,
terminator in the string. each element, called a node, contains the data and a
4. strdup(string) – holds a duplicate of the string pointed to pointer (reference) to the next node in the sequence.
by string.
Arrange the name of the Students according to their names.
Character Functions: #include< ctype.h> Anna Dianne Kevin Lucas Cynthia Claive
1. isalnum(int ch) – returns a non-zero if it’s argument is
either a letter of the alphabet or a digit.
2. isalpha(int ch) – returns a non-zero if ch is a letter of the
alphabet. Otherwise, zero is returned.
3. isdigit(int ch) – return non-zero if ch is a digit from 0-9.
Otherwise, zero is returned.
4. islower(int ch) – returns non-zero id ch is a lowercase
letter (a-z). Otherwise, zero is returned.
5. ispunct(int ch) – returns a non-zero if ch is a punctuation
character excluding the space. Otherwise, zero is returned.
6. isspace(int ch) – returns non-zero if ch is one of the
following: a space, tab,
carriage return. Otherwise, zero is returned.
Mathematical Functions: #include<math.h> Types of Linked List
1. abs() – returns the absolute value of the integer
number. Syntax: int abs(int num); 1. Single Linked List: Navigation is Forward Only
2. ceil() – returns the smallest integer represented as 2. Doubly Linked List: Forward and Backward Navigation
double not less than num. Syntax: double ceil(double num); is Possible
3. fabs() – returns the float absolute value of 3. Circular Linked List: Last element is linked to the first
num. Syntax: double fabs(double num); element
4. floor() – returns the largest integer represented as double
not greater
than num. Syntax: double floor(double num);
What is Singly Linked List
A singly linked list is a list made up of nodes that
consists of two parts
ARRAY VS. LINKED LIST
ARRAY IS THE SEQUENTIAL REPRESENTATION OF LIST
Creating a NODE
Self Referential structures are those structures that
have one or more pointers which point to the same type
of structure, as their member.
Method 2
stud_rec[0] → Juan
stud_rec[1] → Maria
stud_rec[2] → Pedro
Referencing structure elements means accessing or
changing the values inside a structure.
Using input functions like gets() and scanf())
You use this to get values from the user.
Tip: gets() is old and not recommended anymore — better
to use fgets() to avoid bugs.
Structure -is a collection of related heterogenous fields.
This means that fields with different data types can be
grouped together to form a structure. It is also known as a
Record.
What is an Array of Structures?
Imagine you're building a system to store info about
Declaring a structure multiple students.
Format: Each student has a name and an age.
struct <record name in the structure definition> <record char name1[20], name2[20], name3[20];
variable name>; int age1, age2, age3;
struct record stud_rec, stud_rec1, stud_rec2; That works… but what if you have 100 students? It becomes
a mess.
Initializing a structure variable How it works?
A structure variable can be initialized upon structure 1. Structure = One Record (One Student)
declaration.
Initializing a structure variable means giving it values right struct record {
away when you create it. char name[20];
You created a structure variable called stud_rec. int age;
};
You gave it:
2. Array of Structure = Many Records (Many Students)
name = "Juan"
age = 21 struct record stud_rec[3];
You now have 3 forms:
Initializing a structure variable
stud_rec[0] for student 1
You created an array (a list) of 3 students. stud_rec[1] for student 2
Each one has a name and an age. stud_rec[2] for student 3
Now you can access them like:
Each one has:
.name
.age Arrow operator (->): This is used to access structure
members through a pointer.
Structures Within a Structure Memory Address: The pointer holds the address of the
The idea in a structure within a structure is that the main structure, allowing indirect access to its members.
structure (i.e. mother structure) contains a structure from
another structure definition (i.e. child Linked List of Structures
structure). A Linked List is a way of storing a list of data using structures
and pointers.
A structure within a structure is like having a big box (the Unlike arrays, where elements are placed next to each other
"mother structure") that contains a smaller box (the in memory, linked lists use nodes that can be
"child structure") inside it. The small box can have its own anywhere in memory, and each node points to the next one.
stuff, but it’sstill part of the bigger box. This creates a “chain” of data.
For example:
Imagine you have a "Student" structure : Why Use a Linked List?
Name • Dynamic size You can easily add or remove data
Age without needing to know how big the
Grade • list is beforehand.
• Flexible structure You don’t have to shift all the data like
Inside this "Student" structure, you put another structure in arrays. Just fix the pointers!
called "Address" : • Efficient memory Memory is allocated only when
needed. No waste like with oversized arrays.
Street
City
Postal Code
Passing a Structure to a Function
When a structure is used as an argument to a function, the
entire structure is passed
using the standard Call by Value method.
In Call by Value, when a function is called, a copy of the
structure is passed to the function, not the original
structure. The function works with this copy, so any
changes made to it do not affect the
original structure.
Why do we use "Passing Structures to Functions"?
Passing a structure to a function simplifies the process by
sending all necessary data together in one package. This
avoids the need to handle individual data elements
separately and makes the code more efficient and easier to
maintain.
Structures allow us to group related data, such as a
person’s name and age, into a single entity. This helps to
maintain data in an organized and efficient manner making
it easier to manage and understand.
Pointers to Structures Explained
Pointers to structures allow you to work with the memory
address of a structure, instead of directly manipulating the
structure itself. This can be helpful when you want to avoid
copying large structures or when you're working with
dynamic memory allocation.
Pointer to a structure: You declare it like struct record
*ptr;.
File Processing Activities
1. Declaring a file stream
2. Opening a file stream
3. Writing to a file stream
4. Reading from a file stream
5. Closing a file stream
Modes of File Processing
1.Text Mode - value written to
the file is the actual sequence of
characters .
2. Binary Mode – value written
to the file is converted into a
sequence of bytes.
Modes of File Processing
Text Files: A text file contains data in the form of characters
and is generally used to store a stream of characters.
• Each line in a text file ends with a new line character
('\n').
• It can be read or written by any text editor.
• They are generally stored with .txt file extension.
• Text files can also be used to store the source code.
Binary Files: A binary file contains data in binary form (i.e.
0's and 1's) instead of characters. They contain data that is
stored in a similar manner to how it is stored in the main
memory.
• The binary files can be created only from within a
program and their contents can only be read by a
program.
• More secure as they are not easily readable.
• They are generally stored with .bin file extension.
FILE POINTER - is a reference to a particular position in the
opened file. It is used in file handling to perform all file
operations such as read, write, close, etc. We use the FILE
macro to declare the file pointer variable. The FILE macro is
FILE HANDLING - File handling refers to how we store the defined inside <stdio.h> header file.
available data or info in a file with the help of a program. The
C language stores all the data available in a program into a DECLARING A FILE
file with the help of file handling in C. A file pointer should be declared
File Characteristics Syntax:
1. Has a name FILE *;
2. Must be opened and closed Examples:
3. Can be written to, or read from, or appended to FILE *fp;
4. Closing the files FILE *fp1,*fp2;
OPENING A FILE
To open a file, the fopen() function is invoked. This function
opens a file and associate a stream with it.
Syntax: fopen(<file name>,<mode> );
Note: Filename must be a string that is valid for the
operating system to identify. It must have a maximum of 8
characters for the main name and 3 characters for the
extension name. Mode points to a string containing the
desired open status.
File Opening Modes
File opening modes or access modes specify the allowed
operations on the file to be opened. They are passed as an
argument to the fopen() function.
Operators/Functions Used For File Handling In C
There are various functions that can be used to open a file,
read its contents, write additional data, create a new file,
close or delete a file, search for a file, etc. These functions
are known as file handling operators in C.
Deleting and Renaming a Filename
Deleting a Filename
Syntax: rename(old filename, new filename);
Example: rename(“sample.txt”,”new.txt”);
Renaming a Filename
Syntax:
Example:
remove(filename);