Real World Applications: Computing Resistance Problem –Given single-character codes for the colored bands that mark a resistor, compute its resistance.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Files in C Rohit Khokher.
The Preprocessor Underlying C Language Features Copyright © 2012 by Yong-Gu Lee
Structure of a C program
The C Preprocessor Yongjoon Lee April 22, What is it? Things with # –#include Processes the C source files BEFORE handing it to compiler. –`Pre`-process.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 10: Appendices.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Resistor Code How to read the value of a resistor.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 12 - The Preprocessor Directives (Macros)
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
Announcements Final NEXT WEEK (August 13 th Thursday at 16:00) Recitations will be held on August 12 th Wednesday We will solve sample final questions.
Real World Applications: Generating Prime Numbers  Problem Write a program that prints all positive prime integers less than or equal to n. A positive.
Week 2 - Friday.  What did we talk about last time?  Base systems  C literals  Representations in memory.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Engineering Computing I Chapter 4 Functions and Program Structure.
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
1 Preprocessor –How it works File Inclusion: #include Macro Definition: #define Predefined macros –__LINE__, __FILE__, __DATE__, __TIME__ Conditional Compilation.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i,
Introduction to Computer Programming
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout
Resistor Colour Code Why the Colour Code? The Colour code was developed to overcome two basic problems; Difficult to print and see numbers on a.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
C PREPROCESSOR. Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known.
Pre-Compiler Directives for TTCN-3 Ina Schieferdecker in response to CR5089 and 5090.
1 Building a program in C: Preprocessor, Compilation and Linkage.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Resistance. Resistor A resistor is part of an electric circuit that resists the flow of electric current. As current flows through a resistor, some of.
13 C Preprocessor.
Experiment #1 Measurement of Resistance
How to read the value of a resistor
What Is? function predefined, programmer-defined
Announcements Midterm2 Grades to be announced THIS WEEK
Announcements Final Exam will be on August 19 at 16:00
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Functions Separate Compilation
14. THE PREPROCESSOR.
Enum ,Char Functions& Math Library Functions I.Mona Alshehri
C Basics.
Pre-processor Directives
Introduction to Programming
Arrays & Functions Lesson xx
Preprocessor C program → Modified C program → Object Code
Functions Chapter 3 of the text Motivation:
Announcements Midterm2 Grades to be announced NEXT Monday
Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register,
C Preprocessor(CPP).
Preprocessor.
What Color is it?.
Announcements Final will be NEXT WEEK on August 17
C Preprocessor Seema Chandak.
Chapter 11: The Preprocessor
What Is? function predefined, programmer-defined
Electronics Resistance Practice Problems R LabRat Scientific © 2018.
Week 2 - Friday CS222.
Presentation transcript:

Real World Applications: Computing Resistance Problem –Given single-character codes for the colored bands that mark a resistor, compute its resistance in ohms. The color codes are as follows: Black 0, Brown 1, Red 2, Orange 3, Yellow 4, Green 5, Blue 6, 9 Violet 7,Gray 8, White 9. –If the integer codes of the bands are (in order) c1, c2, and c3, the resistance in ohms is R = (10*c1+c2)*10 c3

Sample Input/Output What is the resistance for a resistor with color mark ? The colored bands are coded as follows: COLOR CODE Black > B Brown > N Red > R Orange > O Yellow > Y Green > G Blue > E Violet > V Gray > A White > W Enter three codes. ERO Resistance in ohms:

Main Program #include void print_codes(void); double decode_char(char code); main() { char code1, code2, code3; double R; double c1, c2, c3; int flag; print_codes(); printf("\n\nEnter three codes. "); code1 = getchar(); code2 = getchar(); code3 = getchar(); c1 = decode_char(code1); c2 = decode_char(code2); c3 = decode_char(code3); if(c1 == || c2 == || c3 == ) printf("\n\n\tBad code - cannot compute R.\n"); else { R = (10.0*c1 + c2)*pow(10.0, c3); printf("\n\n\tResistance in ohms:\t%f\n", R); } return EXIT_SUCCESS; }

Print_codes function /* This function prints a menu of color codes to guide the user in entering input. */ void print_codes(void) { printf("\n\n\tThe colored bands" " are coded as follows:\n\n\t"); printf("COLOR\t\t\tCODE\n\t"); printf("-----\t\t\t----\n\n"); printf("\tBlack > B\n"); printf("\tBrown > N\n"); printf("\tRed > R\n"); printf("\tOrange > O\n"); printf("\tYellow > Y\n"); printf("\tGreen > G\n"); printf("\tBlue > E\n"); printf("\tViolet > V\n"); printf("\tGray > A\n"); printf("\tWhite > W\n"); }

Decode_char function /* This function expects a character (color code) and returns a double precision floating-point number as its value. If the code is not legal, it returns a value that signals that fact. */ double decode_char(char code) { switch (code) { case 'B': return 0.0; case 'N': return 1.0; case 'R': return 2.0; case 'O': return 3.0; case 'Y': return 4.0; case 'G': return 5.0; case 'E': return 6.0; case 'V': return 7.0; case 'A': return 8.0; case 'W': return 9.0; default: return ; }

The Preprocessor The C Preprocessor processes a C source file before the compiler translates the program into object code. The preprocessor follows the programmer's (preprocessing) directives. All preprocessor directives start with the character #.

The CC command When you issue the cc or gcc command, –The C preprocessor (known as cpp on our system) processes the C source file. Processing means including the files in #include, replacing macro definition by its replacement text, etc. –C compiler (known as cc1 on our system) compiles the resulting file into object file. –The loader (known as ld) finally uses the object file to produce executable program.

#include... The preprocessor directive #include includes a copy of this file at the point of this command. The file is located at "standard" locations. #include "mydefs.h" includes the file mydefs.h from the working directory.

#define... #define EOF (-1) associates EOF with (-1) : every occurrence of EOF is replaced by the text (-1) by the preprocessor. The defined identity EOF is called a macro. Use this form of macro if a constant is used frequently in a program. Macro definition can only be logically one line. Use backslash to continue a line. E.g., #define EOF \ (-1)

Some macro definitions in Math.h #define M_E #define M_PI #define M_SQRT In your program, instead of using numerical numbers, you can use the symbolic names for the constants. E.g. X = sin(M_PI/4); y = pow(M_E, x);

Parameterized Macros #define SQR(x) ((x)*(x)) After this definition, the preprocessor replaces every occurrence of the form SQR(a) by the replacement text ((a)*(a)) For example: w = SQR(x) + 1; z = SQR(w+1); becomes w = ((x)*(x)) + 1; z = ((w+1)*(w+1));

Putting as many parentheses as possible in macros #define SQR(x) x*x The preprocessor replaces every occurrence of the form SQR(a) by the replacement text a*a For example: w = SQR(x) + 1; z = SQR(w+1); becomes w = x*x + 1; z = w+1*w+1; This is 2w+1, not (w+1) 2 as intended.

Parameterized Macros #define PRINT3(e1,e2,e3) \ printf("\n%c\t%c\t%d", \ (e1), (e2), (e3) ) After this definition, the preprocessor replaces every occurrence of the form PRINT3(a,b,c) by the replacement text printf("\n%c\t%c\t%d", (a), (b), (c) )

Macros Versus Functions #define min(x,y) \ ( ( (x)<(y) )?(x):(y) ) After this definition, the preprocessor replaces every occurrence of the form min(a,b) with the replacement text ( ( (a)<(b) )?(a):(b) ) where a and b can be ANY text. Macros are not functions. There are no arguments passing, or return value to talk about.

Conditional, Miscellaneous #undef MC cancels the definition made to the symbol MC. #define MC makes MC defined (not to any particular replacement text). #ifdef MC #define ROWS 1000 #define COLS 1000 #endif defines ROWS and COLS only if MC is defined.

Conditional, Miscellaneous #ifndef MC #define ROWS 1000 #define COLS 1000 #endif defines ROWS and COLS only if MC is not defined. #if defined(MC) #define ROWS 10 #elif defined(MM) #define ROWS 20 #else #define ROWS 0 #endif

Reading/Home Working Read Chapter 5, page 172 to 190. Work on Problems –Section 5.5, page 176, exercise 1, 3. –Section 5.6, page 190, exercise 1, 3, 7, 9, 15. Check your answers in the back of the textbook. Do not hand in.