1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include <iostream> //allow access to cout, etc, standard c++ header
#include <iomanip> //allows access to formatting for cout, usually floating point printing.
using namespace std; //a bad idea, see 1000 threads on topic in the forums, this allows you to skip typing std:: on cout etc.
void modifyArray (int [], int); // a function header, this tells the compiler it exists before it sees the code for it, so it won't crash parsing main which uses the function before it sees it (it parses the file top to bottom).
void modifyElement (int); //as above.
int main() //the main program, starting point.
{
const int size = 5; // define size as the value 5.
int a[size]; //create an array, called 'a', with size (5) elements.
for (int i = 0; i < size; i++) //loop 0,1,2,3,4 (5 times)
a[i] = i * i; //assigns a the values of the squares of i (0, 1, 4, 9, 16)
modifyArray (a, size); //calls your function. changes a.
cout << "a[1] = " << setw(3) << a[1] << endl; //writes out the second location in a
modifyElement (a[1]); //calls your function. does nothing.
cout << "a[1] = " << setw(3) << a[1] << endl; // as above.
return 0;
}
void modifyArray(int b[], int s) // this function takes an array and an integer. you called it with a and size.
{
for (int k = 0; k < s; k++) //for all the values in a (this is 0-size like the loop in main to initialize the array)
b[k] += 2; // each element of the array has 2 added to it. so it went from 0,1,4,9,16 to 2,3,6,11,18
}
void modifyElement(int d)
{
d *= 2; //d which is a[1] which is 3 is multipled by 2, but this is only done to a local copy, so it does not change anything. no real effect.
}
|