Sie sind auf Seite 1von 13

Exercise 9: C Programming Dynamic memory allocation

Write a Program using pointers and dynamic memory allocation to perform the following.

1. Copy one array into another array in reverse order.

#include <stdio.h>
#include <stdlib.h>

void reverse_array(int*, int);

int main()
{
int n, c, *pointer;

printf("\nEnter the number of elements in the array :\n");

scanf("%d",&n);

pointer = (int*)malloc(sizeof(int)*n);

if( pointer == NULL )


exit(EXIT_FAILURE);
printf("\nEnter the elements of the array :\n");
for ( c = 0 ; c < n ; c++ )
scanf("%d",(pointer+c));

reverse_array(pointer, n);

printf("Original array on reversal is\n");

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


printf("%d\n",*(pointer+c));

free(pointer);

return 0;
}

void reverse_array(int *pointer, int n)


{
int *s, c, d;

s = (int*)malloc(sizeof(int)*n);

if( s == NULL )
exit(EXIT_FAILURE);

for ( c = n - 1, d = 0 ; c >= 0 ; c--, d++ )


*(s+d) = *(pointer+c);

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


*(pointer+c) = *(s+c);

free(s);
}

OUTPUT :
[eeeb84@sel-61 ~]$ gedit f1.c

[eeeb84@sel-61 ~]$ cc f1.c

[eeeb84@sel-61 ~]$ ./a.out


Enter the number of elements in the array :
5
Enter the elements of the array :
1
98
654
3233
111
Original array on reversal is
111
3233
654
98
1

2. Ascending order / Descending order

(A) Ascending Order

#include<stdio.h>

#include<stdlib.h>

void main()

int *a,n,i,j,t;
printf("\nEnter the number of integers to be sorted in ascending order: ");

scanf("%d",&n);

a=(int *)malloc(n *sizeof(int));

printf("\nEnter the %d numbers to be sorted in ascending order one by one: ",n);

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

scanf("%d", (a+i));

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

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

if(*(a+i)<*(a+j))

t=*(a+i);

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

*(a+j)=t;

printf("\nAfter sorting in ascending order: ");

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

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

OUTPUT:

[eeeb84@sel-61 ~]$ gedit f1.c

[eeeb84@sel-61 ~]$ cc f1.c

[eeeb84@sel-61 ~]$ ./a.out


Enter the number of integers to be sorted in ascending order: 5

Enter the 5 numbers to be sorted in ascending order one by one:

After sorting in ascending order:

(b) DESCENDING ORDER

#include<stdio.h>

#include<stdlib.h>

void main()

int *a,n,i,j,t;

printf("\nEnter the number of integers to be sorted in descending order: ");

scanf("%d",&n);

a=(int *)malloc(n *sizeof(int));

printf("\nEnter the %d numbers to be sorted in descending order one by one: ",n);

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

scanf("%d", (a+i));
}

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

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

if(*(a+i)>*(a+j))

t=*(a+i);

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

*(a+j)=t;

printf("\nAfter sorting in descending order: ");

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

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

OUTPUT :

[eeeb84@sel-61 ~]$ gedit f1.c

[eeeb84@sel-61 ~]$ cc f1.c

[eeeb84@sel-61 ~]$ ./a.out


Enter the number of integers to be sorted in descending order: 10

Enter the 10 numbers to be sorted in descending order one by one: 5

3
2

After sorting in descending order:

3. Sum of n numbers

#include <stdio.h>

#include <stdlib.h>

int main(){

int n,i,*ptr,sum=0;

printf("Enter number of elements: ");

scanf("%d",&n);

ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc

if(ptr==NULL)

{
printf("Error! memory not allocated.");

exit(0);

printf("Enter elements of array: ");

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

scanf("%d",ptr+i);

sum+=*(ptr+i);

printf("Sum=%d",sum);

free(ptr);

return 0;

OUTPUT:

[eeeb84@sel-61 ~]$ gedit f1.c

[eeeb84@sel-61 ~]$ cc f1.c

[eeeb84@sel-61 ~]$ ./a.out


Enter elements of array: 1

Sum=6

4. Addition of two matrices

#include<stdio.h>

#include<stdlib.h>

int main()

int i,j,p,q,r,s,*m1, *m2, *a;


printf("Enter the order of matrix A");

printf("\n");

printf("m=");

scanf("%d", &p);

printf("n=");

scanf("%d", &q);

printf("\nEnter the order of matrix B");

printf("\n");

printf("m=");

scanf("%d", &r);

printf("n=");

scanf("%d", &s);

if(p==r&&q==s)

m1=(int*)calloc(p*q, sizeof(int));

m2=(int*)calloc(r*s, sizeof(int));

a=(int*)calloc(p*s, sizeof(int));

printf("\nEnter the elements of matrix A:");

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

for(j=0;j<q;j++)

scanf("%d",(m1+i*q+j));

printf("\nEnter the elements of matrix B:");

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

{
for(j=0;j<s;j++)

scanf("%d",(m2+i*r+j));

printf("\nAddition:");

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

for(j=0;j<q;j++)

*(a+i*q+j)=*(m1+i*q+j)+(*(m2+i*r+j));

printf("\nSum of the Matrices A and B:");

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

printf("\n");

for(j=0;j<q;j++)

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

return 0;

OUTPUT:

[eeeb84@sel-61 ~]$ gedit f1.c

[eeeb84@sel-61 ~]$ cc f1.c


[eeeb84@sel-61 ~]$ ./a.out

Enter the order of matrix A

m=3

n=2

Enter the order of matrix B

m=3

n=2

Enter the elements of matrix A:

Enter the elements of matrix B:

10

12

Addition:

Sum of the Matrices A and B:

3 6

9 12

15 18
5. Dynamic memory allocation to develop a mark sheet of 5 students.

# include <string.h>

# include <stdio.h>

struct student

char name[10];

int m[3];

int total;

char result[5];

}*p,*s;

void main()

int i,j,l,n;

printf("Enter the no. of students : ");

scanf("%d",&n);

p=(struct student*)malloc(n*sizeof(struct student));

s=p;

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

printf("Enter a name : ");

scanf("%s",&p->name);

p-> total=0;l=0;

for(j=0;j<3;j++)

one:printf("Enter Marks of %d Subject : ",j+1);

scanf("%d",&p->m[j]);
if((p->m[j])>100)

printf("Wrong Value Entered");

goto one;

p->total+=p->m[j];

if(p->m[j]<40)

l=1;

if(l==0)

strcpy(p->result,"PASS");

else

strcpy(p->result,"FAIL");

p++;

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

printf("\n%s\t%s",s->name,s->result);

s++;

OUTPUT:

[eeeb84@sel-61 ~]$ gedit f1.c

[eeeb84@sel-61 ~]$ cc f1.c

[eeeb84@sel-61 ~]$ ./a.out

enter the no. of students : 5

enter a name : sakthi

enter the mark in 1 subject : 90


enter the mark in 2 subject : 80

enter the mark in 3 subject : 80

sakthi PASS

enter a name : sak

enter the mark in 1 subject : 90

enter the mark in 2 subject : 80

enter the mark in 3 subject : 70

sak PASS

enter a name : RAFE

enter the mark in 1 subject : 30

enter the mark in 2 subject : 60

enter the mark in 3 subject : 50

rafe FAIL

enter a name : Nathan

enter the mark in 1 subject : 90

enter the mark in 2 subject : 90

enter the mark in 3 subject : 90

nathan PASS

enter a name : vijay

enter the mark in 1 subject : 10

enter the mark in 2 subject : 10

enter the mark in 3 subject : 20

vijay FAIL

Das könnte Ihnen auch gefallen