Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12

Linked at EntranceNepal.com, i-geek blog & Ekendra's Share World

Object I. To write a program to sort an array in ascending orders using function and pointer.

Object II. To write a program using structure to read records of 3 students and print the records.

Download this full program | Lab 11 | in pdf format

Background Theory:

Function: A function is a self contained program segment that carries out some specific, well-defined task. Every C program consists of one or more functions. A function is simply a chunk of C code (statements) that you have grouped together and given a name. The value of doing this is that you can use that "chunk" of code repeatedly simply by writing its name. One of the functions must be called main. Execution of the program will always begin by carrying out the instructions in main. Additional functions will subordinate to main, and perhaps to one another.

Pointer: A pointer is a variable that represents the location rather than the value of a data item, such as a variable or an array element. They can be used to pass information back and forth between a function and its reference point. In particular, pointer provides a way to return multiple data items from function via function arguments. Pointers are also closely associated with arrays and therefore provide an alternative way to access individual array elements.

 Passing pointers to a function: Pointers are often passed to a function as arguments. This allows data items within the calling portion of the program to be accessed by the function, altered within the function, and then returned to the calling portion of the program in altered form. This is use of pointers by passing reference or by address or by the location, in contrast to passing arguments by value.

 When an argument is passed by value, the data item is copied to the function. Thus, any alteration made to the data item within the function is not carried over into the calling routine. When an argument is passed by reference, however (ie when a pointer is passed to a function), the address of a data item is passed to the function.

Structure: A structure is a convenient tool for handling a group of logically related data items. A structure definition creates a format that may be used to declare structure variables. A simple example follows:

struct book

{

          char title[50];

          char author[50];

          int pages;

float price;

};

struct book book1, book2, book3;

 

The members of a structure themselves are not variables. They do not occupy any memory until they are associated with the structure variables such as book1. The template is terminated with a semicolon. Both the template declaration and variables declaration can also be combined in one statement. Normally, structure definitions appear at the beginning of the program file, before any variables or functions are defined. They may also appear before the main, along with macro definitions as #define. In such cases, the definition is global and can be used by other functions as well.

 

Code:

#include<stdio.h>

#include<conio.h>

void sort(int *b, int n)

void main()

{

            int a[50], i, n;

clrscr();

printf(\nDeclare the array size:\t);

scanf(%d, &n);

 

printf(\nEnter these array elements\n);

for(i=0; i<n; a(i))

scanf(%d, &a[i]);

 

sort(a, n);

printf(\nNumbers in ascending order:\n);

for(i=0; i<=n; i++)

printf(%d\t, *(a+i));

getch();

}

 

void sort(int*b, int n)

{

            Int i, j, temp;

for(i=0; i<=n; i++)

for(j=i+1; j<n; j++)

{

            If(*(b+i)>*(b+j))

            {

                        temp=*(b+j);

                        *(b+j)=*(b+i);

                        *(b+i)=temp;

            }

}

return;

            }

#include<stdio.h>

#include<conio.h>

struct student

     {      char name[20];

            int roll;

            int telephone;

     };

 

struct student s[3];

 

void main()

{

            int i, n=3;

            clrscr();

            printf("Enter student details in format NAME, ROLL, TELEPHONE\n\n");

            printf("\nEnter3 record\n");

            for(i=0;i<n;i++)

            scanf("\t%s\t%d\t%d", s[i].name, &s[i].roll, &s[i].telephone);

 

            printf("\nThe records you have entered is");

            for(i=0;i<n;i++)

            printf("\n%s\t%d\t%d", s[i].name, s[i].roll, s[i].telephone);

            getch();

}

To Download this full program | Lab 11 | in pdf format you must have PDF reader as Adobe Acrobat Reader 5 and above in your computer,

click here to download now.

Search yourself for more such codes

Google

Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12

Protected under Creative Commons License.
Written by Ekendra Lamsal, www.ekendralamsal.com
for comments email at gmail address:
gearShifts@gmail.com