Week 5 (CS 200)

Table of Contents

For loops, arrays, vectors

🏠 Back to home page

1. Class schedule

  • Tuesday: For loops, arrays
  • Thursday: Lab

2. Assignment links

Assignment Reading link Canvas link
📚 Intro: For loops Read Canvas quiz
📚 Intro: Traditional arrays Read Canvas quiz
📚 Intro: STL Array and STL Vector Read Canvas quiz
💾 Lab: For loops and arrays Read Canvas assignment

3. Example code

3.1. For loops

3.1.1. Basic for loops

#include <iostream>
using namespace std;

int main()
{
    // Add 1 each time
    cout << " i++" << endl;
    for ( int i = 0; i < 10; i++ )
    {
        cout << i << ",";
    }

    // Add 2 each time
    cout << "\n\n i += 2" << endl;
    for ( int i = 0; i < 10; i += 2 )
    {
        cout << i << ",";
    }

    // Subtract 1 each time
    cout << "\n\n i--" << endl;
    for ( int i = 10; i > 0; i-- )
    {
        cout << i << ",";
    }

    // multiply by 2 each time
    cout << "\n\n i *= 2" << endl;
    for ( int i = 2; i < 100; i *= 2 )
    {
        cout << i << ",";
    }

    return 0;
}

3.1.2. Student list string

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

int main()
{
    int totalStudents;
    string studentNames = "";
    string name;

    cout << "How many students? ";
    cin >> totalStudents;

    for ( int i = 0; i < totalStudents; i++ )
    {
        cout << "Enter name of student " << i << ": ";
        cin >> name;

        studentNames += name + " ";
    }

    cout << endl;
    cout << "Students: " << studentNames << endl;

    return 0;
}

3.2. Traditional array

3.2.1. Bank accounts

#include <iostream>
using namespace std;

int main()
{
    float balances[10]; // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    for ( int i = 0; i < 10; i++ )
    {
        balances[i] = 0;
    }

    bool done = false;
    while ( !done )
    {
        int acct;
        cout << "What is your account number? ";
        cin >> acct;

        if ( acct == -1 )
        {
            for ( int i = 0; i < 10; i++ )
            {
                cout << "Balance for account #" << i << " = $"
                     << balances[i] << endl;
            }
        }

        while ( acct < 0 || acct > 9 )
        {
            cout << "Invalid account number." << endl;
            cout << "What is your account number? ";
            cin >> acct;
        }

        cout << endl << "BANK" << endl;
        cout << "1. Deposit" << endl;
        cout << "2. Withdraw" << endl;
        cout << "3. Quit" << endl;
        cout << "Balance: $" << balances[acct] << endl;
        cout << ">> ";
        int choice;
        cin >> choice;

        if ( choice == 1 )
        {
            float amount;
            cout << "How much to deposit: ";
            cin >> amount;
            balances[acct] += amount;
        }
        else if ( choice == 2 )
        {
            float amount;
            cout << "How much to withdraw: ";
            cin >> amount;
            balances[acct] -= amount;
        }
        else
        {
            cout << "Goodbye" << endl;
        }

        cout << "Transaction done. Logging out" << endl << endl;
    }

    return 0;
}

3.3. STL Vector

3.3.1. Cities

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    vector<string> cities;

    cities.push_back( "Raytown" );
    cities.push_back( "Lee's Summit" );
    cities.push_back( "Independence" );

    cout << "\n Begin" << endl;
    for ( unsigned int i = 0; i < cities.size(); i++ )
    {
        cout << i << "\t" << cities[i] << endl;
    }

    cities.erase( cities.begin() + 1 );

    cout << "\n After erase" << endl;
    for ( unsigned int i = 0; i < cities.size(); i++ )
    {
        cout << i << "\t" << cities[i] << endl;
    }

    cities.clear();

    cout << "\n After clear" << endl;
    for ( unsigned int i = 0; i < cities.size(); i++ )
    {
        cout << i << "\t" << cities[i] << endl;
    }

    return 0;
}

3.4. Arrays and structs

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

#include "Ingredient.h"

int main()
{
    // Using a traditional array
    const int ING_AMOUNT = 3;
    Ingredient rec1Ingredients[ING_AMOUNT];

    rec1Ingredients[0].name = "Popcorn";
    rec1Ingredients[0].unit = "bag";
    rec1Ingredients[0].amount = 1;

    rec1Ingredients[1].name = "Butter";
    rec1Ingredients[1].unit = "stick";
    rec1Ingredients[1].amount = 0.5;

    rec1Ingredients[2].name = "Powdered cheese";
    rec1Ingredients[2].unit = "tbsp";
    rec1Ingredients[2].amount = 3;

    cout << "RECIPE 1: POPCORN" << endl;
    for ( int i = 0; i < ING_AMOUNT; i++ )
    {
        cout << rec1Ingredients[i].amount << " "
             << rec1Ingredients[i].unit << " of "
             << rec1Ingredients[i].name << endl;
    }

    // Using a vector
    vector<Ingredient> rec2Ingredients;

    rec2Ingredients.push_back({.name = "Bread", .unit = "slices", .amount = 2});
    rec2Ingredients.push_back({.name = "Peanut Butter", .unit = "tbsp", .amount = 2});
    rec2Ingredients.push_back({.name = "Jelly", .unit = "tbsp",   .amount = 1});

    cout << endl << "RECIPE 2: Sandwich" << endl;
    for ( size_t i = 0; i < rec2Ingredients.size(); i++ )
    {
        cout << rec2Ingredients[i].amount << " "
             << rec2Ingredients[i].unit << " of "
             << rec2Ingredients[i].name << endl;
    }

    cout << endl << "RECIPE 2: Sandwich (again)" << endl;
    // Another way to loop!
    for ( Ingredient ing : rec2Ingredients )
    {
        cout << ing.amount << " "
             << ing.unit << " of "
             << ing.name << endl;
    }

    return 0;
}

3.5. C-Strings

3.5.1. String size and copy - strlen & strcpy

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

int main()
{
    char name[10] = "Mhuswys";
    char job[] = "Mage";

    cout << endl << "VARIABLE 1 A" << endl;
    cout << "name:   " << name << endl;
    cout << "strlen: " << strlen( name ) << endl; // 7
    cout << "bytes:  " << sizeof( name ) << endl; // 10

    cout << endl << "VARIABLE 2 A" << endl;
    cout << "job:    " << job << endl;
    cout << "strlen: " << strlen( job ) << endl; // 4
    cout << "bytes:  " << sizeof( job ) << endl; // 5

    strcpy( name, "Limnos" );
    strcpy( job, "War" );

    cout << endl << "VARIABLE 1 B" << endl;
    cout << "name:   " << name << endl;
    cout << "strlen: " << strlen( name ) << endl; // 6
    cout << "bytes:  " << sizeof( name ) << endl; // 10

    cout << endl << "VARIABLE 2 B" << endl;
    cout << "job:    " << job << endl;
    cout << "strlen: " << strlen( job ) << endl; // 3
    cout << "bytes:  " << sizeof( job ) << endl; // 5

    return 0;
}

3.5.2. String compare - strcmp

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

int main()
{
    char text1[] = "bat";
    char text2[] = "cat";

    cout << "text1: " << text1 << endl;
    cout << "text2: " << text2 << endl;
    cout << endl;
    cout << "text1 vs. text2: " << strcmp( text1, text2 ) << endl; // -1
    cout << "text2 vs. text1: " << strcmp( text2, text1 ) << endl; // 1
    cout << "text1 vs. text1: " << strcmp( text1, text1 ) << endl; // 0

    return 0;
}

3.5.3. String concatenation - strcat

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

int main()
{
    char city[] = "Overland Park";
    char state[] = "KS";
    char zip[] = "66210";

    char address2[40] = "";
    strcat( address2, city );
    strcat( address2, ", " );
    strcat( address2, state );
    strcat( address2, ", " );
    strcat( address2, zip );

    cout << address2 << endl;

    return 0;
}

Author: Moose (R.W.) Singh

Created: 2026-10-14 Wed 08:55

Validate