CISC105 – General Computer Science
description
Transcript of CISC105 – General Computer Science
![Page 1: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/1.jpg)
CISC105 – General Computer Science
Class 2 – 6/7/2006
![Page 2: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/2.jpg)
User-Defined Identifiers (Variables)
Syntax Rules:1. An identifier can consist of only letters,
digits and underscores2. An identifier cannot begin with a digit3. A C reserved word cannot be used as an
identifier4. An identifier defined in a C standard
library should not be redefined (Advice – not a rule)
![Page 3: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/3.jpg)
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
![Page 4: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/4.jpg)
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!
![Page 5: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/5.jpg)
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’;
![Page 6: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/6.jpg)
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?
![Page 7: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/7.jpg)
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.
![Page 8: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/8.jpg)
General Form of a C program
![Page 9: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/9.jpg)
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
![Page 10: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/10.jpg)
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
![Page 11: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/11.jpg)
Arithmetic Expressions
• + addition operator
• - subtraction operator
• * multiplication operator
• / division operator
• % remainder operator (modulus)
![Page 12: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/12.jpg)
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
![Page 13: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/13.jpg)
Arithmetic Expressions
• Rules for evaluating expressions• Parentheses• Operator precedence
– Unary– *,/,%– +,-
• Associativity – Unary – right to left (right associativity)– Binary – left to right (left associativity)
![Page 14: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/14.jpg)
Arithmetic Expressions
![Page 15: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/15.jpg)
Arithmetic Expressions
![Page 16: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/16.jpg)
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.
ax
bym
![Page 17: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/17.jpg)
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.
![Page 18: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/18.jpg)
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)
![Page 19: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/19.jpg)
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
![Page 20: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/20.jpg)
A matter of Style
![Page 21: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/21.jpg)
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
![Page 22: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/22.jpg)
Program Controlled – I/O Files
![Page 23: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/23.jpg)
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
![Page 24: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/24.jpg)
Syntax Error
![Page 25: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/25.jpg)
Run-Time Error
![Page 26: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/26.jpg)
Undetected Error
![Page 27: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/27.jpg)
Error due to omission
![Page 28: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/28.jpg)
Functions
We have already seen some functions
• From stdio.h – printf and scanf
• From math.h – sqrt and others (see table 3.1)
![Page 29: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/29.jpg)
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!
![Page 30: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/30.jpg)
Functions
• Procedural Abstractions: main program only contains a list of sub-functions
![Page 31: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/31.jpg)
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.
![Page 32: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/32.jpg)
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
![Page 33: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/33.jpg)
House and Stick FigureWhat functions to write?
![Page 34: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/34.jpg)
Structure Chart of Functions
![Page 35: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/35.jpg)
Function Declaration and Main Program
![Page 36: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/36.jpg)
Function Definitions
![Page 37: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/37.jpg)
Function Use and Control Flow
![Page 38: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/38.jpg)
Functions with Arguments
• Input Arguments: arguments used to pass information into a function program
Actual Argument = 135.68
Formal Parameter is rnum
![Page 39: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/39.jpg)
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
![Page 40: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/40.jpg)
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
![Page 41: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/41.jpg)
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
![Page 42: CISC105 – General Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022062314/56814388550346895db005ec/html5/thumbnails/42.jpg)
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