Professional Documents
Culture Documents
Disks
Generally, a disk is a round plate on which data can be encoded. There are two basic
types of disks: magnetic disks and optical disks.
Magnetic Disks
On magnetic disks, data is encoded as microscopic magnetized needles on the disk's
surface. You can record and erase data on a magnetic disk any number of times, just as
you can with a cassette tape. Magnetic disks come in a number of different forms:
floppy Disk: A typical 5¼-inch floppy disk can hold 360K or 1.2MB (megabytes).
3½-inch floppies normally store720K, 1.2MB or 1.44MB of data. Floppy disks or
obsolete today, and are found on older computer systems.
Hard Disk: Hard disks can store anywhere from 20MB to more than 1-TB
(terabyte). Hard disks are also from 10 to 100 times faster than floppy disks.
Removable Cartridge: Removable cartridges are hard disks encased in a metal
or plastic cartridge, so you can remove them just like a floppy disk. Removable
cartridges are very fast, though usually not as fast as fixed hard disks.
Optical Disks
Optical disks record data by burning microscopic holes in the surface of the disk with a
laser. To read the disk, another laser beam shines on the disk and detects the holes by
changes in the reflection pattern.
CD-ROM: Most optical disks are read-only. When you purchase them, they are
already filled with data. You can read the data from a CD-ROM, but you cannot
modify, delete, or write new data.
WORM: Stands for write-once, read-many. WORM disks can be written on once
and then read any number of times; however, you need a special WORM disk
drive to write data onto a WORM disk.
erasable optical (EO): EO disks can be read to, written to, and erased just like
magnetic disks.
The machine that spins a disk is called a disk drive. Within each disk drive is one
or more heads (often called read/write heads) that actually read and write data.
Memory
A memory is just like a human brain. It is used to store data and instructions. Computer
memory is the storage space in the computer, where data is to be processed and
instructions required for processing are stored. The memory is divided into large
number of small parts called cells. Each location or cell has a unique address, which
varies from zero to memory size minus one. For example, if the computer has 64k
words, then this memory unit has 64 * 1024 = 65536 memory locations. The address of
these locations varies from 0 to 65535.
Memory is primarily of three types −
Cache Memory
Primary Memory/Main Memory
Secondary Memory
Cache Memory
Cache memory is a very highspeed semiconductor memory which can speed up the
CPU. It acts as a buffer between the CPU and the main memory. It is used to hold those
parts of data and program which are most frequently used by the CPU. The parts of
data and programs are transferred from the disk to cache memory by the operating
system, from where the CPU can access them.
Advantages
The advantages of cache memory are as follows −
Disadvantages
The disadvantages of cache memory are as follows –
Cache memory has limited capacity.
It is very expensive.
Primary Memory (Main Memory)
Primary memory holds only those data and instructions on which the computer is
currently working. It has a limited capacity and data is lost when power is switched off. It
is generally made up of semiconductor device. These memories are not as fast as
registers. The data and instruction required to be processed resides in the main
memory. It is divided into two subcategories RAM and ROM.
1. Random Access Memory (RAM) –
It is also called as read write memory or the main memory or the primary
memory.
The programs and data that the CPU requires during execution of a program are
stored in this memory.
It is a volatile memory as the data loses when the power is turned off.
RAM is further classified into two types- SRAM (Static Random Access
Memory) and DRAM (Dynamic Random Access Memory).
Some popular Operating Systems include Linux Operating System, Windows Operating
System, VMS, OS/400, AIX, z/OS, etc.
Definition
An operating system is a program that acts as an interface between the user and the
computer hardware and controls the execution of all kinds of programs.
Memory Management
Processor Management
Device Management
File Management
Security
Control over system performance
Job accounting
Error detecting aids
Coordination between other software and users
Compilers
Compiler, Computer software that translates (compiles) source code written in a high-
level language (e.g., C++) into a set of machine-language instructions that can be
understood by a digital computer’s CPU. Compilers are very large programs, with error-
checking and other abilities. Some compilers translate high-level language into an
intermediate assembly language, which is then translated (assembled) into machine
code by an assembly program or assembler. Other compilers generate machine
language directly.
A computer is a device that can accept human instruction, processes it and responds to
it or a computer is a computational device which is used to process the data under the
control of a computer program. Program is a sequence of instruction along with data.
Input unit
Central Processing Unit (CPU)
Output unit
Memory unit
Control unit
Arithmetic Logic unit
Print "passed"
else
Print "failed"
Variables
A variable is nothing but a name given to a storage area that our programs can
manipulate. Each variable in C has a specific type, which determines the size and
layout of the variable's memory; the range of values that can be stored within that
memory; and the set of operations that can be applied to the variable.
The name of a variable can be composed of letters, digits, and the underscore
character. It must begin with either a letter or an underscore. Upper and lowercase
letters are distinct because C is case-sensitive. Based on the basic types explained in
the previous chapter, there will be the following basic variable types –
1. Char: Typically a single octet (one byte). It is an integer type.
2. Int: The most natural size of integer for the machine.
3. Float: A single-precision floating point value.
4. Double: A double-precision floating point value.
5. Void: Represents the absence of type.
Data Types
Data types in c refer to an extensive system used for declaring variables or functions of
different types. The type of a variable determines how much space it occupies in
storage and how the bit pattern stored is interpreted.
Linker Errors: These error occurs when after compilation we link the different object
files with main’s object using Ctrl+F9 key (RUN). These are errors generated when the
executable of the program cannot be generated. This may be due to wrong function
prototyping, incorrect header files. One of the most common linker error is writing Main
() instead of main ().
Semantic errors: This error occurs when the statements written in the program are not
meaningful to the compiler.
Arrays
An array is a collection of items stored at contiguous memory locations. The idea is to
store multiple items of the same type together. This makes it easier to calculate the
position of each element by simply adding an offset to a base value, i.e., the memory
location of the first element of the array (generally denoted by the name of the array).
The base value is index 0 and the difference between the two indexes is the offset.
For simplicity, we can think of an array a fleet of stairs where on each step is placed a
value (let’s say one of your friends). Here, you can identify the location of any of your
friends by simply knowing the count of the step they are on.
Remember: “Location of next index depends on the data type we use”.
The above image can be looked at as a top-level view of a staircase where you are at
the base of the staircase. Each element can be uniquely identified by their index in the
array (in a similar way as you could identify your friends by the step on which they were
on in the above example).
Advantages of using arrays:
Arrays allow random access of elements. This makes accessing elements by
position faster.
Arrays have better cache locality that can make a pretty big difference in
performance.
You can’t change the size i.e. once you have declared the array you can’t change its
size because of static memory allocated to it.
Now if take an example of implementation of data structure Stack using array there are
some obvious flaw.
Let’s take the POP operation of the stack. The algorithm would go something like this.
Output
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20
Call by reference in C
In call by reference, the address of the variable is passed into the function call as
the actual parameter.
The value of the actual parameters can be modified by changing the formal
parameters since the address of the actual parameters is passed.
In call by reference, the memory allocation is similar for both formal parameters
and actual parameters. All the operations in the function are performed on the
value stored at the address of the actual parameters, and the modified value gets
stored at the same address.
Call by reference Example: Swapping the values of the two variables
1. #include <stdio.h>
2. void swap(int *, int *); //prototype of the function
3. int main()
4. {
5. int a = 10;
6. int b = 20;
7. printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing th
e value of a and b in main
8. swap(&a,&b);
9. printf("After swapping values in main a = %d, b = %d\n",a,b); // The values of ac
tual parameters do change in call by reference, a = 10, b = 20
10. }
11. void swap (int *a, int *b)
12. {
13. int temp;
14. temp = *a;
15. *a=*b;
16. *b=temp;
17. printf("After swapping values in function a = %d, b = %d\n",*a,*b); // Formal pa
rameters, a = 20, b = 10
18. }
Output
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10
1 A copy of the value is passed into the An address of value is passed into the function
function
2 Changes made inside the function is limited Changes made inside the function validate outside
to the function only. The values of the of the function also. The values of the actual
actual parameters do not change by parameters do change by changing the formal
changing the formal parameters. parameters.
3 Actual and formal arguments are created at Actual and formal arguments are created at the
the different memory location same memory location
Linear Search
Linear search in C to find whether a number is present in an array. If it's present, then at
what location it occurs. It is also known as a sequential search. It is straightforward and
works as follows: we compare each element with the element to search until we find it
or the list ends. Linear search for multiple occurrences and using a function.
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);
return 0;
}
Output
Binary Search
Binary search in C language to find an element in a sorted array. If the array isn't sorted,
you must sort it using a sorting technique such as merge sort. If the element to search is
present in the list, then we print its location. The program assumes that the input
numbers are in ascending order.
Binary Search Program
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}
Output
Bubble Sort
Bubble sort in C to arrange numbers in ascending order, you can modify it for
descending order and can also sort strings. The bubble sort algorithm isn't efficient as
its average-case complexity is O(n2) and worst-case complexity is O(n2). There are
many fast sorting algorithms like Quicksort, heap-sort, and others. Sorting simplifies
problem-solving in computer programming.
Bubble Sort Program
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
Output
Insertion Sort
Insertion sort in C: C program for insertion sort to sort numbers. This code implements
insertion sort algorithm to arrange numbers of an array in ascending order. With a little
modification, it will arrange numbers in descending order. Best case complexity of
insertion sort is O(n), average and the worst case complexity is O(n2).
Insertion Sort Program
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t, flag = 0;
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 1 ; c <= n - 1; c++) {
t = array[c];
for (d = c - 1 ; d >= 0; d--) {
if (array[d] > t) {
array[d+1] = array[d];
flag = 1;
}
else
break;
}
if (flag)
array[d+1] = t;
}
for (c = 0; c <= n - 1; c++) {
printf("%d\n", array[c]);
}
return 0;
}
Output
Quick Sort
Quicksort is a divide and conquer algorithm. The steps are: 1) Pick an element from the
array, this element is called as pivot element. 2) Divide the unsorted array of elements
in two arrays with values less than the pivot come in the first sub array, while all
elements with values greater than the pivot come in the second sub-array (equal values
can go either way). This step is called the partition operation. 3) Recursively repeat the
step 2(until the sub-arrays are sorted) to the sub-array of elements with smaller values
and separately to the sub-array of elements with greater values. The same logic we
have implemented in the following C program.
Quick Sort Program
#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main(){
int i, count, number[25];
quicksort(number,0,count-1);
return 0;
}
Output
d = b*b - 4*a*c;
if (d < 0) { //complex roots
printf("First root = %.2lf + i%.2lf\n", -b/(double)
(2*a), sqrt(-d)/(2*a));
printf("Second root = %.2lf - i%.2lf\n", -b/(double)
(2*a), sqrt(-d)/(2*a));
}
else { //real roots
root1 = (-b + sqrt(d))/(2*a);
root2 = (-b - sqrt(d))/(2*a);
return 0;
}
Output
Enter a, b and c where ax^2+bx+c=0
1 -2 1
First Root = 1.00
Second Root = 1.00