Engr 0012 (04-1) LecNotes 20-01 Variable (data object) declaration in C two methods of variable declaration 154 a int 0065FDF4 Value-oriented int a; address.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Type Conversion. C provides two methods of changing the type of an expression: Type conversion (done implicitly) Cast expressions (done explicitly) Examples.
Spring Semester 2013 Lecture 5
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Chapter Five Functions
Call By Address Parameters (Pointers) Chapter 5. Functions that “return” more than a single value What if we need more than one value to be returned from.
Computer Programming w/ Eng. Applications
By Senem Kumova Metin 1 DATA TYPES. by Senem Kumova Metin 2 DATA TYPE? …… x; // DECLARATION OF VARIABLE X printf(“Do you want to go on? \n”) printf(“Please.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Structures Spring 2013Programming and Data Structure1.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes arrays collection of values - all of the same type, e.g., int individual values referred.
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
C-1 University of Washington Computer Programming I Lecture 3: Variables, Values, and Types © 2000 UW CSE.
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 3A Integral Data (Concepts)
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
CSC 107 – Programming For Science. The Week’s Goal.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes C++ errors/debugging build/compile compiler does not recognize a statement build/compile.
LOOP & Type Conversion. do – while Loop In the while loop, the test expression is evaluated at the beginning of the loop. If the test condition is false.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
Khalid Rasheed Shaikh Computer Programming Theory 1.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
1 ICS103 Programming in C Lecture 8: Functions I.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
CS1010: Programming Methodology
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes Contrasting MATLAB with C MATLABC language Workspace - interactive computation No real.
CSE / ENGR 142 Programming I
Chapter 6 - Functions modular programming general function format
Lesson #6 Modular Programming and Functions.
ECE Application Programming
Lesson #6 Modular Programming and Functions.
Functions, Part 2 of 2 Topics Functions That Return a Value
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
Revision Lecture
Multiple variables can be created in one declaration
Pointers.
Lesson #6 Modular Programming and Functions.
Engr 0012 (04-1) LecNotes
CS 2308 Exam I Review.
Engr 0012 (04-1) LecNotes
Engr 0012 (04-1) LecNotes
Variables in C Topics Naming Variables Declaring Variables
Conversion Check your class notes and given examples at class.
Lecture3.
Lesson #6 Modular Programming and Functions.
Fundamental Programming
Functions Extra Examples.
Variables in C Topics Naming Variables Declaring Variables
DATA TYPES There are four basic data types associated with variables:
Variables in C Topics Naming Variables Declaring Variables
FUNCTION ||.
Problem 1 Given n, calculate 2n
CPS125.
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Engr 0012 (04-1) LecNotes Variable (data object) declaration in C two methods of variable declaration 154 a int 0065FDF4 Value-oriented int a; address name type a ==> value (154) &a ==> address (0065FDF4) 0065FDF4 pa int address 0065FE4D Address-oriented int *pa; address name type pa ==> address (0065FDF4) &pa ==> address (0065FE4D) *pa ==> value (154)

Engr 0012 (04-1) LecNotes arithmetic and logical operators in C integer division whenever values on both sides of operator are integers otherwise, if one is floating point, promotion takes place

Engr 0012 (04-1) LecNotes integer division whenever values on both sides of operator are integers a = 38/3; b = 38%3; (= 2) long division is performed (= 12)

Engr 0012 (04-1) LecNotes promotion if values are of different type in an expression a = 38.0/3; subset type will be promoted to superset type before executing ==> a = 38.0/3.0; (==> …) can safely assign values of subset type to superset type // variable declaration int a = 3; double b; … // algorithm b = a; promoting type int value to type double

Engr 0012 (04-1) LecNotes demotion // variable declaration int a; double b = 3.2; … // algorithm a = b; demoting type double value to type int don’t do it - may lead to unexpected results

Engr 0012 (04-1) LecNotes trace main() { // begin main // variable declaration int a = 3, b = 3, c = 2, d = 7, e, f; double alfa = 2.0, beta = 5.0, gamma = 4.0, delta, epsilon; // algorithm delta = (a*b/c)*gamma; e = d%b; epsilon = (alfa*beta/gamma)*b; beta = (1/2)*beta; f = d/b; printf( "\ndelta = %10.2f \ne = %d \nepsilon = %.3f" "\nbeta = %f \nf = %8d \n\n", delta, e, epsilon, beta, f); } // end main

Engr 0012 (04-1) LecNotes functions prototype type name( parameter list ); double mycos( double angle ); void convert( double bearing, char *pface, double *pturn, char *pturndir ); int getint1( void ); void getint2( int *pvalue ); type of value sent back by return statement values required, addresses shared

Engr 0012 (04-1) LecNotes functions double mycos( double angle ); returns type double value through return statement requires type double value to do its job void convert( double bearing, char *pface, double *pturn, char *pturndir ); returns no values through return statement requires one type double value to do its job and shares three addresses with calling function

Engr 0012 (04-1) LecNotes functions int getint1( void ); returns type int value through return statement requires no values to do its job void getint2( int *pvalue ); returns no values through return statement shares one address with calling function Sharing an address means that both the function called and the calling function have access to the information at that address (and can change the information at the address)

Engr 0012 (04-1) LecNotes functions int getint1( void ) */ function purpose function needs function results */ { // begin getint1 // variable declaration int value; // algorithm printf( “Please enter an integer ==> ” ); scanf( “%d”, &value ): return( value ); } // end getint1 agreement between declared type and placeholder agreement between value-oriented declaration and address provided in scanf

Engr 0012 (04-1) LecNotes functions void getint2( int *pvalue ) */ function purpose function needs function results */ { // begin getint2 // algorithm printf( “Please enter an integer ==> ” ); scanf( “%d”, pvalue ): } // end getint2 agreement between declared type and placeholder agreement between address-oriented declaration and address provided in scanf

Engr 0012 (04-1) LecNotes functions functions can be called from any other function functions can even call themselves (recursion) rules on calling statements must have same number of parameters as prototype each parameter must be of same type value parameters require values values can be supplied in at least five ways values could be promotable but not demotable address parameters require addresses

Engr 0012 (04-1) LecNotes functions rules on calling statements, continued variable type is never used in calling statement a void parameter list is denoted by empty parentheses values sent back by the return statement may be captured with an assignment statement (otherwise, return statement values are lost)

Engr 0012 (04-1) LecNotes functions examples double mycos( double angle ); prototype accel = mycos( 3.2 ); call potential = mycos( 4.0*PI*current/3.0 ); position = mycos( 2 ); force = mycos( x ); velocity = mycos( *ptime ) values can be provided by: intensity = mycos( sin(x) ); providing an actual value an arithmetic expression an (proper) variable name a function call

Engr 0012 (04-1) LecNotes functions examples void convert( double bearing, char *pface, double *pturn, char *pturndir ); prototype convert( 333, &face, &turn, &direction ); call int getint1( void ); prototype choice = getint1( ); call void getint2( int *pvalue ); prototype getint2( &choice ); call

Engr 0012 (04-1) LecNotes trace int trace( int v1, int *pv2, int *pv2); main() { // begin main // variable declaration int a, b, c; // algorithm a = trace(1, &b, &c); printf( "\na = %d \nb = %d \nc = %d" a, b, c); } // end main //************************************** int trace(int v1, int *pv2, int *pv3) { // begin trace // algorithm printf(“Enter integer ==> ”); scanf( “%d”, pv2); *pv3 = v1*(*pv2); return(v1 + (*pv2) + (*pv3) ); } // end trace