--- Arrays, Strings and Loops β€’ C++ Cabinet

Arrays, Strings and Loops

Arrays

  • Arrays are SEQUENCES of identically typed variables.
  • Array types include the contained type and the number of contained elements.
  • You weave the information together in the declaration >>> int my_array[100]
  • You can use a shortcut to initialize an array >>> int my_second_array[] = { 1, 2, 3 ,4 ,5 }
  • You can omit the length of the array because C++ can infer the number of elements in the braces at compile time.

For Loops

  • A for loop lets you iterate the execution of a statement a specified number of times.
  • You can stipulate a starting point and other conditions.
  • The INIT STATEMENT executes before the first iteration executes, so you can initialize variables used in the for loop.
  • The CONDITIONAL is an expression that is evaluated before each iteration.
    • If it evaluates to true > the loop proceeds. If it evaluates to false > the loop terminates.
  • The ITERATION STATEMENT executes after each iteration, which is useful when you must increment a value to cover a range of values.
    • It’s commonly i++ in loops where i is the iterated variable.
  • The for loop syntax is as follows:
for(init_statement, conditional, iteration_statement){
    -- do thing --
}

Range-based For Loops

  • You can eliminate the iterator variable i by using a range-based for loop.
  • For certain objects like arrats, for understands how to iterate over the range of values within an object.
  • The syntax for a range-based for loop is as follows:
for(element-type elenent-name : array-name){
    -- do thing --
}
  • This code is generally regarded as an improvement on the standard for-loop for readability and speed purposes.

Number of Elements In An Array

  • You can use the sizeof() operator to obtain the total size (in bytes) of an array.
  • You can use a trick to determine the number of elements in an array:
    • You divide the SIZE OF THE ARRAY by the SIZE OF A SINGLE CONSITUENT ELEMENT.
    • For example:
short array[] = {104, 105, 32, 98, 105, 108, 108, 0};
size_t n_elements = sizeof(array) / sizeof(short);
  • This trick is in wide use in older C++ code.
  • If you must use an array, you can also safely obtain the number of elements using the std::function available in the header.

Strings

  • Strings are contiguous blocks of characters.
  • A C-STYLE STRING or NULL-TERMINATED STRING has a 0-byte (a null) appended to the end of it to indicate the end of the string.
  • Array elements are contigious, you can thus store strings in arrays of character types.
  • The format specifier for narrow strings (char*) is %s.
  • A C++ STRING LITERAL is a sequence of characters enclosed in double quotes that creates a null-terminated array of const char stored in static storage.
  • Consecutive string literals get concatenated together, and any intervening whitespaces or newlines get ignored.
  • You can place multiple lines in your souce, and the compiler will treat them as one.
  • This is only useful for readability, when you have a long string literal that would span multiple lines in your source code.
  • using_strings() and using_strings2() are FUNCTIONALLY IDENTICAL.

Working examples

These are rendered from the source stored beside this drawer's notes.

arrays-strings-loops.cpp

raw source β†—
#include <cstdio>
#include <cstddef>

// The following declaration declares an array of 100 int objects.
int example_array1[100];

// Using curly brace syntax, this declaration declares an array of 4 objects at GLOBAL SCOPE..
int example_array2[] = { 1, 2, 3, 4};

// The purpose of this function is to demonstrate accessing array elements.
void accessing_arrays() {
    printf("The third element of the example_array2 array is %d.\n", example_array2[2]); // This is accessing the third element of the GLOBALLY scoped example_array2, at index 2.
    int example_array3[] = { 2, 3 , 4, 5}; // This array is LOCALLY scoped WITHIN the accessing_arrays function.
    printf("The third element of the example_array3 array is %d.\n", example_array3[2]); // This is accessing the third element of the LOCALLY scoped example_array3, at index 2.
    example_array3[2] = 100; // This is reassigning the value of whatever is at index 2 (currently the number 4) in example_array3 to 100.
    printf("The third element of the example_array3 array is %d.\n\n", example_array3[2]); // This should now print 100 instead of 4.

    return;
}

// The purpose of this function is to demonstrate using a for loop to find the maximum of an array.
void using_a_for_loop(){
    unsigned long maximum = 0; // Establishes a variable maximum to hold the value we're looking for.
    unsigned long values[] = { 10, 50, 20, 40, 0}; // This is the array we're looking through to find the maximum.
    /* Note in the below loop, you need to indicate the type still and use semi colons as separators - this WILL screw you up at compile time if you ignore it.
    size_t i=0 is the init atatement, i < 5 is the conditional, i++ is the iteration statement*/
    // We use size_t here because it can index any value within it, it's the technically correct option.
    for(size_t i=0; i < 5; i++){
        if(values[i] > maximum) maximum = values[i]; // Until i < 6, this loop will execute the following code, and the conditional evaluates to false.
    }
    printf("The maximum value of the values array is %lu.\n\n", maximum);

    return;
}

// The purpose of this function is to demonstate using a different, improved form of for loop, for reference purposes.
void using_the_rangebased_for_loop(){
    unsigned long maximum2 = 0;
    unsigned long values2[] = { 10, 50, 100, 60, 40};
    for(unsigned long value : values2){
        if (value > maximum2) maximum2 = value;
    }
    printf("The maximum value of the values2 array is %lu.\n\n", maximum2);

    return;
}

// Using as an example from C++ Crash Course, the following declarations are for string literals  in english and Chinese, as string literals support Unicode.
char english[] = "A book holds a house of gold.";
char16_t chinese[] = u"\u4e66\u4e2d\u81ea\u6709\u9ec4\u91d1\u5c4b";

void using_strings(){
    char house[] = "a house of gold.";
    printf("A book holds %s\n\n", house);
}

void using_strings2(){
    char house2[] = "a " "second " "house " "of " "silver.";
    printf("A second book holds %s\n\n", house2);
}




int main(){
    accessing_arrays();
    using_a_for_loop();
    using_the_rangebased_for_loop();
    using_strings();
    using_strings2();

    return 0;
}