REACH TEST REVIEW - PowerPoint PPT Presentation

1 / 49
About This Presentation
Title:

REACH TEST REVIEW

Description:

... member_type2 member_name2; member_type3 member_name3; . . } object_names ... where a file stream is ... double_array[2][5]); Initializing: Use ... – PowerPoint PPT presentation

Number of Views:104
Avg rating:3.0/5.0
Slides: 50
Provided by: kmn1
Category:

less

Transcript and Presenter's Notes

Title: REACH TEST REVIEW


1
REACH TEST REVIEW
  • CECS 130 EXAM 2

2
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?

3
Function Prototypes
  • Function Prototypes are placed after preprocessor
    directives and before the main() function.
  • Function Definitions are self contained outside
    of the main() function

4
Calling Functions
include ltstdio.hgt int mult ( int, int )
main() int x,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() //
To pause the screen, same like system(pause)
int mult (int x, int y) // Make sure not to
end this with a semicolon return(x y)
5
Calling Functions
include ltstdio.hgt void print_char( char , int)
main() char name20REACH
CRC print_char(name, 3) // The name of the
array is a pointer to that array getchar()
void print_char (char nameStringArray, int x)
printf(The character specified is c,
nameStringArrayx-1)
6
Whats Wrong with This Code?
include ltstdio.hgt void printReportHeader() ma
in() printReportHeader void
printReportHeader() printf(\n
Column1\tColumn2\tColumn3\tColumn4 \n)
7
Whats Wrong with This Code?
include ltstdio.hgt void printReportHeader() ma
in() printReportHeader // Should be corrected
to printReportHeader() void printReportHeader()
printf(\n Column1\tColumn2\tColumn3\tColumn4
\n)
8
Variable Scope
  • Variable scope defines the life time of a
    variable
  • Local Scope defined within functions and loses
    scope after function is finished.
  • Global Scope defined outside of functions and
    can be accessed by multiple functions

9
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)
10
How to Declare a One-Dimensional Array
  • Can you declare a one-dimensional array made up
    of 10 integers?
  • data_type namesize_of_array
  • Answer int iArray10
  • How to declare an Array?
  • int iArray10 // Array of 10 integers
  • float fAverages30 // Array of 30 floats
  • char cName19 // 18 characters and 1 null
    character

11
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.
  • Initialize an array directly when we declare it
  • Example int iArray50,1,2,3,4
  • We csn initialize an array with a loop such as
    for().

12
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
13
Printing Arrays
  • Can you add code that will print out the value of
    each element of iArray?

include ltstdio.hgt main() int x int
iArray5 for( x0 x lt 5 x)
iArrayx 0
14
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)
15
Accessing the elements in an array
  • How do you search through an array?

16
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)
17
2-D Arrays
  • Declaring
  • data_type namesize_dim_1size_dim_2
  • size_dim_1 is known as ROW count.
  • size_dim_2 is known as COLUMN count.
  • int double_array2010
  • Accessing
  • printf(Element 2,5 is d, double_array25)

18
2-D Arrays
  • Initializing Use a second, nested FOR() loop

include ltstdio.hgt main() int x, y,
double_array1020 for( x0 x lt 10 x) //
Outer loop goes with ROW count for(y0
ylt20 y) // Inner loop goes with Column
count double_arrayxy 0

19
2-D Arrays
  • Passing to a function

include ltstdio.hgt includeltstdlib.hgt void
custom_func(int 2) // Multi dimensional
arrays must have a bound main() int
double_array221,2,3,4 custom_func
(double_array) system(pause) void
custom_func (int temp 2) printf(Test
d, d, d, temp00,temp01,temp11)
OUTPUT Test 1, 2, 4
20
POINTERS
  • Pointers are variables that contain memory
    addresses as their values.
  • A variable name directly references a value.
  • A pointer indirectly references a value.
    Referencing a value through a pointer is called
    indirection.
  • A pointer variable must be declared before it can
    be used.
  • ALL Arrays are Pointers!

21
POINTERS
  • Examples of pointer declarations
  • FILE fptr //fptr is a pointer to a file
  • int a //a is a pointer to a file
  • float b //b is a pointer to a file
  • char c //c is a pointer to a file
  • The asterisk, when used as above in the
    declaration, tells the compiler that the variable
    is to be a pointer, and the type of data that the
    pointer points to, but NOT the name of the
    variable pointed to.

22
POINTERS
  • Consider the statements
  • include ltstdio.hgt
  • int main ( )
  • FILE fptr1 , fptr2 / Declare two file
    pointers /
  • int aptr / Declare a pointer to an
    int /
  • float bptr / Declare a pointer to a
    float /
  • int a / Declare an int variable /
  • float b / Declare a float variable
    /
  • return 0

23
Use of () and ()
  • When is used?
  • When is used?
  • -- "address operator" which gives or produces
    the memory address of a data variable
  • -- "dereferencing operator" which provides the
    contents in the memory location specified by a
    pointer

24
Pointers and Functions
  • If instead of passing the values of the variables
    to the called function, we pass their addresses,
    so that the called function can change the values
    stored in the calling routine. This is known as
    "call by reference" since we are referencing the
    variables.
  • The following shows a swap function modified from
    a "call by value" to a "call by reference". Note
    that the values are now actually swapped when the
    control is returned to main function.

25
Pointers
  • Unary operator () Address of
  • int x10
  • int xptr
  • xptr x //xptr now points to x
  • Indirection operator ()
  • int x, y 10
  • int xptr
  • xptr y
  • x xptr //copies contents of y into x

26
Pointers with Functions (example)
  • void swap( int a, int b )
  • int temp
  • temp a a b b temp
  • printf ("ad bd\n", a, b)
  • Output
  • a5 b6 //printed from main
  • a6 b5 //printed from inside swap
  • a6 b5 // printed from main after calling swap
    function
  • include ltstdio.hgt
  • void swap ( int , int )
  • int main ( )
  • int a 5, b 6
  • printf("ad bd\n",a,b)
  • swap (a, b)
  • printf("ad bd\n",a,b)
  • return 0

27
Const Pointers
  • To avoid accidently changing the value the
    pointer points to Use const
  • void custom(const int )
  • main()
  • void custom(const int )

28
Strings
  • Strings are character arrays that have a special
    set of functions for handling their data as
    complete sentences or strings of characters.
  • Since a string is an array it is also a pointer.
  • Character literals are expressed with a single
    quote char examplea
  • String literals are expressed with double quote
  • char example10REACH

29
Declaring Strings Size
  • When determining the maximum length your string
    variable needs to be it is important to consider
    a NULL Character \0
  • char example10REACH
  • example0 -gt R
  • example1 -gt E
  • example2 -gt A
  • example3 -gt C
  • example4 -gt H
  • example5 -gt \0

30
Manipulating Strings
Function Description
strlen() Returns numeric string length up to, but not including null character
tolower() and toupper() Converts a single character to upper or lower case
strcpy() Copies the contents of one string into another string
strcat() Appends one string onto the end of another
strcmp() Compares two strings for equality
strstr() Searches the first string for the first occurrence of the second string
31
String strlen() Example
  • include ltstdio.hgt
  • includeltstdlib.hgt
  • include ltstring.hgt
  • int main ()
  • char szInput256
  • printf ("Enter a sentence ")
  • gets (szInput)
  • printf ("The sentence entered is u characters
    long.\n",(unsigned)strlen(szInput))
  • System(pause)
  • return 0

Output Enter sentence just testing The sentence
entered is 12 characters long.
32
String tolower()/toupper() Example
Output test string.
  • include ltstdio.hgt
  • include ltctype.hgt
  • int main ()
  • int i0
  • char str"Test String.\n"
  • char c
  • while (stri)
  • cstri
  • putchar (tolower(c))
  • i
  • return 0

For toupper() will be same case, just replace
tolower() by toupper()
33
Strcpy() Example
  • include ltstdio.hgt
  • include ltstring.hgt
  • int main ()
  • char str1"Sample string"
  • char str240
  • char str340
  • strcpy (str2,str1)
  • strcpy (str3,"copy successful")
  • printf ("str1 s\nstr2 s\nstr3
    s\n",str1,str2,str3)
  • return 0

Output str1 Sample string str2 Sample
string str3 copy successful
34
Strcat() Example
  • include ltstdio.hgt
  • include ltstring.hgt
  • int main ()
  • char str80
  • strcpy (str,"these ")
  • strcat (str,"strings ")
  • strcat (str,"are ")
  • strcat (str,"concatenated.")
  • puts (str)
  • return 0

Output these strings are concatenated.
35
Strcmp() Example
  • include ltstdio.hgt
  • include ltstring.hgt
  • int main ()
  • char szKey "apple"
  • char szInput80
  • do
  • printf ("Guess my favourite fruit? ")
  • gets (szInput)
  • while (strcmp (szKey,szInput) ! 0)
  • puts ("Correct answer!")
  • return 0

Output Guess my favourite fruit? orange Guess
my favourite fruit? apple Correct answer!
36
Strstr() Example (Find/Replace)
  • /This example searches for the "simple"
    substring in str and replaces that word for
    "sample"./
  • include ltstdio.hgt
  • include ltstring.hgt
  • int main ()
  • char str "This is a simple string"
  • char pch
  • pch strstr (str,"simple") / returns a
    pointer to first occurrence of simple in
    str/
  • strncpy (pch,"sample",6) // copies 6
    characters from source-gtpch
  • puts (str)
  • return 0

Output This is a sample string
37
Quiz?
Can you make a program to sort an array of 10
integers either ascending or descending
order? Consider you have the array107,8,5,3,0,
3,2,1,4,10 Write a code to do the sorting.
38
Answer
  • includeltstdio.hgt
  • int main()
  • int array107,8,5,3,0,3,2,1,4,10
  • int temp0,i,j
  • for(i0ilt10i) // print the
    array before sorting
  • printf(d\n,arrayi)
  • for(i0ilt10i)
  • for(j0jlt10j)
  • if(arrayiltarrayj) / This will
    do Ascending order, if you need Descending order
    just flip the lt to gt /
  • temparrayi // This will
    do the swapping
  • arrayiarrayj
  • arrayjtemp
  • for(i0ilt10i) // print
    the array after sorting
  • printf(5d ,arrayi)

39
Quiz?
  • Can you modify the previous code to pass the
    array to a function that will sort the array and
    print the sorted array in main.

40
Answer
  • includeltstdio.hgt
  • void sortArray(int ,int size)
  • int array107,8,5,3,0,3,2,1,4,10
  • int temp0,i,j
  • int main()
  • for(i0ilt10i)
  • printf(d\n,arrayi)
  • sortArray(array,10)
  • for(i0ilt10i)
  • printf(5d,arrayi)
  • getchar()
  • return 0

void sortArray(int passedArray ,int
arraySize) for(i0iltarraySizei)
for(j0jltarraySizej)
if(passedArrayiltpassedArrayj)
temppassedArrayi
passedArrayipassedArrayj
passedArrayjtemp
Calling the function sortArray(array,10) is
equivalent to sortArray(array0,10)
41
Structs
  • A data structure is a group of data elements
    grouped together under one name. These data
    elements, known as members, can have different
    types and different lengths. Data structures are
    declared in C/C using the following
    syntaxstruct structure_name member_type1
    member_name1member_type2 member_name2member_ty
    pe3 member_name3.. object_names

42
Struct Examples
struct product int weight float price product apple product banana, melon
Or you can do it this way struct product int
weight float price apple, banana, melon
43
Accessing Structs Members
  • apple.weight
  • apple.price
  • banana.weight
  • banana.price
  • melon.weight
  • melon.price

44
Complete Example
  • include ltstdio.hgt
  • includeltstdlib.hgt
  • using namespace std
  • struct database
  • int id_number
  • int age
  • float salary
  • int main()
  • database employee //There is now an employee
    variable that has modifiable
  • // variables inside it.
  • employee.age 22
  • employee.id_number 1
  • employee.salary 12000.21
  • printf(" Employee Age is d\n Employee ID is
    d\n Employee Salary is .2f\n"

45
UNION
  • union u_tag
  • int ival
  • float fval
  • char sval
  • u

46
Union Example
  • include ltstdio.hgt
  • includeltstdlib.hgt
  • int main()
  • union data
  • char a
  • int x
  • float f
  • myData
  • int mode 1
  • myData.a 'A'
  • printf("Here is the Data\nc\ni\n.3f\n",
    myData.a, myData.x, myData.f )
  • myData.x 42
  • mode 2
  • printf("Here is the Data\nc\ni\n.3f\n",
    myData.a, myData.x, myData.f )

47
Difference between UNION and STRUCT
  • The difference between structure and union in c
    are
  • Union allocates the memory equal to the maximum
    memory required by the member of the union but
    structure allocates the memory equal to the total
    memory required by the members.
  • In union, one block is used by all the member of
    the union but in case of structure, each member
    have their own memory space  

48
Questions?
  • Good Luck from REACH in your Test.

49
References
  • TEXTBOOK RESOURCE C Programming for the
    Absolute Beginner 2nd Edition by Michael Vine
  • www.cprogramming.com
Write a Comment
User Comments (0)
About PowerShow.com