Array Functions for Software Engineering
Array Functions for Software Engineering
Passing
Arrays to
Functions
COMP102 Prog. Fundamentals I: Passing Arrays to Function / Slide 2
Smallest Value
● Problem
■ Find the smallest value in a list of integers
● Input
■A list of integers and a value indicating the
number of integers
● Output
■ Smallest value in the list
● Note
■ List remains unchanged after finding the
smallest value!
COMP102 Prog. Fundamentals I: Passing Arrays to Function / Slide 4
Preliminary Design
● Realizations
■ When looking for value with distinguishing
characteristics, need a way of remembering best
candidate found so far
■ Best written as a function - likely to be used often
● Design
■ Search array looking for smallest value
– Use a loop to consider each element in turn
– If current element is smallest so far, then update smallest
value so far candidate
■ When done examining all of the elements, the smallest
value seen so far is the smallest value
COMP102 Prog. Fundamentals I: Passing Arrays to Function / Slide 5
Necessary Information
● Information to be maintained
■ Array with values to be inspected for smallest
value
■ Number of values in array
■ Index of current element being considered
■ Smallest value so far
COMP102 Prog. Fundamentals I: Passing Arrays to Function / Slide 6
Using ListMinimum
int List[3];
List[0] = 9; List[1] = 12; List[2] = 45;
cout << ListMinimum(List, 3) << endl;
COMP102 Prog. Fundamentals I: Passing Arrays to Function / Slide 9
GetList(Values,MaxSize );
DisplayList(Values, MaxSize);
COMP102 Prog. Fundamentals I: Passing Arrays to Function / Slide 11
int main() {
int A[10] = {9,8,7,6,5,4,10,2,1,0};
cout << “The maximum element of this array is: ”
<< A[max_element(10,A)] << endl;
return 0;
}
//Example 1:passing array elements to a function
#include <iostream>
using namespace std;
void print_square (int);
const int ARRAY_SIZE = 5;
int main(){
int index;
int base[ARRAY_SIZE] = {3, 7, 2, 4, 5};
return sum/size;
}
COMP102 Prog. Fundamentals I: Passing Arrays to Function / Slide 16
Example 5
// Add a[i] and b[i] and store the sum in c[i]
void add_array(int size, // in: array size
double a[], // in: first array
double b[], // in: second array
double c[] ) // out: result array
// array elements with subscripts ranging from
// 0 to size-1 are added element by element
// Pre: a[i] and b[i] (0<=i<=size-1) are defined
// Post: c[i] = a[i] + b[i] (0<=i<=size-1)
{ int i;
// Add a[i] and b[i] and store result in c[i]
for (i=0; i < size; i++)
c[i] = a[i] + b[i];
}
COMP102 Prog. Fundamentals I: Passing Arrays to Function / Slide 17
Example 5
int main() {
const int size = 5;
double x[size] = {1.8, 2.2, 3.4, 5.1, 6.7},
y[size] = {2.0, 4.5, 1.3, 4.0, 5.5},
z[size];
int ind;
add_array(size , x, y, z);
cout << "Content of array z is: \n";
for (i = 0; i < size; i++)
cout << "z[" << i << "] is "
<< z[i] << endl;
return 0;
}
COMP102 Prog. Fundamentals I: Passing Arrays to Function / Slide 18
add_array (5, x, y, z );
COMP102 Prog. Fundamentals I: Passing Arrays to Function / Slide 19
Example 6
// Sum up two 2-dimensional arrays into a third one
#include <iostream>
using namespace std;
const int max_cols = 5;
// c[i][j] = a[i][j] + b[i][j]
void add_array(double a[][max_cols],
double b[][max_cols],
double c[][max_cols],
int rows)
{ int i, j;
for (i=0; i < rows; i++)
for (j=0; j < max_cols; j++)
c[i][j] = a[i][j] + b[i][j];
}
int main() {
const int max_rows = 2;
double a[max_rows][max_cols] = {{1.8, 2.2, 3.4, 5.1, 6.7},
{1.0, 2.0, 3.0, 5.0, 6.0}},
b[max_rows][max_cols] = {{0.2, -0.2, -1.4, -3.1, -4.7},
{1.0, 0.0, -1.0, -3.0, -4.0}},
c[max_rows][max_cols];
int i, j;
add_array(a, b, c, max_rows);
// fix how decimals are shown
cout.setf(ios::fixed); // use decimal notation
cout.setf(ios::showpoint); // show decimals
cout.precision(1); // one decimal place
cout << "Content of array c is: \n";
for (i = 0; i < max_rows; i++){
for (j=0; j < max_cols; j++)
cout << c[i][j] << ", ";
cout << endl;
}
return 0;
}
COMP102 Prog. Fundamentals I: Passing Arrays to Function / Slide 22
Pass-by-Reference
void m(int, int []);
int main()
{
int x = 1; // x represents an int value
int y[10]; // y represents an array of int values
y[0] = 1; // Initialize y[0]
Reverse function
return 0;
newList
cout << c[i] << endl;
6 5 4 3 2 1
}
COMP102 Prog. Fundamentals I: Passing Arrays to Function / Slide 25
● //CountLettersInArray.cpp