|
|
|
|
To introduce the array data structure. | |
|
The understand the use of arrays to store, sort and search list and tables of values | |
|
To understand how to declare an array, initialize an array and refer to individual elements of an array. | |
|
To be able to pass arrays to functions. | |
|
To understand basic sorting techniques. | |
|
To be able to declare and manipulate multiple subscript arrays. |
Lab assignment 6 that is due on the 9th week (after the midterm and spring break)
For Midterm, we will cover chapters 1 to 3. Midterm will be 3 hours long. There will be two parts.
|
First part will be closed book and close notes. | |
|
Second part will be open book and open notes. In this part you will write a program, something very similar to a lab assignment that you have done. |
Review of Chapters 1 to 3
NOTE: This is a review of Chapters 1 to 3, things listed may or may NOT be on the midterm exam.
Name all of the basic data types in the language
There are 7 basic data types in C++. They are:
|
int Integer, machine dependent, usually 16 bits. | |
|
float Floating-point number, 32 bits. | |
|
double Double precision floating point, 64 bits (long double 80). | |
|
char Character, 8 bits. | |
|
void “Nothing”, used to indicate that a function returns no value. | |
|
wchar_t Wide character, 16 bits, used for International characters. | |
|
bool Boolean. Indicates true or false. |
What are operators (e.g., arithmetic, relational…)?
Operators: Arithmetic, Logical, Relational and Bitwise.
Arithmetic (+, -, /, *, %)
Increment (++) and decrement (--), Postfix or Prefix.
Relational (>, >=, <, <=, ==, !=). Returns true or false.
Logical (&&, ||, !). Returns true or false.
Bitwise (&, |) (will not be on mid-term exam)
Signed, unsigned, long, short can be applied to integers.
Signed and unsigned can be applied to characters.
Long can be applied to double (int and chars).
Write function prototypes (i.e., declarations) and be able to state what a function prototype is and what it's components are.
Function prototype is declaration of a function before a function is called. This is done before the “main” program (function) starts. Function declaration consists of
Return type
Function name
Parameter list, if function contains arguments. Parameter list consists of parameter type and optionally the name of all the parameters that are passed to the function. Parameter names are not required, but it is a good practice to put them.
Example:
[return type] <function name> [type param_Name_1, …. type param_Name_n]
Be able to demonstrate where variables of global and functional scope are declared.
Scope of a variable is considered global if it is defined outside and before the main function and the functions that will access it. Global variables are defined before the “main” function and before the definition of all other functions. They are then accessible to main and all the functions of the program.
Scope of a variable defined within a function is limited only to that function. This variable is invisible to all other function and the main program.
Scope of variable with a function can also be restricted between { and }.
Be able to demonstrate the usage of default parameters
void myfunc (int x, int y=9, int z=10);
main {
….
….
myfunc(a, b, c); //valid
myfunc(a, b); //valid
myfunc(a); //valid
myfunc(a, , c); //INVALID
….
}
void myfunc (int x, int y, int z) {
cout << "Value of x=" << x << ", y=" << y << ",z=" << z << endl;
return ;
}
Be able to write functions that overload the same function name
Write multiple functions with same name but different arguments or different argument type.
void mySort (int &x, int &y, int &z); // declaration of function mySort;
void mySort (float &x, float &y, float &z); // declaration of function mySort;
void mySort (char &x, char &y, char &z); // declaration of function mySort;
Be able to name and describe the 4 storage class specifiers and 2 access modifiers
Access Modifiers: (const and volatile, will NOT be in midterm)
const
Once the is variable is defined as const, it’s value cannot be change. You will get an error if you attempt to modify this variable.
volatile:
The volatile modifier warns the compiler against optimization of this variable and that this variable’s value may be changed in a way not explicitly specified by the program. For example, the address of a global variable is given to an interrupt-driven clock routine that updates this variable with each tick of the clock. In this case, the content of the variable is changed without explicit use of the assignment operator.
Storage class specifiers:
C++ supports 5 storage class specifiers. They are
auto
auto specifier declares are a local variable. Since all local variables are local by default, it is rarely used.
extern
You can only define one copy of a global variable in a file for each program. This works fine if all functions are in one file. If the variable needs to be accessed across multiple files, the global variable is defined once and then declared as extern in each file that needs to access it. The extern specifier allows a variable to be made known to a module, but does not actually create that variable.
register
The register storage class specifier tells the compiler to store this variable in such a way that it can be accessed as soon as possible. Typically it means that storing the variable in a register of the CPU or cache memory. Typical usage of this specifier is for loop control variables. Using register specifier does not guarantee that compiler will honor the request, it may or may not depending on the circumstances.
“register” specifier in “C” was restricted to integers only, but C++ allows you to use this specifier for any type of data.
static
static specifier makes a variable permanent similar to global variable, but static variables are only visible to functions they belong to. It’s effect on local variable is different than global variable.
static local variable.
When a static specifier is applied to a local variable, the value of the variable is saved when the function ends and is restored when the function is restarted. It is initialized only once when the program execution begins. The difference between a static and global variable is that static variable is only visible to the function it is declared in.
static global variable.
When static specifier is applied to a global variable, it is visible only to the functions that belong to this file. It is invisible to functions in other files, which may be part of the same program.
Mutable (will not be in Midterm)
Used in class. If this specifier is placed before a variable, that variable can be modified by a const function. Const functions are not allowed to modify any member (data or function) of a class, unless that member is declared mutable.
Demonstrate an understanding of enumerations
You can assign a name for integers constants. This is known as Enumeration.
Example:
enum colors
{
red, blue, green, mangnta=7,cyan, yellow
}; (Note: The semicolon is required here)
colors carColors, houseColors;
Value of red is automatically initialized to 0, blue=1, green=2, magenta=7, cyan=8, yellow=9
Demonstrate the usage of parameter passage by value, pointer and reference.
void funcByValue (int x, int y);
void funcByReference (int &x, int &y);
void main () {
int a, b;
char ch;
a = 10;
b = 5;
cout << "Value " << a << ", " << b << endl;
cout << "======================================================" << endl;
funcByValue (a, b);
cout << "Value " << a << ", " << b << " After funcByValue" <<endl;
cout << "======================================================" << endl;
funcByReference (a, b);
cout << "Value " << a << ", " << b << " After funcByReference" << endl;
cout << "======================================================" << endl;
return ;
}
void funcByValue (int x, int y)
{
x = x * 100;
y = y * 100;
cout << "funcByValue in function = " << x << ", " << y << endl;
return ;
}
void funcByReference (int &x, int &y)
{
x = x * 100;
y = y * 100;
cout << "funcByReference in function = " << x << ", " << y << endl;
return ;
}
How would you decide on which type of loop (for, while, do/while) to use?
What is the difference between the three loop?
Must know the syntax of if, if-then-else, switch, while, for, do-while constructs.
When would you use a switch statement instead of if-then-else.
|
Copyright (c) Yusuf Family Website 2001 |