1 / 16

Today’s Agenda

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);

Download Presentation

Today’s Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Today’s Agenda Pointer to a Structure Arrays and Pointers Dynamic Memory Allocation

  2. complexDef.h #define MAX 10 typedef struct { double real; double img; }complex; typedef complex comArr[MAX];

  3. 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);

  4. ComplexOps.c • #include<stdio.h> • #include"complexDef.h"

  5. 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); }

  6. 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; }

  7. 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); • }

  8. ComplexOps.c (contd…) • Exercise • void divideComplex(complex *pcom1,complex *pcom2,complex *presult)

  9. 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++; } }

  10. 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++; } }

  11. 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); }

  12. 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

  13. 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

  14. Dynamic memory allocation • Syntax ptr = (cast_type *) malloc(no of bytes); • Example int *ptr; ptr = (int *)malloc(sizeof(int));

  15. More examples • Char *ptr; • ptr = (char *)malloc(sizeof(char) * 10); • Typedef struct student; • student *ptr; • ptr = (student *)malloc(sizeof(student));

More Related