0% found this document useful (0 votes)
14 views3 pages

Programs

Uploaded by

Shibu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Programs

Uploaded by

Shibu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

STRUCTURE DYNAMIC ALLOCATION

Program: Program:
#include <stdio.h> #include<stdio.h>
#include <conio.h> #include<conio.h>
struct student #include<stdlib.h>
{ void main()
char name[50]; {
int roll; int n,i,*ptr, sum=0;
float marks; printf("Enter number of elements: ");
}s; scanf("%d",&n);
void main() ptr=(int*)malloc(n*sizeof(int)); //memory allocated
{ using mallocif(ptr NULL)
struct student *ptr; ptr = &s; {
printf("Enter student information:\n"); Programmin
printf("Enter student name: "); printf("Sorry! unable to allocate memory"); exit(0);
scanf("%s", &ptrname); }
printf("Enter roll number: "); printf("Enter elements of array: ");
scanf("%d", &ptr roll); for(i=0;i<n;++i)
printf("Enter marks: "); {
scanf("%f", &ptr→marks); scanf("%d",ptr+i);
printf("Displaying Information:\n"); sum+=*(ptr+i);
printf("Name: %s\n", ptr name); }
printf("Roll number: %d\n",ptr→roll); printf("Sum=%d",sum);
printf("Marks: %f\n", ptr→marks); getch(); free(ptr);
} getch();
}
Finding average of numbers stored in sequential Output:
access file Enter elements of array: 3
#include <stdio.h> Enter elements of array: 10
#include <math.h> 10
float average(FILE *input) 10
{ Sum-30
float term, sum;
int n; Program To Open, Write And Close A File
sum = 0.0; #include <stdio.h>
n = 0; #include <string.h>
while(!feof(input)) int main()
{ {
fscanf(input,"%f",&term); FILE *fp;
sumsum+ term; n=n+1; char data[50];
} printf( "Opening the file sample.c in write mode");
return sum/n; fp = fopen("sample.c", "w");
} if (fp=NULL)
int main () {
{ printf( "Could not open file sample.c"); return 1;
FILE *input; }
float avg; printf("\n Enter some text from keyboard");
input = fopen("data.txt","r"); {
avg = average(input); fputs(data, fp);
fclose(input); fputs("\n", fp);
printf("The average of the numbers is %f.\n",avg); }
return 0; printf("Closing the file sample.c");
} fclose(fp);
return 0;
}
One dimensional array Two dimensional array
Program: Calculate the average marks of the student Program: Find the addition of two matrix
#include<stdio.h #include<stdio.h>
#include<conio.h #include<conio.h>
void main() void main()
int m[5],,sum=0,n; {
float avg, printf("enter number of subject \n"); int a[25][25],b[25][25], c[25][25], i, j m, n;
scanf("%d",&n); clrscr();
printf("enter marks \n"); printf("\n Enter the rows and columns of two
for(i=0;i<n;i++) matrices... ");
{ scanf("%d %d", &m, &n)
scanf("%d",&m[i]); printf("\n Enter the elements of A matrix...");
} for(i=0;i<n;i++) for(i=0;i<m;i++)
sum-sum+m[i]; for(j=0;j<n;j++)
avg (float)sum/n: scanf("%d",&a[i][j]);
printf("average %f".avg); printf("\n Enter the elements of B matrix...");
getch() for(i=0;i<m;i++)
} for(j=0;j<n;j++)
Output: scanf("%d", &b[i][j]);
Enter number of subject for(i=0;i<m;i++) for(j=0;j<n;j++)
5 c[i][j]=a[i][j]+b[i][j];
Enter marks of students printf("\n The addition of two matrices");
55 for(i=0;i<m;i++)
60 }
78 printf("\n");
85 for(j=0;j<n;j++)
90 {
Average-73.6 printf("\t%d",c[i][j]);
}
}
getch();
}

You might also like