Transcript REACH TEST REVIEW
CECS 130 EXAM 2
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?
Function Prototypes are placed after preprocessor directives and before the main(){} function.
Function Definitions are self contained outside of the main(){} function
#include
#include
#include
#include
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
#include
Can you declare a one-dimensional array made up of 10 integers?
data_type name[size_of_array]
Answer: int iArray[10];
How to declare an Array?
int iArray[10]; // Array of 10 integers float fAverages[30]; // Array of 30 floats
char cName[19];
// 18 characters and 1 null character
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 iArray[5]={0,1,2,3,4}; We csn initialize an array with a loop such as for().
#include
Can you add code that will print out the value of each element of iArray?
} { #include
} { #include
How do you search through an array?
} { #include
Declaring: data_type name[size_dim_1][size_dim_2] ▪ ▪ size_dim_1 is known as “ROW” count.
size_dim_2 is known as “COLUMN” count.
int double_array[20][10] Accessing: printf (“Element 2,5 is: %d”, double_array[2][5]);
Initializing: Use a second, nested FOR() loop } { #include
Passing to a function: { #include
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!
Lect 14 P. 20
Examples of pointer declarations: FILE *fptr; //fptr is a pointer to a file int *a; //a is a pointer to a file float *b; char *c; //b is a pointer to a file //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.
Lect 14 P. 21
Consider the statements: } { #include
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 Lect 14 P. 23 Winter Quarter
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.
Lect 14 P. 24 Winter Quarter
Unary operator (&) – “Address of” int x=10; 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
#include
Output:
a=5 b=6 //printed from main a=6 b=5 //printed from inside swap a=6 b=5 // printed from main after calling swap function Lect 14 P. 26
To avoid accidently changing the value the pointer points to: Use const } void custom(const int *); main(){} { void custom(const int *)
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 example=‘a’; String literals are expressed with double quote: char example[10]=“REACH”;
When determining the maximum length your string variable needs to be it is important to consider a NULL Character: “\0” char example[10]=“REACH”; example[0] -> R example[1] -> E example[2] -> A example[3] -> C example[4] -> H example[5] -> \0
Function
strlen() tolower() and toupper() strcpy() strcat() strcmp() strstr()
Description
Returns numeric string length up to, but not including null character Converts a single character to upper or lower case Copies the contents of one string into another string Appends one string onto the end of another Compares two strings for equality Searches the first string for the first occurrence of the second string
} #include
Output:
Enter sentence: just testing The sentence entered is 12 characters long.
{ int main () char szInput[256]; printf ("Enter a sentence: "); gets (szInput); printf ("The sentence entered is %u characters long.\n",(unsigned)strlen(szInput)); System(“pause”); return 0;
} { #include
Output:
test string.
For toupper() will be same case, just replace tolower() by toupper()
} #include
#include
} #include
} /*This example searches for the "simple" substring in str and replaces that word for "sample".*/ #include
Can you make a program to sort an array of 10 integers either ascending or descending order?
Consider you have the array[10]={7,8,5,3,0,3,2,1,4,10} Write a code to do the sorting.
#include
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.
#include
void sortArray(int passedArray[ ],int arraySize) { for(i=0;i
Calling the function sortArray(array,10) is equivalent to sortArray(&array[0],10)
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 syntax: struct structure_name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; .
.
} object_names;
struct int product { weight; float price; } ; product apple; product banana, melon; Or you can do it this way: struct int float product { weight; price; } apple, banana, melon;
apple.weight apple.price banana.weight
banana.price
melon.weight melon.price
} #include
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" ,employee.age,employee.id_number,employee.salary); system("pause");
union u_tag { int ival; float fval; char *sval; } u;
#include
The difference between structure and union in c are: 1.
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. 2.
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
Good Luck from REACH in your Test.
TEXTBOOK RESOURCE: C Programming for the Absolute Beginner 2 nd Edition by Michael Vine www.cprogramming.com