Lecture 21

Reading

p419-421, 422-430

Additional notes on new

Consider the following example
double *array;
int size = 10;
array = new double[size];
The new operator does 2 things When you should use dynamic arrays?
One more simple example
#include<iostream>
using namespace std;

int main() {
int size;
int *squares; cout << "Enter an integer: "; cin >> size; squares = new int[size]; for(int i = 0; i < size; i++) { squares[i] = i*i; } return 0; }
What is the output of square[3] if size is 10?





One more example on returning an array
#include<iostream>
using namespace std;

// return an array with the first element is 1, the seconde element is 2 ....
int* integers(int size); int main() { int size = 20; int* result; result = integers(size); for(int i = 0; i < 20; i++) { cout << result[i] << " ";
}
cout << endl;

return 0;
}

int* integers(int size) {
int *temp = new int[size]; // create a pointer to an array
for(int i = 0; i < size; i++) {
temp[i] = i+1; // assign value } return temp; // return the pointer(array) }

Use pointer in functions


Example
#include<iostream>
using namespace std;

void increaseByOne(int* array, int size); void display(int* array, int size); int main() { int a[] = {2,4,6,8}; int size = 4; display(a,4); cout << endl; increaseByOne(a, 4);
display(a,4);
cout << endl; return 0; } void increaseByOne(int* array, int size) { for(int i = 0; i < size; i++) { array[i]++; } } void display(int* array, int size) { for(int i = 0; i < size; i++) { cout << array[i] << " "; } }







Some people like to write it in this way
#include<iostream>
using namespace std;

typedef int* IntPtr; void increaseByOne(IntPtr array, int size); void display(IntPtr array, int size); int main() { int a[] = {2,4,6,8}; int size = 4; display(a,4); cout << endl; increaseByOne(a, 4); display(a,4); cout << endl; return 0; } void increaseByOne(IntPtr array, int size) {
for(int i = 0; i < size; i++) {
array[i]++;
}
}

void display(IntPtr array, int size) {
for(int i = 0; i < size; i++) {
cout << array[i] << " ";
}
}

What is the output of the following program?
#include<iostream>
using namespace std;

void f1(double* a); void f2(double* a, double* b); void f3(double* a, double*& b); int main() { double *a, *b, x=10, y=20; a = &x; b = &y; cout << x << endl; cout << *a << endl; cout << y << endl; cout << *b << endl; cout << endl; f1(a); cout << x << endl; cout << *a << endl; cout << y << endl; cout << *b << endl; cout << endl; f2(a,b); cout << x << endl; cout << *a << endl; cout << y << endl; cout << *b << endl; cout << endl; f3(a,b); cout << x << endl; cout << *a << endl; cout << y << endl; cout << *b << endl; return 0; } void f1(double* a) { *a = 7; } void f2(double* a, double* b) { *a = 55; b = a; } void f3(double* a, double*& b) { *a = 55; b = a; }
One more example
#include<iostream>
#include<cctype>
using namespace std;

// change the string to uppercase letters
char* changeToUpper(char* s); int main() { char string[] = "abcdefghijklmnopqrstuvwxyz1234567890"; char* upper; upper = changeToUpper(string); cout << string << endl << upper << endl; return 0; } char* changeToUpper(char* s) { int length = strlen(s); // the length of the string char* result = new char[length+1]; // dynamically created an array for(int i = 0; i < length; i++) { result[i] = toupper(s[i]); // return the uppercase letter of s[i] } result[length] = '\0'; // don't forget the null character return result; }

Output
abcdefghijklmnopqrstuvwxyz1234567890
ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890