1 / 42

CISC105 – General Computer Science

CISC105 – General Computer Science. Class 2 – 6/7/2006. User-Defined Identifiers (Variables). Syntax Rules: An identifier can consist of only letters, digits and underscores An identifier cannot begin with a digit A C reserved word cannot be used as an identifier

brooklyn
Download Presentation

CISC105 – General Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CISC105 – General Computer Science Class 2 – 6/7/2006

  2. User-Defined Identifiers (Variables) Syntax Rules: • An identifier can consist of only letters, digits and underscores • An identifier cannot begin with a digit • A C reserved word cannot be used as an identifier • An identifier defined in a C standard library should not be redefined (Advice – not a rule)

  3. User-Defined Identifiers • User-Defined Identifiers are case sensitive • Hello != hello != HELLO • Accepted rule: • Constants use uppercase KMS_PER_MILE • Other identifiers in lowercase miles, kms • Separate words by using an underscore per_capita_income

  4. Data Types • int : whole numbers in the range -32767 to 32767 – Why is this limited? • double : contains and integral and fractional part • Larger values use Scientific Notation • 1.25E5 = 1.25e5 = 1.25 x 105 • Still have size limitation – all real numbers cannot be represented!

  5. Data Types • char : Represents an individual character – a letter, a digit or symbol • Represented by single quotes ‘1’, ‘a’, ‘A’, ‘ ‘ • Do not enter a char as ‘z’ at a prompt • char first_initial = ‘m’; • You can compare and add char data types but use with care! • char letter = ‘A’ + 32; Will return ‘a’;

  6. Executable Statements - scanf • scanf is an input function call that usually follows a printf prompt • scanf(“%lf”, &miles) • & is the C address-of operator and tells scanf to where to find the variable to store the information into • Using scanf for multiple input? Write scanf to input Bob?

  7. Executable Statements – return(0) • The return(0) statement returns control from the program back to the OS. The return value of 0 indicates an error free execution.

  8. General Form of a C program

  9. Program Style • Do not write more than one command per line. • Be consistent in use of spaces • /*This is a comment*/ • /* This is a comment */ • Use program documentation to enhance the readability of your program

  10. Program Style • Every program should start with comments telling the reader: • The programmers name • Class and assignment info if appilicable • Date of the current version • Brief description of what the program does

  11. Arithmetic Expressions • + addition operator • - subtraction operator • * multiplication operator • / division operator • % remainder operator (modulus)

  12. Arithmetic Expressions • Mixed-type expressions: int + double = double • Type cast – convert an expression to a different type by writing the desired type in paranthesis • Unary operator (+, -) – requires one operand • Binary operator – requires 2 operands

  13. Arithmetic Expressions • Rules for evaluating expressions • Parentheses • Operator precedence • Unary • *,/,% • +,- • Associativity • Unary – right to left (right associativity) • Binary – left to right (left associativity)

  14. Arithmetic Expressions

  15. Arithmetic Expressions

  16. Writing Formulas in C • Multiplication can be implied in a formula by writing the 2 items together (a=bc) • C requires the use of * operator a = b * c; • Writing a formula for division we usually write the numerator and denominator on a separate line • C requires numerator and denominator on the same line m = (y – b) / (x – a); Note: use parentheses to clarify the numerator and denominator.

  17. Formatting Numbers in Output • Formatting int is easy! Just add the number of characters you wish to display before the ‘d’ • printf(“The value is: %3d.\n”, this_value); • Displays this_value with a minimum of 3 characters if this_value is larger than 3 characters it will print the entire value.

  18. Formatting Numbers in Output • Formatting a double variable in output is done by indicating the total field width and the number of decimal places desired in the format %n.mf (n can be zero) • printf(“The value is : %6.2f\n”, this_value); • If this_value was 120.567 the output would be 120.57 (rounding the decimal)

  19. Interactive Mode vs. Batch Mode • Interactive Mode – a mode of execution where the user responds to prompts by entering data • Batch Mode – a mode of execution where the program scans data from a previously prepared data file • Input Redirection is achived in Unix / MS-DOS by placing <filename at the end of the command line

  20. A matter of Style

  21. Output Redirection • You can redirect output to file by using >filename • You can use input and output redirection on the same command line: • myprog <mydata >myoutput

  22. Program Controlled – I/O Files

  23. Common Programming Errors • Syntax Errors: Violation of C grammar rules – detected by compiler as it attempts to translate your program • Run-Time Errors: Detected and displayed by computer during execution of program • Undetected Errors: An execution error that does not prevent successful run but can lead to incorrect results. • Logic Errors: Program follows a faulty algorithm

  24. Syntax Error

  25. Run-Time Error

  26. Undetected Error

  27. Error due to omission

  28. Functions We have already seen some functions • From stdio.h – printf and scanf • From math.h – sqrt and others (see table 3.1)

  29. Functions Why use functions? • Need to do a computation several times in a program • Way to modularize a program • Big problem -> smaller problems • Top-down Design • Code Reuse • Use previous program to solve a bigger/slightly different problem • Why reinvent the wheel – sqrt would be difficult to write every time you need it – just use the math.h library!

  30. Functions • Procedural Abstractions: main program only contains a list of sub-functions

  31. Aspects of a Function • Declarations • tells the name of the function • what inputs are required • the type of output to expect Note: Declarations are usually put in a .h file, but .h files are more advanced so we will have the declaration in same file as the main program.

  32. Aspects of a Function • Definition • Defines what the function does • Contains the actual code for whatever task the function needs to program • Use • When a function is used in another place in a program • The same function can be used several times

  33. House and Stick FigureWhat functions to write?

  34. Structure Chart of Functions

  35. Function Declaration and Main Program

  36. Function Definitions

  37. Function Use and Control Flow

  38. Functions with Arguments • Input Arguments: arguments used to pass information into a function program Actual Argument = 135.68 Formal Parameter is rnum

  39. Functions with Arguements • Output Argument: arguments used to return results to the calling function What would be returned by the following?double result = scale(25.0, 4); Answer: 250,000

  40. Function Notes • Variables are local • Variables are not shared between functions • Variable names can be reused in functions • Do not point to same memory space • Should only be used when referencing the same information • Necessary variables must be passed a arguments • Constant Variables are Global • Values of constants are unique and can be accessed from all functions in the program

  41. Function Notes • Make sure you comment preconditions and postconditions • Precondition: condition assumed to be true prior to a function call • N and m are defined, math.h is included • Postcondition: condition assumed to be true after a function call

  42. Function Notes • Number of arguments in a function call must be equal to the formal parameters in the function definition! • Order of Arguments in the function call must correspond to the order required in the formal parameters in the function definition • Each argument in the function call must be of the same data type as the formal parameter in the function definition

More Related