Pages

Monday, July 15, 2013

C language III (array , structures , union , pointers , graphics in c )


ARRAY : An Array is a fixed-size sequenced collection of elements of the same data type. Array can be bsed to represent a list of numbers, or a list of names i.e. list of employees in an organization , Attendence record of any student in a class … etc.



ONE DIMENSIONAL ARRAY : A group of related data elements arranged in a single row or column.
To declare one-dimensional integer array d of size 10 , you would write int d[10] and for string array char d[50]; where 50 is maximum 50 characters can be inputed in char name d.
Ex: {4.3,2.4,3.2,4.9,5.0} , name= “ DENNIS RITCHIE”

TWO DIMENSIONAL ARRAY OR MULTI-DIMENSIONAL ARRAY :  A two-dimensional array is, essentially, an array of one-dimensional arrays. To declare a two-dimensional integer array d of size 10,20, you would write   int d[10][20]; and for character array same as one-dimensional array.
Ex :
5 7 -6
4 5 7
9 1 3  
This is d[3][3] array.
STRING HANDLING FUNCTIONS IN C
• strlen( ) : Function is used to find the length of a character string.
Ex:
int n;
char st[20] = “ New Delhi”;
n=strlen(st);
• strcpy( ) : Function is used to copy a character string to a character variable.
Ex:
 char city[15];
strcpy( city, “ New Delhi”)
This makes variable city as string New Delhi.
• strcat( ) : Function is used to join two character strings.
Ex:
 char city[20] = “Mumbai”;
 char pin[8] = “- 560001”
strcat(city,pin);

• strcmp( ) : Function is used to compare two character strings. If result is 0 then both are same strings ,  if result is negative value then strings are in ascending order and if result is positive integer then string is in descending order.
Ex :
char city[15] = “MADRAS”;
char town[15] = “MANGALORE”;
strcmp( city, town);
Which give -10 as result which is first mismatching character difference of D and N based on ASCII code value.
• atoi( ) : Function is a C library function used to convert a string of digits to the integer value.

Ex :
char st[10] = “24175” ;
int n;
n = atoi(st);

FUNCTIONS IN C

Functions are subprograms which is used to compute a value or perform any given task. It is of two types i.e. Library functions and User-defined functions.
Library function are used to perform standard operations like sqrt(x), scanf( ), printf( ) etc. which is given along with compiler and User-defined functions are block of statements which is written by user to compute a value or to perform any task.

FUNCTION DECLARATION

datatype  functionname( variable declaration from 1 to n numbers)


     
STRUCTURES AND UNIONS
A structure is a user defined compound data type which consists of data members related to a person or an item. General form of structure is or structure is declared as:
struct name;
{
    datatype name1;
    datatype name2;
   ………………….
    ………………...
};
struct name hello;
Here, hello is variable name of structure “name”.
UNION
Union is a concept adopted from structures.It has same syntax like as structures. In union only one data with name is active at a time. It is used in cases where user have to select any one datatype in the group of datatypes.
Ex:
union student
{
    int m;
    float x;
    char c;
}hello;

POINTERS
 A pointer is a special kind of variable in C and C++ that holds the address of another variable.
Pointers and arrays are two sides of the same coin. To write any kind of non trivial application in C, pointers are needed. Pointer declaration is preceded by the * (asterisk) symbol. It is declared like a variable with appropriate data type. It is declared as:

Ex : datatype  *name ;

Here, “name” is the pointer name just like variable.

CALL BY VALUE :  Passing a variable by value makes a copy of the variable before passing it onto a function. This means that if you try to change the value inside a function, it will only have the changed value inside that function. One the function returns, the variable you passed it will have the same value it had before you passed it into the function. It is called as :  name(var1,var2).

CALL BY REFERENCE :  The address of variable are substituted to the pointers and any alteration to its value inside the function is automatically carried out in that location. The change is indirectly made and is accepted by the calling program. It is called as :  name(&var1 , &var2).


FILES IN C

File system in C language is used to store any program in permanently storage device i.e. hard disk etc..

• fopen( ) : Creates a new file or open a saved file.
• fclose( ) : Closes a file which is open.
• getc( ) : Reads a character from a file.
• putc( ) : Writes a character to a file.
• feof( ) : Used to locate end of a file.
• getw( ) : Used to read an integer data from file.
• putw( ) : Used to write an integer data to the file.
• fscanf( ) : It is similar to scanf( ) but fscanf( ) is used to read data from the disk or called as it is used to read data from file.
• fprintf( ) : Used to write data to a file. It is also similar to printf( )  but difference is that it is used to write data to the disk.
• rewind( ) :  Used to move the file pointer to the beginning of a file. Used as rewind(fp); .
• fseek( ) : Used to move the file pointer to any position in a file from a given reference psiotion.
• fread( ) : Used to read a structure from a file.Note that the values of the data members in the structure are read together.
• fwrite( ) : Used to write a structure to a file.The values of the data members in the structure are written together.
• ftell( ) : Used to locate the current position of the pointer.
• remove( ) : Used to erase or remove a file from the disk. Ex: remove(“filename”);
• rename( ) : Used to rename the file. Ex: rename(“old filename”,”new filename”);


GRAPHICS KEYWORDS IN C



• textmode( ) : Used to set the numbers of rows and columns of the monitor screen.It is written as : textmode (m);
where m is an integer as mode value.
Ex: textmode(1); or textmode(c40);

Text mode values
Symbol Mode value Text mode
BW40 0 B/W with 25 rows & 40 columns
c40 1 Color with 25 rows & 40 columns
BW80 2 B/W with 25 rows & 80 columns
c80 3 Color with 25 rows & 80 columns
LASTMODE -1 Restore previous

• textbackground( ) : Used to set the background color in the text mode. It is written as:
  textbackground(n);
   where n is the background color value.

Ex: textbackground( 1); or textbackground(BLUE);

Text background color values
Symbol Color value Symbol Color value
BLACK 0 LIGHTBLUE 9
BLUE 1 LIGHTGREEN 10
GREEN 2 LIGHTCYAN 11
CYAN 3 LIGHTRED 12
RED 4 LIGHTMAGENTA 13
MAGENTA 5 YELLOW 14
BROWN 6 WHITE 15
LIGHTGRAY 7 BLINK 128
DARKGRAY 8


• textcolor( ) : Used to set the color of the text. It is written as: textcolor (n );
where n is the color value.

Ex: textcolor(4); or textcolor(RED)

• gotoxy( ) : Used to move the cursor to any specific location on the screen. It is written as:
gotoxy(x,y);
where x and y are integers which is column and row positions respectively.
Ex: gotoxy(20,10);
it will move the cursor the 20th column and 10th row.

• clrscr( ) : Used to clear the screen. It is written as : clrscr( );
• cputs( ) : Used to display a text message in the monitor screen in the graphic mode. it is written as : cputs( “text message “);

Ex: cputs(“ How are you”);
this will show the given message in double quotes.

• setbkcolor( ) : Used to to set the background color for the monitor screen. It is written as : setbkcolor(m);.
where m is a color value.

Ex: setbkcolor(1); or setbkcolor(RED);

• setpalatte( ) : Used to setbackground color with the index.
    Ex: setpalatte(0,4);  or setpalatte(0,RED);
this will set the background color ted with index.

• line( ) : Used to draw a line on the screen in the given coordinate position. It is written as :
line(x1,y1,x2,y2);
where x1 and y1 represents starting coordinates and x2 and y2 are end coordinates of the line.

Ex : line(80,50,300,200);

• moveto( ) : Used to move the control from current position to the given position of coordinate. It is written as : moveto(x,y);  , where x and y are the final coordinate position.

Ex : moveto(200,300);

• lineto( ) : Used to draw a line from current position to the given coordinate. It is written as :    lineto(x,y);  , here x and y are end point of the line.

Ex : lineto(100,200);
it will draw a line from current cursor position to given position.

• rectangle( ) : Used to draw a rectangle by specifying the diagonal coordinates. It is written as : rectangle(x1,y1,x2,y2);
     where x1 and y1 represents the top left coordinates of the rectangle and x2 and y2 represents bottom right coordinates of the rectangle.
Ex : rectangle(100,80,300,200);
• circle( ) : Used to draw circle. It is written as :  circle(x,y,r);  , where x and y are center of the circle and r is the integer representing radius of the circle.
Ex : circle(150,200,40);  , it will draw a circle with coordinates (150,200) as center and 40 as radius.

• arc( ) : Used to draw a circular arc by specifying the centre , radius , and starting and ending angles. It is written as : arc(x,y,sa,ea,r);  , where x and y are centre coordinates of arc , sa is starting angle of the arc , ea is ending angle of arc and r is the radius of arc.
Ex : arc(150,200,30,90,60) , it will draw arc with coordinates (150,200) and 60 as radius and starting angle is 30° and ending angle is 90°
• ellipse( ) : Used to draw an ellipse or elliptical arc by specifying the center , radius,start and end angles and the semi-major and semi-minor axes of the ellipse. It is written as : ellipse(x,y,sa,ea,xr,yr);
   where x and y are center coordinates of ellipse
 sa is the starting angle and ea is the end angle of ellipse
 xr is and semi-major axis of ellipse and
yr is the semi-minor axis of the ellipse.
Ex : ellipse(150,200,0,360,60,25) ,  it will draw an ellipse with center coordinates(150,200),major axis 120(i.e. 60+60) and minor axis 50(i.e. 25+25).
• bar( ) : Used to draw a rectangular bar using the diagonally opposite corners. It is written as : bar(x1,y1,x2,y2);
where x1 and y1 are top left coordinates of bar and
 x2 and y2 are bottom right coordinates of the bar.
Ex : bar(100,80,120,250);
• bar3d( ) : Used to draw a three dimensional view of a rectangular bar using the diagonally opposite corners of a rectangle with specified depth. It is written as : bar3d(x1,y1,x2,y2,d,tf);
where ,
 x1 and y1 are top left coordinates of bar and
 x2 and y2 are bottom right coordinates of the bar
 d is an integer represents the depth of the bar which is the third dimensions of the bar and
tf(top flag) is the integer which is non-zero value to add the top to the bar. No top is added to the bar when tf is zero.
• malloc( ) : Used to allocate a block of memory to store the data. It is written as: malloc(byte size);
Ex: malloc(400)
it will allocate 400 byte in a block to store data.
malloc(imagesize(50,40,200,230));
it will allocate the number of bytes to store the image which lies inside the rectangle with diagonal coordinates (50,40) and (200,230).
NOTE: In all graphics keywords give #include<graphics.h> header file is used.

No comments:

Post a Comment