/* C++ Arrays and Pointers Demo

Copyright © Robin Broad 2018

Free software published under GNU GPL

http://www.robinbroad.co.uk/

May 2018

*/

 

//Preprocessor instructions to the C++ compiler

//Include the standard C input/ output library

/*This code is heavily commented to assist students of C++

Author: Robin Broad

May 2018

*/

#include <iostream>

//Use the standard names in namespace std for the functions in the iostream library (e.g. cout). Without this, the compiler would not recognise the function cout, which we are using in this program

using namespace std;

//Include the standard C maths library. We need it to calculate the square [pow(x,2)] of the array elements

#include<math.h>

 

//*** Defining Functions ***

void printArray(int passedArrayReference[], int arraySize){

        //This function will print out all of the elements of the array passed to it

        //The array is passed by reference to the function. This means that the function does not receive a copy of the array, but a reference to it. Other arguments are passed by value and cannot be altered in the main program by the function.

        //Void means that this function does not return a value

        for (int i = 0; i < arraySize; ++i) {

                cout << passedArrayReference[i];

                if (i< arraySize-1) cout << ", ";

                //We don't need a comma after the last number printed

        }

        cout <<"\n";

}

 

void fillArrayWithSequence(int passedArrayReference[], int arraySize){

        //This function will fill the array with a sequence of integers starting from 1

        //The array is passed by reference to the function. This means that the function does not receive a copy of the array, but a reference to it. Other arguments are passed by value and cannot be altered in the main program by the function.

        //Void means that this function does not return a value

        for (int i = 0; i < arraySize; ++i) {

                passedArrayReference[i]=i+1;

                //+1 because we want to start at 1, not 0

        }

}

 

void squareArrayElements(int passedArrayReference[], int arraySize){

        //This function will square the elements of the array

        //The array is passed by reference to the function. This means that the function does not receive a copy of the array, but a reference to it. Other arguments are passed by value and cannot be altered in the main program by the function.

        //Void means that this function does not return a value

        for (int i = 0; i < arraySize; ++i) {

                passedArrayReference[i] = pow(passedArrayReference[i],2);

                //pow(x,2) means calculate the square (power of 2) of the number

        }

}

 

void arrayPointerEdit(int *passedArrayPointer, int arraySize){

        //Squaring the elements in the array again, this time using a pointer

        //Void means that this function does not return a value

        for (int i = 0; i < arraySize; ++i) {

                cout << "Overwriting contents of memory address: " << passedArrayPointer <<"\n";

                *passedArrayPointer= pow(*passedArrayPointer,2);

                //pow(x,2) means calculate the square (power of 2) of the number

                passedArrayPointer++;

        }

}

 

//*** Main program ***

int main() {

 

        //Output Header

        cout <<"C++ Arrays and Pointers Demo\nRobin Broad\nMay 2018\n";

        /*This program demonstrates passing arrays to functions by reference, passing pointers to arrays to functions and changing the contents of a memory address using a pointer*/

 

        //Declaring variables

        int arraySize=10;

        int arrayA[arraySize] = {0};

        //This declares the array as an array of integers and initialises all of the elements of the array to zero

 

        //Declaring pointers

        int *pointerToArrayA=arrayA;

        //The * is the dereference operator which is used to declare that this is a pointer. It is assigned to hold the start address of the array. It's type should also match the type of the data that it points to.

        int *pointerToArraySize=&arraySize;

        //The * is the dereference operator which is used to declare that this is a pointer. It is assigned to hold the memory address of the variable arraySize. It must have the same type as the data it points to.

        //The & is the reference operator which gives us the memory address of the variable arraySize

 

        //Call a function to print the array

        cout << "Zero initialised array:\n";

        printArray(arrayA,arraySize);

 

        //Call a function to fill the array with a sequence of integers starting from 1

        fillArrayWithSequence(arrayA,arraySize);

        cout << "Integer sequence array:\n";

        printArray(arrayA,arraySize);

 

        //Call a function to square the elements of the array

        squareArrayElements(arrayA,arraySize);

        cout << "Squared elements array:\n";

        printArray(arrayA,arraySize);

 

        //Call a function to square the elements in the array again, this time using a pointer

        cout << "Squaring the elements in the array again, this time using a pointer:\n";

        arrayPointerEdit(pointerToArrayA,arraySize);

        cout << "Reprinting the array:\n";

        printArray(arrayA,arraySize);

 

        //Manipulate the array size by changing the contents of a memory address

        cout << "The arraySize variable is stored at memory address: " << pointerToArraySize << " and holds the value " << *pointerToArraySize <<"\n";

        cout << "Manipulating contents of memory address: " << pointerToArraySize<<"\n";

        *pointerToArraySize=5;

        cout << "Contents of memory address: " << pointerToArraySize << " is now " << *pointerToArraySize<<"\n";

        cout << "Printing the array again, just to check:\n";

        printArray(arrayA,arraySize);

        return 0;

}

 

/*This program produced the following output when compiled using the GNU GCC C++ compiler from within the Code::Blocks IDE running on an Ubuntu GNU/Linux operating system, by Robin Broad, Newcastle upon Tyne, UK, May 2018:

 

C++ Arrays and Pointers Demo

Robin Broad

May 2018

Zero initialised array:

0, 0, 0, 0, 0, 0, 0, 0, 0, 0

Integer sequence array:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Squared elements array:

1, 4, 9, 16, 25, 36, 49, 64, 81, 100

Squaring the elements in the array again, this time using a pointer:

Overwriting contents of memory address: 0xbfefe7d0

Overwriting contents of memory address: 0xbfefe7d4

Overwriting contents of memory address: 0xbfefe7d8

Overwriting contents of memory address: 0xbfefe7dc

Overwriting contents of memory address: 0xbfefe7e0

Overwriting contents of memory address: 0xbfefe7e4

Overwriting contents of memory address: 0xbfefe7e8

Overwriting contents of memory address: 0xbfefe7ec

Overwriting contents of memory address: 0xbfefe7f0

Overwriting contents of memory address: 0xbfefe7f4

Reprinting the array:

1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000

The arraySize variable is stored at memory address: 0xbfefe808 and holds the value 10

Manipulating contents of memory address: 0xbfefe808

Contents of memory address: 0xbfefe808 is now 5

Printing the array again, just to check:

1, 16, 81, 256, 625

 

Process returned 0 (0x0)   execution time : 0.015 s

Press ENTER to continue.

*/

 

/*

        Copyright © Robin Broad 2018

        This program is free software: you can redistribute it and/or modify

        it under the terms of the GNU General Public License as published by

        the Free Software Foundation, either version 3 of the License, or

        (at your option) any later version.

 

        This program is distributed in the hope that it will be useful,

        but WITHOUT ANY WARRANTY; without even the implied warranty of

        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

        GNU General Public License for more details.

 

        GNU General Public License: https://www.gnu.org/licenses/gpl.html

*/