160 likes | 231 Views
Today’s Agenda. Pointer to a Structure Arrays and Pointers Dynamic Memory Allocation. complexDef.h. #define MAX 10 typedef struct { double real; double img; }complex; typedef complex comArr[MAX];. complexOps.h. #include"complexDef.h" extern void readComplexNo(complex *pcom);
E N D
Today’s Agenda Pointer to a Structure Arrays and Pointers Dynamic Memory Allocation
complexDef.h #define MAX 10 typedef struct { double real; double img; }complex; typedef complex comArr[MAX];
complexOps.h • #include"complexDef.h" • extern void readComplexNo(complex *pcom); • extern void printComplexNo(complex *pcom); • extern complex addComplex(complex *pcom1,complex *pcom2); • extern void mulComplex(complex *pcom1,complex *pcom2,complex *presult); • extern void divideComplex(complex *pcom1,complex *pcom2,complex *presult); • extern void readComplexArr(complex *pcom,int size); • extern void printComplexArr(complex *pcom,int size);
ComplexOps.c • #include<stdio.h> • #include"complexDef.h"
ComplexOps.c (contd…) void readComplexNo(complex *pcom) { printf("Enter real and img values for complex no\n"); scanf("%lf %lf",&pcom->real,&pcom->img); } void printComplexNo(complex *pcom) { printf("Complex No: real part = %lf img part = %lf\n",pcom->real,pcom->img); }
ComplexOps.c (contd…) complex addComplex(complex *pcom1,complex *pcom2) { complex c; c.real = pcom1->real + pcom2->real; c.img = pcom1->img + pcom2->img; printComplexNo(&c); return c; }
ComplexOps.c (contd…) • void mulComplex(complex *pcom1,complex *pcom2,complex *presult) • { • presult->real = (pcom1->real * pcom2->real) • - (pcom1->img * pcom2->img); • presult->img = (pcom1->img * pcom2->real) • + (pcom1->real * pcom2->img); • printComplexNo(presult); • }
ComplexOps.c (contd…) • Exercise • void divideComplex(complex *pcom1,complex *pcom2,complex *presult)
ComplexOps.c (contd…) void readComplexArr(complex *pcom,int size) { int i; printf("Enter %d complex numbers\n",size); for(i=0;i<size;i++) { scanf("%lf %lf",&pcom->real,&pcom->img); pcom++; } }
ComplexOps.c (contd…) void printComplexArr(complex *pcom,int size) { int i; for(i=0;i<size;i++) { printf("real part = %lf\timg part = %lf\n", pcom->real,pcom->img); pcom++; } }
main.c #include<stdio.h> #include"complexOps.h" int main() { complex c1,c2,c3,c4,c5; comArr ca; readComplexNo(&c1); printComplexNo(&c1); readComplexNo(&c2); printComplexNo(&c2); c3 = addComplex(&c1,&c2); printComplexNo(&c3); mulComplex(&c1,&c2,&c4); printComplexNo(&c4); divideComplex(&c1,&c2,&c5); printComplexNo(&c5); readComplexArr(ca,3); printComplexArr(ca,3); }
Output Enter real and img values for complex no 8.9 2.6 Complex No: real part = 8.900000 img part = 2.600000 Enter real and img values for complex no 7.8 4.6 Complex No: real part = 7.800000 img part = 4.600000 Complex No: real part = 16.700000 img part = 7.200000 Complex No: real part = 16.700000 img part = 7.200000 Complex No: real part = 57.460000 img part = 61.220000 Complex No: real part = 57.460000 img part = 61.220000 Complex No: real part = 0.992439 img part = -0.251951 Enter 3 complex numbers 2.3 4.5 4.5 6.7 5.6 7.8 real part = 2.300000 img part = 4.500000 real part = 4.500000 img part = 6.700000 real part = 5.600000 img part = 7.800000
Exercise • Write a program to generate a student list. • Operation • Read a list • Print a list • Count number of students whose cgpa is less than 6.5 • Sort list in decreasing order based on their cgpa (higher cgpa should come first) • Merge two student lists based on there cgpa
Dynamic memory allocation • Syntax ptr = (cast_type *) malloc(no of bytes); • Example int *ptr; ptr = (int *)malloc(sizeof(int));
More examples • Char *ptr; • ptr = (char *)malloc(sizeof(char) * 10); • Typedef struct student; • student *ptr; • ptr = (student *)malloc(sizeof(student));