CECS 121 REACH Final Test Review - PowerPoint PPT Presentation

1 / 73
About This Presentation
Title:

CECS 121 REACH Final Test Review

Description:

Kelly 11/12/86 6 Louisville Allen 04/05/77 49 Atlanta Chelsea 03/30/90 12 Charleston Can you write a program that prints out the contents of this information.dat ... – PowerPoint PPT presentation

Number of Views:134
Avg rating:3.0/5.0
Slides: 74
Provided by: CRC67
Category:

less

Transcript and Presenter's Notes

Title: CECS 121 REACH Final Test Review


1
CECS 121 REACH Final Test Review
2
Test Review Topics
  • Old Material
  • escape sequences
  • operator precedence
  • printf()
  • scanf()
  • if()
  • switch case
  • ,
  • while, do-while, , --
  • functions

New Material 1-D arrays string functions fopen() f
close() fscanf() fprintf()
3
Escape Sequences
  • Definition Escape sequences are specially
    sequenced characters used to format output
  • \
  • Ex printf( \ This is quoted text \ )
  • \
  • Ex printf( \n A single quote looks like \
    \n)
  • \ \ Comment Block

4
Directives
  • include ltstdio.hgt
  • Using a directive to include a header file
  • Stdio.h standard input output header file

5
Memory
  • A computers long-term memory is called
    nonvolatile memory and is generally associated
    with mass storage devices, such as hard drives.
  • A computers short term memory is called volatile
    memory. It loses is data when power is removed
    from the computer, such as RAM

6
Data Types
Data Type Description Declaration Example
Integer Whole numbers, positive or negative int x -3 , 0, 3 , 29
Floating-point number All numbers, positive or negative, decimals and fractions float x -0.35543 , 0.00, 554433.33281
Character Representations of integer values known as character codes char x m, M,
To declare a constant (read only) value const
int x 20 const float PI 3.14
7
Variable Types
TYPE SIZE VALUES
bool 1 byte true (1) or false (0)
char 1 byte a toz , A to Z, 0 to 9, space, tab, and so on
int 4 bytes -2,147,483,648 to 2,147,483,647
short 2 bytes -32,768 to 32,767
long 4 bytes -2,147,483,648 to 2,147,483,647
float 4 bytes - (1.2 x 10-38 to 3.4 x 1038)
double 8 bytes - (2.3 x 10-308 to -1.7 x 10308)
8
Arithmetic and Order of Precedence
Operator Description
Multiplication
/ Division
Modulus (remainder)
Addition
- Subtraction
Order of Precedence Description
( ) Parentheses are evaluated first, from innermost to outermost
, /, Evaluated second, from Left to Right
, - Evaluated last, from Left to Right
9
printf() scanf()
  • Can you explain what the code is doing?

10
Can you predict the printout?
  • int main()
  • printf (c c \n", 'a', 65)
  • printf ("d ld\n", 1977, 650000L)
  • printf (" 10d \n", 1977)
  • printf ("010d \n", 1977)
  • printf ("floats 4.2f \n", 3.1416)
  • printf ("s \n", "A string")
  • printf(f \n, 55.55)
  • return 0

11
Printouts
  • printf (c c \n", 'a', 65) aA
  • printf ("d ld\n", 1977, 650000L) 1977650000
  • printf (" 10d \n", 1977)
    1977
  • printf ("010d \n", 1977) 0000001977
  • printf ("floats 4.2f \n", 3.1416)
    3.14
  • printf ("s \n", "A string") A string
  • printf(f \n, 55.55) 55.550000

12
Review Printing with Precision
Example Printout
printf(.1f,3.123456) 3.1
printf(\n.2f,3.123456) 3.12
printf(\n.3f,3.123456) 3.123
printf(\n.4f,3.123456) 3.1235
printf(\n.5f,3.123456) 3.12346
printf(\n.6f,3.123456) 3.123456
13
scanf
  • Syntax
  • scanf(conversion specifier, variable)

Conversion Specifies Description
d Receives integer value
f Receives floating-point number
c Receives character
14
Can you predict the printout when the user enters
2 and 4?
  • include ltstdio.hgt
  • main()
  • int iOperand1 0
  • int iOperand2 0
  • printf(\n Enter first operand )
  • scanf(d, iOperand1)
  • printf(\n Enter second operand )
  • scanf(d, iOperand2)
  • printf(The result is d \n, 24/(iOperand1
    iOperand2)6/3)

15
Boolean Operators
  • Do you know the answers to these?
  • A. !( 1 0 )
  • B. !( 1 1 0 )
  • C. !( ( 1 0 ) 0 )

16
Boolean Operators Quiz Answers
  • A. !( 1 0 ) ANSWER 0
  • B. !( 1 1 0 ) ANSWER 0 (AND is
    evaluated before OR)
  • C. !( ( 1 0 ) 0 ) ANSWER 1 (Parenthesis
    are useful)

17
If Statements
18
Quiz
  • Can you write code that will ask a user to enter
    a number 1 , 2 , or 3 and print out the
    following

User Input Printout
1 1 is the loneliest number
2 2 is better than 1
3 3s a crowd
19
Answer
  • int a
  • int main()
  • printf (Enter one of the following d, d, or
    d\n, 1, 2, 3)
  • scanf(d, a)
  • if(a1 a2 a 3)
  • if(a1)
  • printf(\n d is the loneliest number \n, 1)
  • if(a2)
  • printf(\nd is better than d \n,2,1)
  • if(a3)
  • printf(\nd \ s a crowd \n,3)
  • else
  • printf(Sorry, you entered an invalid value\n)

20
Switch-Case Statments
21
Example Switch-Case Statement
22
Loops
  • while ( condition ) Code to execute while the
    condition is true
  • Quiz Can you write a program that prints x
  • while x increments from 0 to 10?

23
Answer While Quiz
24
  • do while ( condition )
  • What is the main difference between Do while
    and while?

25
Answers
  • while ( condition ) Code to execute while the
    condition is true
  • do while ( condition )
  • Do while() executes code at least once!

26
Questions
  • How do you generate a new line?
  • What will happen if a user tries to enter a
    decimal into scanf(d, number)
  • What does each of the following do?
  • , - , , / , , , gt , lt

27
FOR Loops
  • Use when the number of iterations is already
    known
  • Syntax

for ( variable initialization condition
variable increment/decrement) Code to execute
while the condition is true
28
Can you predict the printout?
include ltstdio.hgt int main() int x for
( x 0 x lt 10 x ) printf(
"d\n", x ) getchar()
29
Practice FOR Loops
  • Write a program using a FOR Loop to display all
    of the multiples of 5 from 0 to 100.

30
Answer
include ltstdio.hgt int main() int x for
( x 0 x lt 20 x ) printf(
"d\n", x5 ) getchar()
31
Incrementing/Decrementing
  • x x-- Postfix
  • x --x Prefix

main() int x 0 int y 0 printf(\n The
value of y is d \n, y) printf(\n The value
of x is d \n, x)
Answer 0 1
32
BREAK and CONTINUE
  • Use to manipulate flow in loops
  • What does a Break statement do when executed
    within a loop?
  • What does a Continue statement do when executed
    within a loop?

33
Break and Continue Example
include ltstdio.hgt main() int x for ( x
10 x gt5 x-- ) if (x7)
break printf( \n d \n , x )
include ltstdio.hgt main() int x for ( x
10 x gt5 x-- ) if (x7)
continue printf( \n d \n , x )

34
Function Prototypes Definitions
  • Function Prototype Syntax
  • return-type function_name ( arg_type arg1,
    ..., arg_type argN )
  • Function Prototypes tell you the data type
    returned by the function, the data type of
    parameters, how many parameters, and the order of
    parameters
  • Function definitions implement the function
    prototype
  • Where are function prototypes located in the
    program?
  • Where do you find function definitions?

35
Function Prototypes
  • Where are function prototypes located in the
    program?
  • Answer before the main() Function!
  • Function Definitions are self contained outside
    of the main() function

36
Calling Functions
include ltstdio.hgt int mult ( int, int) int
main() int x int
y printf( "Please input two numbers to be
multiplied " ) scanf( "d", x ) scanf(
"d", y ) printf( "The product of your two
numbers is d\n", mult( x, y ) ) getchar()
int mult (int a, int b) return a b
37
Whats Wrong with This Code?
include ltstdio.hgt printNumber() main() int
x printNumber(x) printNumber() printf(\
n The number is d \n, x)
38
Answer
include ltstdio.hgt void printNumber( int x)
main() int x printNumber(x) void
printNumber(int y) printf(\n The number is d
\n, y)
Note its not absolutely necessary to write
VOID, but its a good practice.
39
Can You Pick Out the Local and Global Variables?
include ltstdio.hgt void printNumbers() int
iNumber main() int x for(x0,
xlt10,x) printf(\n Enter a
number) scanf(d, iNumber) printNumbers
() void printNumbers() printf(\n Your
number is d \n, iNumber)
40
Variable Scope
  • Variable scope defines the life time of a
    variable
  • Local Scope defined within functions and loses
    scope after function is finished. Can reuse in
    other functions (ex. p.123)
  • Global Scope defined outside of functions and
    can be accessed by multiple functions

41
Challenge Question
  • Can you write code that asks a user to input 4
    integers, adds the numbers together in one
    function, multiplies them in another, and finally
    prints out the difference between the sum and
    product? The user should have to do this 5 times.

42
ANSWER (one option)
include ltstdio.hgt int addNumbers(int, int,
int, int) int multNumbers(int, int, int, int)
main() int a, b, c, d, counter0
while(counterlt6) printf(\n Please
enter 4 integers separated by spaces\n) scanf(
d d d d, a, b, c, d) printf(\n d
\n, addNumbers(a,b,c,d)-multNumbers(a,b,c,d)) c
ount int addNumbers(int f, int g,
int h, int y) return fghy int
multNumbers(int f, int g, int h, int y) return
fghy
43
How to Declare a One-Dimensional Array
  • Can you declare a one-dimensional array made up
    of 10 integers?
  • Answer int iArray10
  • How to declare an Array
  • int iArray10
  • float fAverages30
  • double dResults3
  • short sSalaries 9
  • char cName19 //18 characters and 1 null
    character

44
How to Initialize a 1-D Array
  • Why do we initialize? Because memory spaces may
    not be cleared from previous values when arrays
    are created
  • Can initialize an array directly
  • Example int iArray50,1,2,3,4
  • Can initialize an array with a loop such as FOR()

45
Example of Initializing an Array Using a For()
Loop
include ltstdio.hgt main() int x int
iArray5 for( x0 x lt 5 x)
iArrayx 0
46
Printing Arrays
  • Can you add code to print out the values of the
    program below?

include ltstdio.hgt main() int x int
iArray5 for( x0 x lt 5 x)
iArrayx 0
47
Answer
include ltstdio.hgt main() int x int
iArray5 for( x0 x lt 5 x)
iArrayx 0 for(x0 xlt5 x)
printf(\n The value of iArray index d is d
\n, x, iArrayx)
48
Accessing the elements in an array
  • How do you search through an array?

49
include ltstdio.hgt main() int x int
iValue int iFound -1 int iArray5 for(
x0 x lt 5 x) iArrayx
(xx) printf(\n Enter value to search
for) scanf(d, iValue) for(x0 xlt5
x) if( iArrayx iValue)
iFound x break
) if(iFound gt-1) printf(\n I found
your search value in element d \n,
iFound) else printf(\n Sorry, your
search value was not found \n)
50
String functions
Function Description
strlen(str) Returns numeric string length up to, but not including null character
tolower(str) and toupper(str) Converts a single character to upper or lower case
strcpy(str1, str2) Copies the contents of str2 string into str1 string.
strcat(str1, str2) Appends str2 string onto the end of str1 string.
strcmp(str1,str2) Compares two strings for equality. A zero value indicates that both strings are equal.A value greater than zero indicates that the first character that does not match has a greater value in str1 than instr2 And a value less than zero indicates the opposite.
strstr(str1, str2) Searches the first string for the first occurrence of the second string. A pointer to the first occurrence in str1 of any of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1.
51
Data File Hierarchy
Entity Description
Bit Binary digit, 0 or 1 Smallest value in a data file
Byte Eight bits Stores a single character
Field Grouping of bytes i.e a word, social security number
Record Grouping of fields a single row of information, student name, age, ID, GPA
File Grouping of records separate fields in a record using spaces, tabs, or commas
52
Simple Data Structures
53
(No Transcript)
54
(No Transcript)
55
Passing Structures to Functions
56
Sizeof()
57
Malloc()
Please enter how long your name is 21 Please
enter your name Nawaf Hello Nawaf
Please enter how long your name is -7 Failed
allocation memory
58
free()
59
Calloc and Realloc()
  • int n
  • int n1
  • n( int ) calloc(5, sizeof(int)) //
    Reserves a block of memory for 5 integers
  • //Decide you need to reallocate more memory later
    in the program
  • n1 (int ) realloc(n, 10 sizeof(int))//allocat
    e 10 integers instead of 5
  • if (n1!NULL)
  • nn1
  • else printf("Out of memory!")
  • realloc() returns null if unable to complete or a
    pointer to the newly reallocated memory.

60
New Material-
  • Do you know the syntax for each of these, used to
    read and write to data files?
  • Pointers think of it as the memory address of
    the file
  • fopen()
  • fclose()
  • fscanf()
  • fprintf()

61
fopen(file name, Mode)
  • fopen() returns a FILE pointer back to the pRead
    variable

include ltcstdiogt Main() FILE pRead pRead
fopen(file1.dat, r) if(pRead NULL)
printf(\nFile cannot be opened\n) else
printf(\nFile opened for reading\n) fclose(p
Read)
62
Common Text File Modes
Mode Meaning Already Exists Does Not Exist
r Open a file for reading read from start error
w Create a file for writing destroy contents create new
a Append to a file write to end create new
r Open a file for read/write read from start error
w Create a file for read/write destroy contents create new
a Open a file for read/write write to end create new
63
fclose(file pointer)
  • Pretty basic, yet important.

64
fscanf(FILE pointer, data type, variable in
which to store the value)
  • Reads a single field from a data file
  • s will read a series of characters until a
    white space is found
  • can do fscanf(pRead, ss, name, hobby)

65
  • include ltstdio.hgt
  • Main()
  • FILE pRead
  • char name10
  • pRead fopen(names.dat, r)
  • if( pRead NULL )
  • printf( \nFile cannot be opened\n)
  • else
  • printf(\nContents of names.dat\n)
  • fscanf( pRead, s, name )
  • while( !feof(pRead) )
  • printf( s\n, name )
  • fscanf( pRead, s, name )
  • fclose(pRead)

66
Quiz
Kelly 11/12/86 6 Louisville Allen 04/05/77 49 At
lanta Chelsea 03/30/90 12 Charleston
Can you write a program that prints out the
contents of this information.dat file?
67
  • include ltstdio.hgt
  • main()
  • FILE pRead
  • char name10
  • char birthdate9
  • float number
  • char hometown20
  • pRead fopen(information.dat, r)
  • if( pRead NULL )
  • printf( \nFile cannot be opened\n)
  • else
  • fscanf( pRead, ssfs, name0,
    birthdate, number, hometown )
  • while( !feof(pRead) )
  • printf( s \t s \t f \t
    s\n, name, birthdate, number, hometown )

68
fprintf(FILE pointer, list of data types,list
of values or variables)
  • The fprintf() function sends information (the
    arguments) according to the specified format to
    the file indicated by stream. fprintf() works
    just like printf() as far as the format goes.

69
  • include ltstdio.hgt
  • main()
  • FILE pWrite
  • char fName20
  • char lName 20
  • float gpa
  • pWrite fopen(students.dat,w)
  • if( pWrite NULL )
  • printf(\nFile not opened\n)
  • else
  • printf(\nEnter first name, last name, and GPA
    separated
  • printf(Enter data separated by spaces)

70
Quiz
  • Can you write a program that asks the user for
    their
  • Name
  • Phone Number
  • Bank account balance
  • And then prints this information to a data file
    called accounts.dat ?

71
Questions?
Good Luck in your final Exam from REACH
72
References
  • TEXTBOOK RESOURCE C Programming for the
    Absolute Beginner 2nd Edition by Michael Vine
  • www.cprogramming.com

73
Quiz
Kelly 11/12/86 6 Louisville Allen 04/05/77 49 At
lanta Chelsea 03/30/90 12 Charleston
How many fields are there? How many records are
there? How many bytes are there in the first
record? How many bits are there in Kelly?
Write a Comment
User Comments (0)
About PowerShow.com