Quiz 1 review | CMSC 240 Software Systems Development - Spring 2024

Quiz 1 review

Objective

Review the course topics and content that will be assessed on the first quiz.

Quiz Details

What to Study

Other resources:
Unix Tutorial

Text book: Programming Principles and Practice Using C++, 2nd Edition by Bjarne Stroustrup

Lectures:

Practice Questions

Know your Linux commands

Pass by value, pass by pointer, pass by reference

Consider the following C++ code.

passby.cpp

#include <iostream>
using namespace std;

void passByValue(float copyOfValue) 
{
    copyOfValue = 1234.567;
}

void passByPointer(float* addressOfValue) 
{
    *addressOfValue = 1234.567;
}

void passByReference(float& referenceToValue) 
{
    referenceToValue = 9876.543;
}

int main() 
{
    float value = 5551.212;

    passByValue(value);

    cout << value << endl;

    passByPointer(&value);

    cout << value << endl;

    passByReference(value);

    cout << value << endl;

    return 0;
}

Memory layout

MemoryLayout

Command line arguments

args.cpp

#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    if (argc != 2) // argc counts the num of CLPs
    {
        cerr << "Usage: " << argv[0]
             << " <first name>" << endl;
        exit(1);
    }

    cout << "Hello " << argv[1] << endl;

    return 0;
}

Consider the C++ program args.cpp above.

Draw the State of Stack Memory

swap.cpp

#include <iostream>
using namespace std;

// Swap two int values.
void swap(int* a, int* b)
{
    int temp = *a;   // store contents of a in temp
    *a = *b;         // put contents of b into a
    *b = temp;       // put temp a into b
    
    // Draw the state of memory here <----

}

int main()
{
    int x = 12;
    int y = 33;
    swap(&x, &y);  // pass by pointer
    cout << "x == " << x << "  y == " << y << endl;
    return 0;
}

MemoryGrid

Consider the C++ program swap.cpp above.