Apr 17, 2014

C Programs

A Program to perform multiplication of two 3x3 and 3x2 matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], b[3][2],c[3][2],i,j,k;
printf("Enter elements for matrix A:\n");
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)

{
scanf("%d",&a[i][j]);
}
}
printf("Enter element for matrix B:\n");
for (i=0;i<3;i++)
{
for (j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
for (i=0;i<3;i++)
{
for (j=0;j<2;j++)
{
c[i][j]=0;
for (k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\n Product of the given matrices is:\n");
for (i=0;i<3;i++)
{
for (j=0;j<2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();

}


Transpose of Matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][5], b[5][3],i,j;
printf("Enter elements for matrix A:\n");
for (i=0;i<3;i++)
{
for (j=0;j<5;j++)
{
scanf("%d",&a[i][j]);
}
}
for (i=0;i<5;i++)
{
for (j=0;j<3;j++)
{
b[i][j]=a[j][i];
}
}
printf("Transpose of matrix A is:\n");
for (i=0;i<5;i++)
{
for (j=0;j<3;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
getch();
}

A program to find the sum of squares of elements on a diagonal of a square matrix.
#include <stdio.h>
#include<conio.h>
void main ()
{
staticint array[10][10];
int i, j, m, n, a = 0, sum = 0;
printf("Enetr the order of the matix \n");
scanf("%d %d", &m, &n);
if (m == n )
 {
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i)
 {
for (j = 0; j < n; ++j)
  {
scanf("%d", &array[i][j]);
   }
  }
printf("The given matrix is \n");
for (i = 0; i < m; ++i)
        {
for (j = 0; j < n; ++j)
   {
printf(" %d", array[i][j]);
  }
printf("\n");
   }
for (i = 0; i < m; ++i)
 {
sum = sum + array[i][i]*array[i][i];
 a = a + array[i][m - i - 1]*array[i][m-i-1];
   }
printf("\nThe sum of the main diagonal elements is = %d\n", sum);
printf("The sum of the off diagonal elemets is   = %d\n", a);
   }
else
printf("The given order is not square matrix\n");
getch();
}


A function to multiply two nxn matrices.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20][20],b[20][20],c[20][20],i,j,k,n;
printf("Enter size of matrix\n");
scanf("%d",&n);
printf("Enter element for matrix A:\n");
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter element for matrix B:\n");
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
c[i][j]=0;
for (k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\nProduct of two n x n matrices is:\n");
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}


A function to short a set on n numbers in ascending order of magnitude. 
#include<stdio.h>
#include<conio.h>
void sort(int m, int x[]);
void main()
{
inti,j,temp;
intn,a[100];
printf("Enter how many numbers you want to store\n");
scanf("%d",&n);
printf("Enter %d element\n",n);
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort (n,a);
printf("The sorted values are:\n");
for(i=0;i<n;i++)
{
printf("%4d",a[i]);
printf("\t");
}
getch();
}
void sort (int n, int a[])
{

inti,j,temp;
for (i=0;i<=n-1;i++)
    {

for (j=0;j<=n-1;j++)
        {
if (a[j]<=a[j-1])
            {
temp=a[j-1];
a[j-1]=a[j];
a[j]=temp;
            }
        }
    }
}


A program to delete all vowels from a sentences. Assume that the sentence is not more than 80 characters long.

#include<stdio.h>
#include<string.h>
void main(){
charstr[80],s[80];
inti,j=0;
printf("Enter any string->\n");
gets(str);
printf("The string is->   %s  ",str);
for(i=0;i<=strlen(str);i++)
    {
        if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u' ||str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U');
else
s[j++]=str[i];
    }
s[j]='\0';
printf("\nThe string without vowel is->%s",s);
getch();
}


Write a program to count the number of words in a sentence.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
inti,l,count=0;
charstr[80];
printf("Enter a line of text, not more than 80 characters\n");
gets(str);
l=strlen(str);
for (i=0;i<l;i++)
{
if(str[i]==' ')
{
count++;
}
else
continue;
}
printf("No. of words %d",count+1);
getch();
}


Write a program which will read a line and squeeze out all blanks from it and output the line with no blanks.

#include<stdio.h>
#include<conio.h>
void main()
{
char text[100], blank[100];
int c = 0, d = 0;

printf("Enter some text\n");
gets(text);

while (text[c] != '\0')
   {
if (!(text[c] == ' ' && text[c+1] == ' '))
        {
blank[d] = text[c];
d++;
      }
c++;
   }

blank[d] = '\0';

printf("\n\nText after removing blanks\n%s\n", blank);

getch();
}



Write a program which will read a line and delete from it all occurrences of the word "the".

#include <stdio.h>
#include <string.h>
void main()
{
int i, j = 0, k = 0, count = 0;
charstr[100], key[20];
char str1[10][20];
printf("enter string:");
scanf("%[^\n]s",str);
for (i = 0; str[i]!= '\0'; i++)/* Converts the string into 2D array */
    {
if (str[i]==' ')
        {
str1[k][j] = '\0';
k++;            j = 0;
        }
else
        {
str1[k][j] = str[i];
j++;
        }
    }
str1[k][j] = '\0';
printf("enter key:");
scanf("%s", key);
for (i = 0;i < k + 1; i++)/* Compares the string with given word */
    {
if (strcmp(str1[i], key) == 0)
        {
for (j = i; j < k + 1; j++)
strcpy(str1[j], str1[j + 1]);
k--;
        }    }
for (i = 0;i < k + 1; i++)
    {
printf("%s ", str1[i]);
    }
getch();
}



Write a program to count the number of occurrence of any two vowels in succession in a line of text. For example, in the following sentence:
"Please allow a studious girl to read behavioral science"
Such occurrences are ea, io, ou, ie.
Observe that in a word such as studious we have counted "io" and "ou" as two separate occurrences of two consecutive vowels.

#include<stdio.h>
#include<conio.h>
intisvowel(char chk);
void main()
{
char text[1000], chk;
intcount,a=0;
count = 0;
printf("Please enter a line of text.\n");
while( (text[count] = getchar()) != '\n' )
count++;
text[count] = '\0';
count = 0;
while ( (chk = text[count]) != '\0' )
  {
if ( isvowel(chk) )
            {
if ( (chk = text[++count]) &&isvowel(chk) )
                        {
putchar(text[count -1]);
putchar(text[count]);
putchar('\n');
a++;
                        }
            }
else
count++;

  }
printf("No. of vowel in succession=%d",a);
return 0;
}

intisvowel(char chk)
 {
if ( chk == 'a' || chk == 'e' || chk == 'i' || chk == 'o' || chk == 'u'||chk=='A'||chk=='E'||chk=='I'||chk=='O'||chk=='U' )
return 1;
return 0;
getch();
}


Define a structure Employee having data members name, address and salary. Take data from employee in an array dynamically and find the average salary.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct employee
{
char name[15];
char address[10];
float salary;
} ;
void main()
{
struct employee *e;
int n,i;
float sum=0.0, t,s;
printf("Enter how many records you want to store:\n");
scanf("%d",&n);
e=(struct employee*) malloc(n * sizeof (struct employee));
printf("\n\nEnter the records\nEnterName,Address and Salary resp.\n");
for(i=0;i<n;i++)
scanf("%s%s%f",&e[i].name,e[i].address,&e[i].salary);
printf("\nEntered records are:\n");
for(i=0;i<n;i++)
            {
printf("\nName:%s\nAddress:%s\n
Salary:%.2f\n\n",e[i].name,e[i].address,e[i].salary);
    t=t+e[i].salary;
            }
    s=t/n;
printf("\n\nAverage salary=%.2f",s);
getch();
}


Define a structure Coordinate having data members x and y for x and y coordinates. Take values for two points and find the distance between them. Use a function to calculate the distance but the result should be displayed in main function.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct coordinate
{
float a1,b1,a2,b2;
}x,y;
float distance(float a1,float b1,float a2,float b2)
{
float c;
c=(sqrt(((a2-a1)*(a2-a1))+((b2-b1)*(b2-b1))));
return c;
}
int main()
{
float d;
printf("Enter the co-ordinates of point A --->\n");
printf("x1= ");
scanf("%f",&x.a1);
printf("y1= ");
scanf("%f",&y.b1);
printf("Enter the co-ordinates of point B --->\n");
printf("x2= ");
scanf("%f",&x.a2);
printf("y2= ");
scanf("%f",&y.b2);
d=distance(x.a1,y.b1,x.a2,y.b2);
printf("The distance between A(%0.2f,%.2f) &b(%0.2f,%.2f) is ---> %0.3f",x.a1,y.b1,x.a2,y.b2,d);
getch();
return 0;
}


Define a structure student having data members name,class,section and marks for six subjects. Use array within structure to represent the marks in different subjects. Take the information for the n numbers of students given by user dynamically and display their percentage.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
char name[50],sec[10];
int clas;
float marks[6];
};
float percentage(float a)
{
return((a/6));
}
int main()
{
int i,j,n,cl;
char na[50],se[10];
float sum[100],per[100],p;
struct student *s;
printf("How many data do you have :");
scanf("%d",&n);
s=(struct employee*) malloc(n * sizeof (struct student));
for(i=0;i<n;i++)
{
printf("%d).Students Name -->",i+1);
scanf("%s",s[i].name);
printf("%d).Students class -->",i+1);
scanf("%d",&s[i].clas);
printf("%d).Students sec -->",i+1);
scanf("%s",s[i].sec);

sum[i]=0;
for(j=0;j<6;j++)
{
printf("Enter %dst subject marks -->",j+1);
scanf("%f",&s[i].marks[j]);
sum[i]+=s[i].marks[j];
}
per[i]=percentage(sum[i]);
printf("\n");

}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(per[i]>per[j])
{
p=per[i];
per[i]=per[j];
per[j]=p;

strcpy(na,s[i].name);
strcpy(s[i].name,s[j].name);
strcpy(s[j].name,na);

strcpy(se,s[i].sec);
strcpy(s[i].sec,s[j].sec);
strcpy(s[j].sec,se);

cl=s[i].clas;
s[i].clas=s[j].clas;
s[j].clas=cl;
}
}
}

printf("\n\n\n");
printf("\t\t\t\tFINAL REPORT\n\n");
printf("\t\t\tNAME\tCLASS\tSECTION\tPERCENTAGE\n");
for(i=0;i<n;i++)
{
printf("%d\t\t\t%s\t%d\t%s\t%.2f",i+1,s[i].name,s[i].clas,s[i].sec,per[i]);
printf("\n");
}
getch();
return 0;
}


Define a structure Table having data members length, breadth and height. Represent different measurements by another structure Measurement having data members meter and centimeter. Take data for some table and find their volume.

#include<conio.h>
#include<stdio.h>
struct table
{
floatlength,breadth,height;
}t;
struct measurement
{
floatmeter,centimeter;
}m;
void main()
{
int n;
printf("Length in meter:\n");
scanf("%f",&t.length);
printf("breadth in meter:\n");
scanf("%f",&t.breadth);
printf("height in meter:\n");
scanf("%f",&t.height);
m.meter=(t.length*t.breadth*t.height);
m.centimeter=((t.length*100)*(t.breadth*100)*(t.height*100));
printf("\nVolume in Meter:  %f",m.meter);
printf("\nVolume in Centimeter:  %.3f",m.centimeter);
getch();
return 0;
}


Define a structure Table for Vehicle Owner having data member name, address, telephone number, vehicle number and license number. Take the data for ten owners write them in file "Own.txt". Read the data from the file and display them.
#include<conio.h>
#include<stdio.h>
struct table
{
char name[100],address[100],vechine_number[10];
inttelephone_number,license_number;
};
int main()
{
int i;
char c;
table f[10];
FILE *fv,*fr;
fv=fopen("d:\\own.txt","w");
printf("\nDRIVER DETAIL FILL FORM\n");
fprintf(fv,"\t\tDRIVER DETAIL\n\n");
fprintf(fv,"S.N\tName\tAddress\tTelephone_NO.\tVechile_Number\tLicence_No.");
for(i=0;i<1;i++)
{
printf("%d). Driver Name ->",i+1);
scanf("%s",f[i].name);
printf("%d). Driver address ->",i+1);
scanf("%s",f[i].address);
printf("%d). Driver telephone no ->",i+1);
scanf("%d",&f[i].telephone_number);
printf("%d). Driver vechine number ->",i+1);
scanf("%s",f[i].vechine_number);
printf("%d). Driver license number ->",i+1);
scanf("%d",&f[i].license_number);

fprintf(fv,"\n%d\t%s\t%s\t%d\t\t%s\t\t%d",i+1,f[i].name,f[i].address,f[i].telephone_number,f[i].vechine_number,f[i].license_number);

}
fclose(fv);
fr=fopen("d:\\own.txt","r");
if(fv==NULL)
{
printf("File not found");
}
printf("\n");
while(c!=EOF)
{
c=fgetc(fr);
printf("%c",c);
}
fclose(fr);
getch();
return 0;
}



Given three variables x, y and z, write a function to circularly shift their values. In other words if x=5, y=9, and z=8, after circular shift y=5, z=9 and x=8. Call the function with variables a,b and c to circularly shift their values.

#include<stdio.h>
#include<conio.h>
void shift (inta,int b, int c);
void main()
{
Intx,y,z;
printf("Enter x:");
scanf("%d",&x);
printf("Enter y:");
scanf("%d",&y);
printf("Enter z:");
scanf("%d",&z);
shift (x,y,z);
getch();
}
void shift(intx,int y, int z)
{
Inta,b,c;
a=z;
b=x;
c=y;
printf("\nCirculated value is:\n\n");
printf("x=%d\ny=%d\nz=%d",a,b,c);
}


Write a function to solve a quadratic equation a2+bx+c=0. The input to be the function are the values a, b and c and the outputs of the function should be stored in variable names p and q appropriately declared.
#include<conio.h>
#include<stdio.h>
#include<math.h>
int fun(float a,floatb,float c)
{
floatp,q,d,e;
d=(-b/(2*a));
e=((b*b)-(4*a*c));
if(a==0)
{
printf("The roots are not available");

}
else if(e==0)
{
printf("Both roots are equal :(%.02f)",d);
}
else if(e>0)
{
p=d+(sqrt(e)/(2*a));
q=d-(sqrt(e)/(2*a));
printf("roots are real :(%.02f) ,(%.02f)",p,q);
}
else if(e<0)
{
e*=(-1);
p=(sqrt(e)/(2*a));
q=(sqrt(e)/(2*a));
printf("roots are imaginary :  %.02f+%.02fi & %.02f-%.02fi",d,p,d,q);
}
}
int main()
{
floata,b,c;
printf("Enter the value of a: \n ");
scanf("%f",&a);
printf("Enter the value of b:\n");
scanf("%f",&b);
printf("Enter the value of c:\n ");
scanf("%f",&c);
fun(a,b,c);
getch();
return 0;
}



Given a text file, create another text file deleting all the vowels (a, e, i, o, u).

#include<conio.h>
#include<stdio.h>
#include<string.h>
int main()
{
char c[100],str;
int a=0;
FILE *fr,*ft;
fr=fopen("D:\\file1.txt","w");
printf("Enter the text :");
scanf("%[^\n]",c);
fprintf(fr,"%s",c);
fclose(fr);
fr=fopen("D:\\file1.txt","r");
ft=fopen("D:\\file2.txt","w");
while(str!=EOF)
{
str=fgetc(fr);

if(str=='a'||str=='e'||str=='i'||str=='o'||str=='u'||str=='A'||str=='E'||str=='I'||str=='O'||str=='U');
else
{
fputc(str,ft);

}
}
fclose(ft);
fclose(fr);
getch();
return 0;
}


Explain the importance of pointer. Write a function that is passed an array of n pointers to floats and return a newly created array that contains those n float values.

Importance of Pointer:
i. Pointer are more efficient data array. They are used to manipulate arrays more easily by moving pointers to them instead of moving the arrays themselves.
ii. Pointer reduces the length and complexities of program.
iii. Pointer are used to return more than one values from a function.
iv. Pointer are used to create complex data structures such as link list, trees etc.
v. Pointer increases the execution speeds.
vi. Pointer are used to communicate information about memory which returns the location of free memory. Eg. Using like malloc(), calloc(),realloc(),free().

#include<stdio.h>
#include<conio.h>
void fun(float *a, int n);
int main()
{
Intn,i;
float *a;
printf("Enter length of an array\n");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for (i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
fun(a,n);
printf("\nThe elements are:\n");
for(i=0;i<n;i++)
{
printf("%.2f\n",a[i]);
}
getch();
}
void fun (float *a,int n)
{
int i;
float temp;
for(i=0;i<n;i++)
{
temp=a[n-1-i];
a[n-1-i]=a[i];
a[i]=temp;
}
}


Define a structure of student having data members, name, address, marks in C language and marks in information system. Take data for n students in an array dynamically and find the total marks obtained.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
char name[50],address[50];
float marksc,marksi;
};
int main()
{
int i,j,n,cl;
char na[50],se[10];
float sum[200];
struct student *s;
printf("How many data do you have :");
scanf("%d",&n);
s=(struct employee*) malloc(n * sizeof (struct student));
for(i=0;i<n;i++)
{
printf("%d).Students Name:",i+1);
scanf("%s",s[i].name);
printf("%d).Students address:",i+1);
scanf("%s",s[i].address);
sum[i]=0;
printf("Enter marks in C language:",j+1);
scanf("%f",&s[i].marksc);
printf("Enter marks in IT:",j+1);
scanf("%f",&s[i].marksi);
printf("\n");
sum[i]=s[i].marksc+s[i].marksi;
}


printf("\n\n\n");
printf("\t\t\t\tFINAL REPORT\n\n");
printf("S.N\tNAME\t\tADDRESS\t\t\tTOTAL\n");
for(i=0;i<n;i++)
{
printf("%d).\t%s\t\t%s\t\t%0.3f\n",i+1,s[i].name,s[i].address,sum[i]);

}
getch();
}




What are the uses of graphical function? Explain the basic graphical function with suitable program.


Ans: C graphics using graphics.h functions or WinBGIM(Windows 7) can be used to draw different shapes, display text in different fonts, change colors, and many more. Using functions of graphics.h in turbo c compiler you can make graphics programs, animations, projects and games. You can draw circles, lines, rectangles, bars and many other geometrical figures. You can change their colors using the available functions and fill them.
Following are some of the basic graphical functions.
i. initgraph();
This function initializes the graphics system. Prototype is defined in graphics.h. It is declared as:
void far initgraph(int far *gd, int far *gm, char far *pathtodrive);
wheregd is the graphics driver and gm is graphics mode.
ii. closegraph();
This function shuts down the graphics system. It is declared as:
void far closegraph (void);
iii. getmaxx(); and getmaxy();
The getmaxx() returns maximum x screen coordinate and getmaxy() returns maximum y screen coordinate. It is declared as:
int far getmaxx(void);
int far getmaxy(void);
iv. circle();
This function draws a circle at (x,y) of the given radius. It is declared as:
void far circle(int x, int y, int radius);
v. setcolor();
This function sets the current drawing color. It is declared as:
void far setcolor (int color);
vi. outtextxy();
It displays a string at the specified location (graphics mode). It is declared as:
void far outtextxy(int x, int y, char far *textstring);

/*Sample program to draw a circle*/
//circle (x coordinate, y coordinate, radius);
#include<graphics.h>
#include<conio.h>
void main()
{
intgd=DETECT,gm;
initgraph(&gd,&gm," ");
circle (150,150,100);
getch();
closegraph();
}


Write and test the following power () function that returns 'x' raised to the power 'n', where 'n' can be any integer:
double power(double x, int p)

#include<conio.h>
#include<stdio.h>
#include<math.h>
double power(double x,int p)
{
double f;
f=pow(x,p);
return f;
}
int main()
{
float x;
double q;
int n;
printf("Enter the value of x:");
scanf("%f",&x);
printf("Enter the power n:");
scanf("%d",&n);
q=power(x,n);
printf("%.2f",q);
getch();
    }


Justify that the pointer is jewel of C language. Write a function that is passed an array of 'n' pointers to float and returns a newly created array that contains those 'n' float values in reverse order. Assume any necessary data.

Ans: A Pointer is a variable that contains address of other variables i.e a variable that pointer to the location of data item. It provides a way of accessing a variable without referring to the variable directly. It uses the address of the variable . The pointer is called jewel of C language because the real power of C lies in the proper use of pointer.

Use of pointer:
1. Pointer are more efficient data array. They are used to manipulate arrays more easily by moving pointers to them instead of moving the arrays themselves.
2. Pointer reduces the length and complexities of program.
3. Pointer are used to return more than one values from a function.
4. Pointer are used to create complex data structures such as link list, trees etc.
5. Pointer increases the execution speeds.
6. Pointer are used to communicate information about memory which returns the location of free memory. Eg. Using like malloc(), calloc(),realloc(),free().

#include<stdio.h>
#include<conio.h>
void fun(float *a, int n);
void main()
{
Intn,i;
float *a;
printf("Enter length of an array\n");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for (i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
fun(a,n);
printf("\nThe reverse elements are:\n");
for(i=0;i<n;i++)
{
printf("%f\n",a[i]);
}
getch();
}
void fun (float *a,int n)
{
int i;
float temp;
for(i=0;i<n/2;i++)
{
temp=a[n-1-i];
a[n-1-i]=a[i];
a[i]=temp;
}
}





What is a pointer and explain its applications? Write a program that uses pointers to copy an array of double.

Ans:
A pointer is a derived data type in C. It is built from one of the fundamental data types available in C. Pointer contains memory addresses as their values. Since these memory addresses are the locations in the computer memory where program instructions and data are stored, pointer can be used to access and manipulate data stored in the memory.

Application:
1. Passing Parameter by reference.
Pointer can be used to stimulate passing parameter by reference. Pointer is used to pass parameter to function. In this scheme we are able to modify value at direct memory location..
void interchange(int*num1,int*num2)
{
int temp;
temp  =*num1;
*num1 =*num2;
*num2 =*num1;
}

2. Accessing Array Element:
intmain()
{
int a[5]={1,2,3,4,5};
int*ptr;
ptr= a;

for(i=0;i<5;i++){
printf("%d",*(ptr+i));
}

return(0);
}
We can access array using pointer. We can store base addresses of array in pointer.
ptr= a;
Now we can access each and individual location using pointer.
for(i=0;i<5;i++){
printf("%d",*(ptr+i));
}

3. Dynamic Memory Allocation:
struct student {
char name[10];
introllno;
};
We can use pointer to allocate memory dynamically. Malloc and calloc function is used to allocate memory dynamically.

4. Passing Parameter to the function:
struct student {
char name[10];
introllno;
};
Suppose we want to pass the above structure to the function then we can pass structure to the function using pointer in order to save memory. Suppose we pass actual structure then we need to allocate (10+4=14 Bytes (*)) of memory. If we pass pointer then we will require 4 Bytes (*) of memory.

5. Some of other applications:
1. Passing String to function.
2. Provides effective way of implementing the different data structures such as tree, graph, linked list.




#include<stdio.h>
#include<conio.h>
void fun();
void main()
{
fun();
getch();
}
void fun()
{
inti,n,*a;
printf("enter length of array:\n");
scanf("%d",&n);
printf("enter %d elements:\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("the order is:\n");
for(i=0;i<n;i++)
{
a[i]=2*a[i];
printf("%d\n",a[i]);
}
 }



Create a file info.txt that stores roll_no, name and marks of 50 students and display the student's info having marks greater than 75.

#include<conio.h>
#include<stdio.h>
struct file
{
      char name[50];
      introll_no;
      float marks;
};
void main()
{
      Inti,n,d=1;
      struct file f[50];
      FILE *fr;
      fr=fopen("d:\\file.txt","w");
      fprintf(fr,"\t\tDATA CATLOG\n\n");
      printf("\t\tDATA CATLOG\n\n");
      fprintf(fr,"S.N\tRoll-No.\tName\tMarks");
      printf("Enter how many student's name\n");
      scanf("%d",&n);
      for(i=0;i<n;i++)
      {
                      printf("%d).NAME -->",i+1);
                      scanf("%s",f[i].name);
                      printf("%d).Roll No. -->",i+1);
                      scanf("%d",&f[i].roll_no);
                      printf("%d).Marks -->",i+1);
                      scanf("%f",&f[i].marks);
                      fprintf(fr,"\n%d\t%s\t\t%d\t%f",i+1,f[i].name,f[i].roll_no,f[i].marks);
                      printf("\n");
      }
      printf("S.N\tRoll-No.\tName\t\tMarks");
      for(i=0;i<n;i++)
      {
                      if(f[i].marks>75)
                      {
                      printf("\n%d\t%d\t\t%s\t\t%f",d,f[i].roll_no,f[i].name,f[i].marks);
                      d++;
                      }
      }
      fclose(fr);
      getch();
}

No comments:

Post a Comment

Don't be shy. Leave your comment here.