programming for engineers: data and...
TRANSCRIPT
A Sample ProgramData Types
ACLU #3Programming Assignment
Programming for Engineers:Data and C
19 January 200921 January 2009
Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
rhodium.c
/* rhodium.c – your weight in rhodium */#include <stdio.h>#include <conio.h>
int main(void){
float weight; /* user weight */float value; /* rhodium equivalent */
printf(“Are you worth your weight in rhodium?∖n”);printf(“Let’s check it out.∖n”);printf(“Please enter your weight in pounds: ”);
Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
rhodium.c
/* rhodium.c – your weight in rhodium */#include <stdio.h>#include <conio.h>
int main(void){
float weight; /* user weight */float value; /* rhodium equivalent */
printf(“Are you worth your weight in rhodium?∖n”);printf(“Let’s check it out.∖n”);printf(“Please enter your weight in pounds: ”);
Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
rhodium.c (cont’d)
/* get input from the user */scanf(“%f”, &weight);/* assume rhodium is $7010 per ounce *//* 14.5833 converts pounds avd. to ounces troy */value = 7010.0 * weight * 14.5833;printf(“Your weight in rhodium is worth $%.2f.∖n”, value);printf(“You are easily worth that! If rhodium prices drop,∖n”);printf(“eat more to maintain your value.∖n”);
printf(”∖n∖nPress any key to continue...”);while(!kbhit());
return 0;} Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
rhodium.c (cont’d)
/* get input from the user */scanf(“%f”, &weight);/* assume rhodium is $7010 per ounce *//* 14.5833 converts pounds avd. to ounces troy */value = 7010.0 * weight * 14.5833;printf(“Your weight in rhodium is worth $%.2f.∖n”, value);printf(“You are easily worth that! If rhodium prices drop,∖n”);printf(“eat more to maintain your value.∖n”);
printf(”∖n∖nPress any key to continue...”);while(!kbhit());
return 0;} Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
Integer Typeschar Typefloat TypesBool TypeComplex and Imaginary Types
Data Types
Data Type Bytes Minimum Maximum C Versionint 4 −231 ≈ −2× 109 231 − 1 ≈ 2× 109 K&Rlong 4 −231 231 − 1 K&Rshort 2 −215 ≈ 32, 000 215 − 1 ≈ 32, 000 K&Runsigned 4 0 232 K&Rchar 1 −27 = 128 27 − 1 = 127 K&Rfloat 4 1.175× 10−38 1.175× 1037 K&Rdouble 8 2.225× 10−308 22.225× 10307 K&Rsigned 4 −231 231 − 1 K&Rvoid N. A. C90Bool N.A. C99Complex N.A. C99Imaginary N. A. C99
Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
Integer Typeschar Typefloat TypesBool TypeComplex and Imaginary Types
Integer vs. Floating Point
▶ Integer Type (char)0 0 0 0 0 1 1 1
0×27+0×26+0×25+0×24+0×23+1×22+1×21+1×20
= 7
▶ Floating Point0 1000 0000 1.100 1001 0000 1111 1101 0000
(−1)0 ×(1× 20 + 1× 2−1 + ⋅ ⋅ ⋅+ 0× 2−23
)×(2128−127
)= +3.14159
Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
Integer Typeschar Typefloat TypesBool TypeComplex and Imaginary Types
int Type
▶ Declarationint number;
▶ Initializationnumber=1527; //decimal constantnumber=02767; //octal constantnumber=0x5F7; //hexadecimal constant
▶ Displayprintf(“%d”,number); //decimalprintf(“%o”,number); //octalprintf(“%x”,number); //hexadecimal
Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
Integer Typeschar Typefloat TypesBool TypeComplex and Imaginary Types
Other int Types
▶ Short int▶ Short number
printf(“%hd”,number);▶ Unsigned short number
printf(“%hu”,number);▶ Signed short number
printf(“%hd”,number);
▶ Long int▶ Long number
printf(“%ld”,number);▶ Unsigned long number
printf(“%lu”,number);▶ Signed long number
printf(“%ld”,number);
Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
Integer Typeschar Typefloat TypesBool TypeComplex and Imaginary Types
Other int Types (cont’d)
▶ Long Long int
▶ Long long numberprintf(“%lld”,number);
▶ Unsigned long long numberprintf(“%llu”,number);
▶ Signed long long numberprintf(“%lld”,number);
Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
Integer Typeschar Typefloat TypesBool TypeComplex and Imaginary Types
Integer Mathematics
▶ Neither integer underflow nor integer overflow is detected orsignaled.
▶ Integer division discards the remainder. For example, 94 = 2
instead of 94 = 2.25.
Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
Integer Typeschar Typefloat TypesBool TypeComplex and Imaginary Types
char Type
▶ Declarationchar ch;
▶ Initializationchar=’A’; //good stylechar=65; //poor stylechar=0x41; //poor stylechar=0101; //poor style
▶ Readscanf(“%c”,&ch);
▶ Displayprintf(“%c”,ch);
Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
Integer Typeschar Typefloat TypesBool TypeComplex and Imaginary Types
Other Specifics for Integer Types
▶ Octal▶ Symbol are 0 to 7▶ Must start with zero▶ Example: 0732 = 7× 82 + 3× 81 + 2× 80 = 47410
▶ Hexadecimal▶ Symbol are 0 to 9, A to F , or a to f▶ Must start with 0x or 0X▶ Example: 0x732 = 7× 162 + 3× 161 + 2× 160 = 184210
Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
Integer Typeschar Typefloat TypesBool TypeComplex and Imaginary Types
Escape Sequences
Sequece Meaning∖a Alert or bell∖b Backspace∖f Form feed∖n New line∖r Carriage return∖t Horizontal tab∖v Verticle tab∖∖ Backslash∖’ Single quote∖” Double quote∖? Question mark∖Ooo Octal number, with o an octal digit of 0 to 7∖xhh Hexadecimal number, with h a hexadecimal digit 0 to F
Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
Integer Typeschar Typefloat TypesBool TypeComplex and Imaginary Types
float Types
▶ Declarationfloat number;
▶ Initializationnumber=3.14159;number = 0.314159e+1;
▶ Displayprintf(“%f”,number);//decimal fractionprintf(“%e”,number);//exp. notationprintf(“%Lf”,number);//long double ver.printf(“%Le”,number);//long double ver.
Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
Integer Typeschar Typefloat TypesBool TypeComplex and Imaginary Types
Special Floating Point Cases
▶ Overflows to +INF
▶ Underflow is soft to 0.000000e+00
▶ Domain errors to +NAN
Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
Integer Typeschar Typefloat TypesBool TypeComplex and Imaginary Types
Float vs. Double
▶ float0 1000 0000 1.100 1001 0000 1111 1101 0000
(−1)0 ×(1× 20 + 1× 2−1 + ⋅ ⋅ ⋅+ 0× 2−23
)×(2128−127
)= +3.14159
▶ double float0 100 0000 0000 1.100 1001 0000 1111 1101 0
1111 0000 0001 1011 1000 0110 0110 1110
(−1)0 ×(1× 20 + 1× 2−1 + ⋅ ⋅ ⋅
)×(21024−1023
)= +3.14159
Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
Integer Typeschar Typefloat TypesBool TypeComplex and Imaginary Types
Bool Type
▶ Bool type is a C99 addition
▶ Bool type is not supported in Turbo C++▶ Values
▶ true is represented by a 1.▶ false is represented by a 0.
Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
Integer Typeschar Typefloat TypesBool TypeComplex and Imaginary Types
Complex and Imaginary Types
▶ C99 supports complex and imaginary types▶ float complex▶ double complex▶ long double complex
▶ Turbo C++ does not support these types directly
▶ The imaginary type is similar to the complex type.
Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
ACLU #3
Programming for Engineers: Data and C
A Sample ProgramData Types
ACLU #3Programming Assignment
Programming Assignment
Complete Chapter 3 Exercise 2Complete Chapter 3 Exercise 4Complete Chapter 3 Exercise 6
Due Date: 24 February 2011
Programming for Engineers: Data and C