Array arguments


int scores[max_scores];

 //call of function that inputs into array of ints arg.
load_scores (scores);  
-----------------------------
//definition of the load_scores function
void load_scores (int A[]) { 	
              //formal arg A is array of ints.
              //Note size of 1st dim.is not specified.
//any value is ignored by compiler. 
//Function does not know physical size of 1D array argument

  int i;
  cout << "Enter " << max_scores << " values: ";
  for (i=0; i< max_scores; i++)
    cin >> A[i];    //inputs into the actual arg array element
}

Prototype:
 void load_scores (int []);    //must have the brackets.
-------------------------------------------------------


A better function would have size as an argument:

load_scores(scores,max_scores);

Or:

cout << "Enter number of scores: ";
cin >> num_scores;  //num_scores is the virtual size of the array
load_scores(scores,num_scores);

   
//new definition of the improved function
void load_scores (int A[], int size) {
  int i;
  for (i=0; i< size; i++)
    cin >> A[i];
}



-------------------------------------------------------

//function to search an array of ints of any size for any value
int search_array (int arr[], int length, int item) {
  int i=0;
  bool found=false;
  while (i< length && !found)  //not at end and not yet found
    if (arr[i] == item)
      found = true;            //found it, drop out of loop
    else 
      i++;  
  if (found)
    return i;          //return the index where it is
  else
    return -1;         //return non-index to indicate not in array
}


Prototype:
int search_array (int [], int, int);

Use:
int scores[max_scores];
load_scores(scores,max_scores);
if (search_array(scores,max_scores,100) != -1)
  cout << "A 100 exists in the array";
else
  cout << "No 100's";


int ind;
//return value of search_array assigned to ind, then compared to -1
if ((ind=search_array(scores,max_scores,100)) != -1)
  cout << "A 100 exists at element " << ind;
else
  cout << "No 100's";




-------------------------------------------------------

//function to search an array of ints of some size for some value
// return count of number of occurences
int search_array (int arr[], int length, int item) {
  int i, count=0;
  for (i=0; i< length; i++)  
    if (arr[i] == item)
      count++;          
  return count;         
}


int scores[max_scores];
load_scores(scores,max_scores);
cout << "Enter value to search for: ";
cin >> val;
int val_count = search_array(scores,max_scores,val);
cout << "Number of " << val << " is " << val_count;




-----------------------------------------------------
//function to find maximum value in an array of ints
int max_value (int values[], int size) {
  int i, largest=values[0];
  for (i=1; i< size; i++)
    if (values[i] > largest)
      largest = values[i];
  return largest;
}


int scores[max_scores];
cout << "Enter number of scores: ";
cin >> num_scores;
load_scores(scores,num_scores);
cout << "Max score is "<< max_value(scores,num_scores);




-------------------------------------------------------

//function to read up to max_size floats into array of floats; 
//return size/length
int load_vector (float vector[]) {
  int i, num;
  cout << "Enter number of values to be input: ";
  cin >> num;
  if (num <= max_size) {      //check is within bounds
    for (i=0; i< num; i++)
      cin >> vector[i];
    return num;
  }
  else {
    cout << "Error.  Too large";
    return 0;
  }
}


float V[max_size];
int size;
size = load_vector(V);



-------------------------------------------------------

//function to compute average of an array of floats
float average (float vector[], int length) {
  int i;
  float total=0;
  for (i=0; i< length; i++)
    total += vector[i];
  return total / length;
}


//continue from above code
cout << "Average value: " << average(V,size);

Exercises: write functions to:
to append inputted data to the end of an array
remove element

Program to compute average fuelings. fueling.cpp
Program to generate random numbers into array, then check for duplicates. arraydups.cpp
program to generate radnom numbers into array, then sort them. sort140.cpp

Next (2D arrays)


©David Wills