{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lect24 - DS CS 11002 9 Computer Sc Engg IIT Kharagpur 6 1...

Info icon This preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1 & $ % User Defined Data: Product Constructor Lect 24 Goutam Biswas
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2 & $ % Built-in Data Types Built-in data types of C language are int, float, unsigned int, char, double etc. The representations and operations corresponding to these data types are decided by the language or by the compiler. Lect 24 Goutam Biswas
Image of page 2
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 3 & $ % Defined Types It is essential to define new types of data (object) and perform operations on them in programming related to different applications. A programming language cannot have everything as built-in, but it should provide facility ( constructor ) to define a new data type, declare an objects of the defined type, and facilities to support operations on them. Lect 24 Goutam Biswas
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 4 & $ % A complex Example A complex number (or its approximation) is not a built-in datatype of C language a . a Not really - C99 has a data type Complex, complex . See the man page of complex.h . Lect 24 Goutam Biswas
Image of page 4
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 5 & $ % C99 complex in GCC #include <stdio.h> #include <complex.h> int main() // gComplex1.c { complex double x, y ; x = 1.0 + 2.0i; // real + imaginary part // Operator overloading y = ~x; // complex conjugate printf("x: %lf+j%lf\n",creal(x),cimag(x)); printf("y: %lf - j%lf\n",creal(y),-cimag(y)); return 0; } Lect 24 Goutam Biswas
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 6 & $ % Note But we shall assume that the data type complex is not supported in C language and will define and support as a user defined data. Lect 24 Goutam Biswas
Image of page 6
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 7 & $ % Data in a Complex Number In school mathematics book a complex number z is written as z = a + jb , where a is the real part and b is the imaginary part . The value of j = - 1 . Lect 24 Goutam Biswas
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 8 & $ % Data in a Complex Number But the actual data in z may be viewed as an ordered pair of real numbers i.e. z = ( a, b ) , where the first component is the real part and the second component is the imaginary part . The collection of complex numbers is the Cartesian Product of reals i.e. C = R × R . Lect 24 Goutam Biswas
Image of page 8
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 9 & $ % Note Viewing C as R × R is not enough. The data type C is an algebraic structure equipped with a set of operations and relation e.g. addition , subtraction , equality etc. But right now we are mainly interested about the representation. Lect 24 Goutam Biswas
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 10 & $ % Approximation for Representations The first question is how to represent a complex number in a C program. We have already identified a complex number with a pair of reals. But a real number cannot have exact representation in a computer. It is approximated as a floating-point number ( float or double ). So a complex number may be approximated as an ordered pair of floating-point numbers .
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern