Unit 6 - Arrays - Elementary Statistics return to unit course outline

Using arrays as function arguments:

Arrays can be passed down to functions like other single variables. We use them by their name as arguments when we call the function. The parameter list of the function must indicate the fact that a parameter is an array by augmenting the [ ] at the end of the one dimensional arrays. In one-dimensional arrays the actual size does not have to be given. In case of two-dimensional arrays we recommend that you give the size of the array in both dimensions.

NOTE: Arrays are always passed by name. In other words if we change the array in the function, then this change reflects back to the calling function.

Read the explanations provided for the example .

Place your mouse in the areas below to see comments about the corresponding sections of code  
Code
 
 /*
      J.Y.Kazakia October 14,1998
 
This example illustrates the following:
 
1) Arrays in C++.
 
2) Arrays passed to a function return their updated values to the main.
 
Here a set of four numbers is passed to a function called square_them first individually and then as an array. The results in both cases are then compared. The function forms the squares of the numbers.
 
                                */
#include <iostream.h>
#include <fstream.h>
 
  /* function prototypes */
 
void square_them ( float a, float b, float c, float d,float array[]);      
 
ofstream out( "6ex3rep.txt" , ios:: out);
 
main ( )
 
{
 
float a, b, c, d;
float array[4];
int i;
 
a = 1.1;
b = 2.2;
c = 3.3;
d = 4.4;
 
for ( i = 0; i <= 3; i++)
array [ i ] =(1+ i) +( i +1 ) / 10.;
 
/* this is equivalent to writing: array[0] = 1.1;
                                  array[1] = 2.2;
                                  array[2] = 3.3;
                                  array[3] = 4.4;
 
   Print before you call the function    */
out << " \n\n\n  Printing before  calling the function";
out << endl << endl;
out << " a =" << a << " b =" << b << " c =" << c ;
out <<" d =" << d ;
 
for ( i = 0; i <= 3; i++)
out <<"\n\n   array["<<i<<"] = "<<array[i];
 
/*****************************************
 
   Call the function
 
*****************************************/
 
square_them( a, b, c, d, array );
 
/*   Print after  you call the function    */
 
out << " \n\n\n  Printing after  calling the function";
out << endl << endl;
out << " a =" << a << " b =" << b << " c =" << c ;
out <<" d =" << d ;
 
for ( i = 0; i <= 3; i++)
out <<"\n\n   array["<<i<<"] = "<<array[i];
 
 
}
 
 void square_them( float a, float b, float c, float d, float u[])
 
{
int i;
 
a = a * a;
b = b * b;
c = c * c;
d = d * d;
 
i=0;
while (i <= 3)
  {
     u[i] = u[i] * u[i];
     i++;
 
/*   Print from within the function    */
 
out << " \n\n\n  Printing from within the function";
out << endl << endl;
out << " a =" << a << " b =" << b << " c =" << c ;
out <<" d =" << d ;
 
for ( i = 0; i <= 3; i++)
out <<"\n\n   array["<<i<<"] = "<< u[i];
 
}
 
THE OUTPUT IS:
 
  Printing before  calling the function
 
 a =1.1 b =2.2 c =3.3 d =4.4
 
   array[0] = 1.1
 
   array[1] = 2.2
 
   array[2] = 3.3
 
   array[3] = 4.4
 
 
  Printing from within the function
 
 a =1.21 b =4.84 c =10.89 d =19.36
 
   array[0] = 1.21
 
   array[1] = 4.84
 
   array[2] = 10.89
 
   array[3] = 19.36
 
 
  Printing after  calling the function
 
 a =1.1 b =2.2 c =3.3 d =4.4
 
   array[0] = 1.21
 
   array[1] = 4.84
 
   array[2] = 10.89
 
   array[3] = 19.36
 
 
   */

 

(text file of the above code - 6ex3)

(printable file)

© 2001 J.Y. Kazakia. All rights reserved