Posts

Showing posts with the label c programming

How recursive bubble sort work

                                              Recursive Bubble Sort Base Case: If array size is 1, return. Do One Pass of normal Bubble Sort. This pass fixes last element of current subarray. Recur for all elements except last of current subarray. Example: Index=     0 1  2 3 4 5 6 7   100                          90 80 70 60 50 40 30  Array []= Suppose we want to sort the above in the increasing order by the recursive bubble sort. Make a get grater which will return the grater of the array. Compare the first and last element of the array if the first is greater than last the swap with each other. And again call the sort function and decrement the array size by 1; 1) 30                     90 80 70 60 50 40 100    ...

File Handling

File – a file represent a sequence of byte on the disk where a group of related data is stored. File is created for permanent storage of data. It is ready made structure. In C, we use a structure pointer of the file type to declare a file. C programming language provides number of functions to perform basic file operations. Few of them are as under:   Function Description fopen() Create a new file or open an existing file fclose() Closes a file getc() Reads a character from a file putc() Writes a character to a file fscanf() Reads a set of data from a file fprintf() Writes a set of date to the file getw() Reads an integer from a file Opening Files – fopen() function is used to create a new file or open an existing file, this call will initialize an object of the type FILE, which contains all the information necessary to ...

Array of Structures

Arrays of structure s are good for storing of complete set of data that fits within the structure format. Arrays of Structures store together several values of different types, grouped as structures. Arrays of structures also provide a means of holding data you read from the disk. Declaring an Array of Structures is easy as you specify the number of reserved structures inside array brackets when you declare the structure variable. For example, the following structure declaration creates 1000 store structures, each containing three members. struct stores {      int                      employees;      int                      registers;      float             ...

Structures

Structure is a collection of one or more variable types. As we know, the elements in an array must be the same data type, and you must refer to the entire array by its name. Each element (called a member) in a structure can be a different data type. Structures have ability to group data of different data types together but work with that data as a whole. Suppose a company has inventory system so item name will be character array, quantity in stock will be integer and retail price will be in double, so to deal with such system we will use structure.   To define a structure, we must use the struct statement. Struct defines for your program a new data type with more than one member. The format of the struct statement is struct  {    Member definition;    Member definition;    Member definition;    .    .    . } [one or more structure variables] Each member definitio...

Strings in C

Image
A string is a series of characters treated as a single unit. A string may include letters, digits and various special characters such as +, -, *, / and $. String literals, or string constants in C are written in double quotations marks. A string in C is an array of characters ending in the null character (‘\0’). There is null character at end of each and every string which indicates ending or termination of a string. Format String specifier is %s in printf and scanf. The value of string is address of its first character.   Declaration and initialization of string can be done by using either a character array or a variable of type char pointer as shown below: Char color[] = “blue”; Char *colorptr = “blue”; In which each initialize a variable to the string “blue”. The first declaration creates five element array color containing the characters ‘b’, ‘l’, ‘u’, ‘e’ and ‘\0’. The second declaration creates pointer variable colorptr that points to the string “blue” somew...

Pointers in C

Image
Pointer is a variable that points to or references a memory location in which data is stored. This means that a pointer holds the memory address of another variable. Put another way, the pointer does not hold a value in the traditional sense; instead, it holds the address of another variable. A pointer "points to" that other variable by holding a copy of its address. Because a pointer holds an address rather than a value, it has two parts. The pointer itself holds the address and that address points to a value. Pointer declaration is shown below. You start by specifying the type of data stored in the location identified by the pointer. The asterisk tells the compiler that you are creating a pointer variable. Finally you give the name of the variable. Data-type *variable-name Such a variable is called a pointer variable. When we define a pointer variable we do so by preceding its name with an asterisk. In C, we also give our pointer a type which, in this case, refe...