Showing posts with label Department of CSE. Show all posts
Showing posts with label Department of CSE. Show all posts

Dynamic Memory Allocation


Lecture 24

Your computer's memory is a resource - it can run out. The memory usage for program data can increase or decrease as your program runs.
Up until this point, the memory allocation for your program has been handled automatically when compiling. However, sometimes the computer doesn't know how much memory to set aside (for example, when you have an unsized array).
The following functions give you the power to dynamically allocate memory for your variables at RUN-TIME (whilst the program is running). For the past tutorials, memory was allocated when the program was compiled (i.e. COMPILE-TIME).
To use the four functions discussed in this section, you must include the stdlib.h header file.

More on Files (Part 2)


Lecture 23

Record in Files

So far we have dealt with reading and writing only characters and strings. What if we want to read or write numbers from/to file? Furthermore, what if we desire to read/write a combination of characters, strings and numbers? For this first we would organize this dissimilar data together in a structure and then use fprintf( ) and fscanf( ) library functions to read/write data from/to file. Following program illustrates the use of structures for writing records of employees.

More on Files


Lecture 22


A file-copy Program

We have already used the function fgetc( ) which reads characters from a file. Its counterpart is a function called fputc( ) which writes characters to a file. As a practical use of these character I/O functions we can copy the contents of one file into another, as demonstrated in the following program. This program takes the contents of a file and copies them into another file, character by character.

Introduction to File Input/Output


Lecture 21


There are different operations that can be carried out on a file. These are-
1.    Creation of a new file
2.    Opening an existing file
3.    Reading from a file
4.    Writing to a file
5.    Moving to a specific location in a file (seeking)
6.    Closing a file

Let us now write a program to read a file and display its contents on the screen. We will first list the program and show what it does, and then dissect it line by line.

More on Structures


Lecture 20

The values of a structure variable can be assigned to another structure variable of the same type using the assignment operator. It is not necessary to copy the structure elements piece-meal. Obviously, programmers prefer assignment to piece-meal copying. This is shown in the following example.

Structures


Lecture 19

C language wouldn’t have been so popular had it been able to handle only all ints, or all floats or all chars at a time. In fact when we handle real world data, we don’t usually deal with little atoms of information by themselves—things like integers, characters and such. Instead we deal with entities that are collections of things, each thing having its own attributes, just as the entity we call a ‘book’ is a collection of things such as title, author, call number, publisher, number of pages, date of publication, etc. As you can see all this data is dissimilar, for example author is a string, whereas number of pages is an integer. For dealing with such collections, C provides a data type called ‘structure’. A structure gathers together, different atoms of information that comprise a given entity.

Recursion


Lecture 18

In C, it is possible for the functions to call themselves. A function is called ‘recursive’ if a statement within the body of a function calls the same function. Sometimes called ‘circular definition’, recursion is thus the process of defining something in terms of itself.

Let us now see a simple example of recursion. Suppose we want to calculate the factorial value of an integer. As we know, the factorial of a number is the product of all the integers between 1 and that number. For example, 4 factorial is 4 * 3 * 2 * 1. This can also be expressed as 4! = 4 * 3! where ‘!’ stands for factorial. Thus factorial of a number can be expressed in the form of itself. Hence this can be programmed using recursion. However, before we try to write a recursive function for calculating factorial let us take a look at the non-recursive function for calculating the factorial value of an integer.

Strings and Pointers


Lecture 17

In the previous lecture, we have seen some of the string library functions. The best use of pointers to manipulate string can be illustrated with the help of making those library functions into user defined functions- which means you can develop user defined functions that will perform exactly the same way strln ( ), strcpy ( ), and strcmp ( ) works.

strln ( )

Take a look at the following program. It performs the same task the library function strln () does.

Strings


Lecture 16

What are Strings

The way a group of integers can be stored in an integer array, similarly a group of characters can be stored in a character array. Character arrays are many a time also called strings. Many languages internally treat strings as character arrays, but somehow conceal this fact from the programmer. Character arrays or strings are used by programming languages to manipulate text such as words and sentences.
A string constant is a one-dimensional array of characters terminated by a null ( ‘\0’ ). For example,

char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '\0' } ;

Pointers and Arrays


Lecture 15

Passing Array Elements to a Function

Array elements can be passed to a function by calling the function by value, or by reference. In the call by value we pass values of array elements to the function, whereas in the call by reference we pass addresses of array elements to the function. These two calls are illustrated below:

Call by Reference


Lecture 14


Call by Reference

Arguments can generally be passed to functions in one of the two ways:
                (a) sending the values of the arguments
                (b) sending the addresses of the arguments

In the first method the ‘value’ of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function. With this method the changes made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function. The following program illustrates the ‘Call by Value’.

Introduction to Pointers


Lecture 13

The most crucial of all of C’s grammars is pointers. Many other programming languages have the similar concept as pointer in C, but they merely do not use the best of pointers. C uses pointers as its inevitable part. Though the understanding of pointers is the most difficult part in learning C, once you have the clean idea on pointers, you will see that by using pointers, you can solve any problem in C.

Consider the declaration-
int i = 3;

This declaration tells the C compiler to:
(a) Reserve space in memory to hold the integer value.
(b) Associate the name i with this memory location.
(c) Store the value 3 at this location.

Arrays: Multidimensional, Passing Arrays to Functions


Lecture 12

Multidimensional Arrays

Arrays in C can have multiple subscripts. A common use of multiple subscripted arrays is to represent tables of values consisting of information arranged in rows and columns. To identify a particular table element, we must specify two subscripts: the first identifies the row and the second identifies the column. Arrays that require two subscripts to identify a particular element are called double subscripted arrays. Note that, multidimensional arrays can have more than two subscripts. ANSI standard supports at least 12 subscripts.

When we say a 3X4 array, we declare it as follows-
int a [3][4];

When we declare in such a way, the actual thing that takes place in memory is illustrated below-

a [0] [0]
a [0] [1]
a [0] [2]
a [0] [3]
a [1] [0]
a [1] [1]
a [1] [2]
a [1] [3]
a [2] [0]
a [2] [1]
a [2] [2]
a [2] [3]
So, the first subscript denotes the row and the second subscript denotes the column.

Arrays: One Dimensional


Lecture 11

Arrays

In this lecture we will get familiar with one of the C’s most essential data structures called arrays. Arrays are data structures consisting of related data items of the same type. You can also say that it is a group of memory locations related by the fact that they all have the same name and same type.

Why Arrays

For understanding the arrays properly, let us consider the following program:

More on Functions, Scope Rules, Math Library Functions


Lecture 10

More on Functions

Let’s see the last program from our last lecture-
#include<stdio.h>
#include<conio.h>

int maximum(int,int,int); // function prototype

main(){
      int x,y,z;
      printf(“Enter three integers: ”);
      scanf(“%d %d %d”,&x,&y,&z);
      printf (“The maximum is %d”,maximum(x,y,z));
}

//function definition

int maximum(int a,int b, int c){
      int max=a;
      if(b>max)
            max=b;
      if(c>max)
            max=c;
      return max;
}

Introduction to Functions, Call by Value


Lecture 9

We have already known that in a C program, there must be a main () function. We use to call this main () function as the brain. Without brain, can you imagine any human? No! Similarly without a main () function, you cannot imagine a C program! In C, we used many library functions as well- printf (), scanf (), clrscr (), or getch (). printf() prints on the output, scanf () takes input from the user and assigns that input to a variable by going to the variable’s address, clrscr () clears the output buffer and getch () waits for a key-stroke from the user. Well, if you think uniform one thing is common to say- the function does one particular job. Isn’t it? In C, user can define functions as well- as many functions as they wish. If you have a program to take two integers from the user and add, subtract, multiply, divide and mod you can define 5 different functions and ask each of them to do that job respectively. How? We will take a look at it later!

C Data Types


Lecture 8

Data Types in C

The primary data types of C are integers (int), floating point numbers (float) and characters (char). Are they all? Do the C programmers use only these 3 data types and create brilliant programs? The answer is- No! Not only this, the primary data types themselves could be of several types. For example, a char could be an unsigned char or a signed char or an int could be a short int or a long int. Let’s take a tour to know what those are.

Integers: short and long

The range of integer depends on the compiler. Turb C/C++ is a 16 bit compiler. For such compiler the range of an integer is –32768 to 32767.

The do-while Loop, The switch Statement


Lecture 7

The do-while Loop

The do-while loop looks like-
There is a minor difference between the working of while and do-while loops. This difference is the place where the condition is tested. The while tests the condition before executing any of the statements within the while loop. In contrast, the do-while tests the condition after having executed the statements within the loop. do-while would execute its statements at least once, even if the condition fails for the first time. The while, on the other hand will not execute its statements if the condition fails for the first time.

The for Loop, The break Statement, The continue Statement


Lecture 6

The for Loop

The for loop is the most popular looping control among all. The for loop allows us to specify three things in a single line:
a)    Setting a loop counter to an initial value.
b)    Testing the loop counter to determine whether its value has reached the number of repetitions desired.
c)    Increasing/decreasing the value of loop counter each time the program segment within the loop is executed.

The general form of for statement is as follows:

for (initialize counter; test counter; increment/decrement counter){
      do this;
      and this;
      and this;
}
For example, the program below prints 1 to 10.
main(){
      int counter;
      for (counter=1;counter<=10;counter++){
            printf (“%d\n”, counter);
      }
}

Essentials of Repetition, The while Loop


Lecture 5

Essentials of Repetition


Most C programs involve in repetition or looping. A loop is a group of instructions the computer executes while some loop continuation condition remains true. There are two types of repetition.

1. Counter-controlled repetition
2. Sentinel-controlled repetition

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by CelebrityDisk | Written by Alamin - link | Grants For Single Moms