Arrays

Arrays

Array declaration

#include <iostream>
using namespace std;
int main() {

  int arr[100]; // declaration of an array 
                // of integers of size 100
  // The elements of 'arr' are denoted by
  // arr[0], arr[1], arr[2], ... arr[99]
  
  // We can assign values to the elements of the array:
  arr[0] = 15;   arr[1] = 26;
  arr[10] = 71;  arr[99] = 27; 

  // and we can access them:
  int x = arr[1];
  int y = arr[2*4 + 2];
  arr[98] = arr[99] - arr[0] + 1; 
}

Array initialization

#include <iostream>
using namespace std;

const int N = 1000;
int main() {

  char c1[5] = {'A', 'T', 'G', 'C', 'T'};
  
  char c2[5] = {'Z'}; // the first element is set equal 
                      // to 'Z', and the other four to '\0'

  char c3[]  = {'@', '#', '$', '%', '^', '&', '*'};
  // here, the size of the declared array is 7, and 
  // it's determined from the given list of characters
  
  char c4[N] = {'A'}; // using an integer constant
                      // to specify the size
}

Example: Create an array of 25000 integers and initialize it with random values in the interval 0 <= x <= 999.

#include <iostream>
#include <cstdlib>
using namespace std;

const int N = 25000;
int main() {
  srand(time(NULL));

  int arr[N] = {0};
  for(int i = 0; i < N; i++){
    arr[i] = rand() % 1000;
  }
}

Decide if an array of integers contains the number 123

#include <iostream>
#include <cstdlib>
using namespace std;

const int N = 25000;

int main() {
  // initialize the array
  int arr[N] = {0};
  // fill it with random numbers
  for (int i = 0; i < N; i++) {
    arr[i] = rand() % 1000;
  }
  // check if the array contains 123
  bool contains = false;
  for(int i = 0; i < N; i++) {
    if (arr[i] == 123) { contains = true; break; }
  }
  // here, contains is true or false
}

A solution in the form of a function:

const int N = 25000;

bool contains_123(int arr[N]) {

  for(int i = 0; i < N; i++) {
    // if 123 is found, return true immediately
    if (arr[i] == 123) return true; 
  }
  // otherwise, if 123 was not found:
  return false;
}

Find the largest element in an array

const int N = 25000;

// Finds the largest element of the array
// It assumes that the array length N >= 1.
int largest(int arr[N]) {

  int max = arr[0];
  for(int i = 0; i < N; i++) {
    if (arr[i] > max) { max = arr[i]; }
  }
  return max;

}

Reverse an array

const int N = 5000;

// swaps elements i and j
void swap(int arr[N], int i, int j) {
  int tmp = arr[i];
  arr[i] = arr[j];
  arr[j] = tmp;
}
// reverse the array (updates the array in-place)
void reverse(int arr[N]) {
  for(i = 0; i < N/2; i++) {
    swap(arr, i, N-i);
  }
}

Compact an array

#include <iostream>
using namespace std;

const int N = 10;

// print the array
void print(int arr[N]);

// compact the array
// The function pushes all nonzero elements 
// to the beginning of the array, preserving 
// their original ordering.
void compact(int arr[N]);

int main() {
  int data[N] = {4, 1, 0, 5, 2, 0, 0, 3, 0, 7};
  cout << "Original data:  ";
  print(data);

  compact(data);
  cout << "Compacted data: ";
  print(data);
}
  
void compact(int arr[N]) {
  int i = 0;
  int idst = 0;
  while (i < N) {
    if (arr[i] != 0) {
      // swap elements i and idst
      int t = arr[i];
      arr[i] = arr[idst];
      arr[idst] = t;
      idst++;
    }
    i++;
  }
}

void print(int arr[N]) {
  for (int i = 0; i < N; i++) 
    cout << arr[i] << ' ';
  cout << endl;
}
Original data:  4 1 0 5 2 0 0 3 0 7 
Compacted data: 4 1 5 2 3 7 0 0 0 0