Complex Number Module The code given here relies upon ANSI C’s ability to return a struct. Esakov and Weiss use the argument list to return a pointer to.

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Structures Often we want to be able to manipulate logical entities as a whole For example, complex numbers, dates, student records, etc Each of these must.
C Module System C and Data Structures Baojian Hua
Data Types C built-in data types –char, int, float, double, int*, etc. User-defined data types: the programmer can define his/her own data types which.
Week 5 - Wednesday.  What did we talk about last time?  Arrays.
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 CS 201 Exam 1 Review More on Makefiles Debzani Deb.
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004.
ADT Specification Example
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Guide To UNIX Using Linux Third Edition
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
CS201 – C Functions Procedural Abstraction Code Reuse C Library.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
C Tokens Identifiers Keywords Constants Operators Special symbols.
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
Introduction to C For the participants of Practical Course „Scientific Computing and Visualization“
(language, compilation and debugging) David 09/16/2011.
CS 261 – Recitation 2 Fall 2013 Oregon State University School of Electrical Engineering and Computer Science.
Data Display Debugger (DDD)
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
C code organization CSE 2451 Rong Shi. Topics C code organization Linking Header files Makefiles.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1 Structures. 2 What is a Structure? Used for handling a group of logically related data items  Examples: Student name, roll number, and marks Real part.
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
Structs & typedef. Structs 2 Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example:
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Programmer Defined Types and Classes
Function: Declaration
C Primer.
A bit of C programming Lecture 3 Uli Raich.
CPS 393 Introduction to Unix and C
Functions Separate Compilation
Day 04 Introduction to C.
Structs & typedef Already seen in tirgul.
Computer Systems and Networks
Instructor: Ioannis A. Vetsikas
typedef typedef int Index; typedef char Letter; Index i; i = 17;
مبانی کامپیوتر و برنامه سازی
By: Syed Shahrukh Haider
Structures.
מ- C++ ל- C קרן כליף.
Built-In (a.k.a. Native) Types in C++
Introduction to C Topics Compilation Using the gcc Compiler
Sudeshna Sarkar 7th March 2017
References Revisited CSE 333 Summer 2018
Introduction to C Topics Compilation Using the gcc Compiler
Programming Language C Language.
Functions Lecture 5.
Introduction to Computer Organization & Systems
Programming in C Pointers and Arrays.
References Revisited CSE 333 Autumn 2018
Module 2 Variables, Data Types and Arithmetic
Compiler vs linker The compiler translates one .c file into a .o file
15213 C Primer 17 September 2002.
Structs & typedef Already seen in tirgul.
Module 13 Dynamic Memory.
Week 5 - Wednesday CS222.
Presentation transcript:

Complex Number Module The code given here relies upon ANSI C’s ability to return a struct. Esakov and Weiss use the argument list to return a pointer to a struct. This is how K & R C would do it. A programmer can choose either strategy. But, you should fully understand what the memory model is doing for both strategies. Can you see the overhead involved in passing and returning entire structs? Can you see that the C Compiler needs to create space when it returns a struct.

main.c globals.hcomplex.h complex.c bash> gcc -o complex -ansi -Wall main.c complex.c

/**********************************************************/ /* globals.h */ /**********************************************************/ #ifndef _globals #define _globals typedef enum{ FALSE = 0, TRUE = 1 } bool ; #endif

/***************************************************************************/ /* Programmer: Jim Canning */ /* */ /* Main driver to test complex number module. */ /***************************************************************************/ #include #include "complex.h" int main( int argc, char *argv[] ) { complex complex1, complex2, complex3 ; double r, i ; printf( "Complex 1: Please enter two floating point values: " ) ; scanf("%lf", &r) ; scanf("%lf", &i ) ; complex1 = load_complex( r, i ) ; printf("\nThe complex number is " ); print_complex( complex1 ) ; printf("\n"); printf( "Complex 2: Please enter two floating point values: " ) ; scanf("%lf", &r) ; scanf("%lf", &i ) ; complex2 = load_complex( r, i ) ; printf("\nThe complex number is " ); print_complex( complex2 ) ; printf("\n");

complex3 = add_complex( complex1, complex2 ) ; printf("The addition of the two complex numbers is "); print_complex( complex3 ) ; printf("\n") ; complex3 = subtract_complex( complex1, complex2 ) ; printf("The subtraction of the two complex numbers is "); print_complex( complex3 ) ; printf("\n") ; complex3 = multiply_complex( complex1, complex2 ) ; printf( "The multiplication of the two complex numbers is " ); print_complex( complex3 ) ; printf("\n") ; complex3 = divide_complex(complex1, complex2 ) ; printf( "The division of the two complex numbers is " ); print_complex( complex3 ) ; printf("\n") ; if ( equal_complex( complex1, complex2 ) ) printf("\nThey are two equal complex numbers.\n"); else printf("\nThe numbers are not equal.\n") ; return 0 ; }

/****************************************************************************/ /* complex.h */ /****************************************************************************/ #ifndef _complex #define _complex #include "globals.h" typedef struct complex{ double real ; double imaginary ; } complex ; extern complex load_complex( double real, double imaginary ) ; extern void retrieve( complex complex1, double *p_real, double *p_imaginary ); extern complex add_complex( complex complex1, complex complex2 ) ; extern complex multiply_complex( complex complex1, complex complex2 ) ; extern complex subtract_complex( complex complex1, complex complex2 ) ; extern bool equal_complex( complex complex1, complex complex2 ) ; extern complex divide_complex( complex complex1, complex complex2 ) ; extern void print_complex( complex complex1 ) ; #endif

/*****************************************************************************/ /* Programmer: Jim Canning */ /* complex.c */ /*****************************************************************************/ #include "globals.h" #include "complex.h” #include extern complex load_complex( double real, double imaginary ) { complex a ; a.real = real ; a.imaginary = imaginary ; return ( a ) ; } extern void retrieve( complex complex1, double *p_real, double *p_imaginary ){ *p_real = complex1.real ; *p_imaginary = complex1.imaginary ; return ; }

extern complex add_complex( complex complex1, complex complex2 ) { complex a ; a.real = complex1.real + complex2.real ; a.imaginary = complex1.imaginary + complex2.imaginary ; return ( a ) ; } extern complex multiply_complex( complex complex1, complex complex2 ) { complex a ; a.real = ( complex1.real * complex2.real ) - ( complex1.imaginary * complex2.imaginary ) ; a.imaginary = ( complex1.real * complex2.imaginary ) + ( complex1.imaginary * complex2.real ) ; return a ; }

extern void print_complex( complex complex1 ) { printf("%f ", complex1.real) ; printf("%f ", complex1.imaginary ) ; return ; }

extern bool equal_complex( complex complex1, complex complex2 ) { return (( complex1.real == complex2.real ) && ( complex1.imaginary == complex2.imaginary )) ; } extern complex divide_complex( complex complex1, complex complex2 ) { complex a ; a.real = (( complex1.real * complex2.real ) + ( complex1.imaginary * complex2.imaginary ) ) / ( ( complex2.real * complex2.real ) + ( complex2.imaginary * complex2.imaginary )) ; a.imaginary = (( complex1.imaginary * complex2.real ) - ( complex1.real * complex2.imaginary ) ) / ( ( complex2.real * complex2.real ) + ( complex2.imaginary * complex2.imaginary )) ; return ( a ) ; }

main.c globals.hcomplex.h complex.c bash> make Makefile

# # This is a Makefile for the complex number # exercise. # complex: main.o complex.o gcc -o complex main.o complex.o main.o: main.c complex.h gcc -ansi -Wall -c main.c complex.o: complex.c complex.h gcc -ansi -Wall -c complex.c clean: rm -f *.o complex

man make Learning about make Google make