Lab 4: Arrays and Use of
Functions
Arrays
You can imagine array is a single row of data, it is very similar to one row in Excel, each "cell" is assigned an id. The id of array is started from 0.
Array A:
0 | 1 | 2 | 3 | ... |
1st data | 2nd data | 3rd data | 4th data | ... |
To access the variable of the array is the same as normal variable, the name of the first variable in array is A[0], the second is A[1], ...
To declare an array, it is the same as declaring a variable, we type name[size]:
e.g. an array of char type variable with size 26:
char alphabet[26];
e.g. an array of int type with size 10:
int digit[10];
To assign or get the value of a specific array variable, we need to know the position/id of the variable you want:
e.g. set the 24th variable in alphabet to 'x'
alphabet[23] = 'x';
e.g. add the 5th and 7th integer and store it in a new variable add:
int add = dight[4]+digit[6];
2D Array
How about multi-dimensional arrays?
array2D:
[0][0] | [0][1] | [0][2] | [0][3] | [0][4] | [0][5] |
[1][0] | [1][1] | ... | ... | ... | ... |
[2][0] | ... | ... | ... | ... | ... |
[3][0] | ... | ... | ... | ... | ... |
[4][0] | ... | ... | ... | ... | [4][5] |
For example:
The above array is declared by:
int array2D[6][5]
Similarly, you can write array2D[1][2] for accessing the data located at the 3rd element of the 2nd row.
However, you should never attempt to access data past
the last element of the array, such as when
you have a 10-element array, and you try to access the 11th element. The memory
for the array
that was allocated for it will only be 10 locations in memory, but the 11th
could be anything, which could crash your computer.
When we write programs with 2D array, we often use for-loop to access the it, consider the code below:
#include <iostream.h>
int main()
{
int x, y, array2D[8][8], i;//declares
a 8x8 array
for(x=0; x<8; x++)
{
for(y=0; y<8; y++)
{
array2D[x][y]=i++;//sets the
element to zero;
}
}
for(x=0; x<8;x++)
{
for(y=0; y<8; y++)
{
cout<<array2D[x][y]<<"
";
}
cout << endl;
}
return 0;
}
Try to read the code carefully and answer the following questions:
|
|
In learning programming languages, it is important to try it by yourselves, try to edit the program above in C++ and figure out the answer of the questions and other usage of array.
For example, try to change the line cout<<array2D[x][y]<<" "; in the last for-loop to cout<<array2D[y][x]<<" ";
Functions
The advantages of using function:
Parameter passing
If you want to pass parameter to a function, there will be two type of parameter passing methods. One is pass by value, another is pass by reference.
pass by value : C++ will create a duplicate for the variable. All changes make inside the function will only be done only be duplicated variable, not the one passing in
pass by reference : what changes make inside the function will be done on the variable passing in, no duplication will be created
The following example shows the difference between these two methods.
pass by value - a, b
pass by reference - result
void add(int a, int b, int& result) {
a = b;
result = a + b;
}
void main(){
int a = 3, b = 5, result = 0;
add(a,b,result);
}
The value of a, b, result will be 3,5,10. Whatever operation you had done in the function, if the parameter is passing by value, the value of the parameter remain the same after calling the function. However, the parameter which is pass by reference, the value of it will change after calling the function.
Recursion
In order to write a recursive function, you need to consider the following 2 case :
1. base case
2. general case
Then, combine them into a function.
int recursive_function(parameter){
// base case
if (base case condition meet)
return the base value;
// general case
return recursive_function(revised parameter);
}
Write a recursive function that counts the number of zero digits in a non-negative integer.
int zeros(int num){
if (num == 0)
return 1;
if (num < 10)
return 0;
if (num % 10 == 0)
return 1 + zeros(num/10);
else
return zeros(num/10);
}
Lab Task for String Manipulation:
Task: Interleaving String
You are required to check a string is the interleaving of two other strings by recursive function.
Given s1,s2,s3, you need to check whether s3 is constructed by interleaving s1 and s2.
For example:
Given s1 = "aabcc", s2 = "bbdcca",
When s3 = "aabbdcbccac": return true.
When s3 = "aadbbbaccc": return false.
Test case:
checkinterleaving(s1,s2,s3);
Write a recursive function to output the identification as above.