CSCI 235 Course Notes

website·Indexed 30 days ago

https://csu-cs.github.io/csci-235/

Course notes for CSCI 235 covering C++ programming fundamentals. Topics include basic syntax, operators, file I/O, conditional branching, loops, functions, recursion, namespaces, custom types, enums, structs, and multidimensional arrays.

Tokens
24K
Snippets
133
Records
175
Agent score
75%

What's inside CSCI 235

  1. Understand the five core concepts of program creation

    All programs are built using five fundamental logical ideas:

    1. Sequential Processing: Executing a list of instructions in a specific order.
    2. Conditional Execution: Using logic (e.g., If... then... else...) to determine which path to take.
    3. Looping or Iteration: Repeating a behavior until a specific condition is met.
    4. Problem Decomposition: Breaking a complex problem into smaller, independent sub-problems (also known as stepwise refinement or top-down design).
    5. Functions: Groups of instructions that process input to return a single result.
  2. Understand computer hardware and software components

    A programmable computer is an electronic device that retrieves, stores, performs arithmetic/logical operations on, and outputs numerical data.

    Hardware Components:

    • Input Devices: Feed data/programs (e.g., Keyboard, Mouse).
    • Output Devices: Display results (e.g., Monitor, Printer).
    • Network Interface Card (NIC): Handles both input and output.
    • Central Processing Unit (CPU): The 'brain' that carries out arithmetic and logical operations, typically as a microprocessor chip.
    • Main Memory (RAM): Primary storage consisting of an ordered sequence of memory cells, each with a unique address, containing instructions or data.
    • Secondary Storage: Permanent storage (e.g., Hard drives, Flash drives, CDs, Tapes).

    Software Types:

    • System Programs: Control the computer (e.g., Operating Systems managing memory, I/O, and storage).
    • Applications: Perform specific tasks (e.g., Word processors, Web browsers, Games).
  3. Compare programming language levels

    Programming languages have evolved from low-level machine code to high-level languages for better human readability and efficiency.

    • Machine Language: Direct binary sequences (0s and 1s).
    • Assembly Language: Uses mnemonic instructions (e.g., mov, imul) to represent machine operations.
    • High-Level Languages: Human-readable languages (e.g., C++, Java, Python, C#) that are translated into machine language by a Compiler.
  4. CSCI 235 Procedural Programming Course Overview

    CSCI 235 focuses on the procedural aspects of the C++ language. The course covers problem-solving techniques, algorithm development, program design, and testing. Key programming constructs include control structures, repetition, functions, arrays, data types, and file handling. It also addresses the history of computing and ethical issues in computing.
  5. Implement function prototypes (forward declarations)

    A function prototype is the function's heading followed by a semicolon, without the body. Prototypes inform the compiler about the function's existence and signature before it is actually defined.

    Best practices:

    1. Place prototypes before the main() function.
    2. Place full function definitions below the main() function.
    3. Precede each prototype with a comment describing its purpose and usage.
    // Multiplies two integers and returns the result
    int multiply(int a, int b);
    
    int main() {
        int res = multiply(5, 10);
        return 0;
    }
    
    int multiply(int a, int b) {
        return a * b;
    }
  6. Implement parallel arrays for multi-type data in C++

    Parallel arrays are used when you need to store complex information about an object (e.g., a person's name, age, and height) but are limited to standard arrays which only hold elements of the same data type. By using separate arrays of different types that are linked by their index, the values at name[index], age[index], and height[index] all describe the same entity.
    constexpr int COUNT = 6;
    string name[COUNT]   = { "Michael", "Jessica", "Ashley", "Christopher", "Jairy", "Dondi" };
    int age[COUNT]       = { 21, 17, 45, 44, 70, 65 };
    double height[COUNT] = { 6.7, 5.5, 5.9, 6.6, 6.5, 5.9 };
  7. Understand C++ basic elements and syntax

    C++ programs are sequences of statements designed to accomplish a task. Key concepts include:

    • Syntax: Rules specifying which statements are legal.
    • Semantics: The meaning of the instructions.
    • Tokens: The smallest individual units of a program, including special symbols, word symbols, and identifiers.
    • Special Symbols: Operators and delimiters such as +, -, *, /, ., ;, ?, ,, <=, !=, ==, >=.
  8. Use index-based return values for parallel array functions

    When writing functions to search or analyze parallel arrays, prefer returning the index of the element rather than the value itself. Returning an index allows the caller to retrieve all related information across all parallel arrays for that specific entity, whereas returning a value only provides information from one array.
    // Better: returns the index so we can find the person's name and age too
    int maxIndex(const double VALUES[], int length)
    {
        int maxIdx = 0;
        for (int index = 1; index < length; ++index)
        {
            if (VALUES[index] > VALUES[maxIdx])
                maxIdx = index;
        }
        return maxIdx;
    }
  9. Manage identifier scope in C++

    The scope of an identifier determines where it is accessible within a program.

    Types of Scope:

    • Local identifier: Declared within a function or block; accessible from the point of declaration to the end of that block.
    • Global identifier: Declared outside of all function definitions; accessible by functions or blocks if declared before the function definition.

    Key Rules:

    • Identifiers must be declared before use.
    • C++ does not support nested functions (defining one function inside another).
    • If a local identifier shares a name with a global identifier, the local one takes precedence within its block. To access the global variable in this scenario, use the scope-resolution operator ::.
    int x = 10; // Global variable
    
    void myFunction() {
        int x = 5; // Local variable
        // Access local x
        std::cout << x << std::endl; // Prints 5
        // Access global x using scope-resolution operator
        std::cout << ::x << std::endl; // Prints 10
    }
  10. Use switch statements for equality checks

    Use a switch statement instead of multiple == checks on the same variable. Indent the case labels, and indent the statements within each case one level further.
    switch (val)
    {
        case -1:
            cout << "value is -1" << endl;
            break;
        case 1:
            cout << "value is 1" << endl;
            break;
        default:
            cout << "value is neither -1 nor 1" << endl;
            break;
    }
  11. Manage function length and complexity

    To maintain readability and maintainability, avoid long functions. A function exceeding 40-50 lines is generally considered too long. If a function's purpose requires the frequent use of the word 'and' to describe, it likely performs too many tasks and should be split into smaller sub-functions. Common functionality should be extracted into helper functions to avoid repetition.
    firstName = getInfo("What is your first name?");
    lastName = getInfo("What is your last name?");
    email = getInfo("What is your email?");
    
    string getInfo(const string& PROMPT)
    {
        cout << PROMPT << endl;
        string input;
        cin >> input;
        return input;
    }
  12. Declare and initialize C++ variables

    Follow these standards for variable declarations:

    • Declare variables in the smallest scope possible.
    • Use the most specific data type (e.g., int instead of double for integers, char instead of int for characters).
    • Use true and false for bool variables; never use 0 or 1.
    • Initialize variables at the point of declaration rather than declaring them and assigning values later.
    • Use const for values that do not change, and name them in ALL_CAPS.
    int count = 0; // Correct initialization
    const int NUMBER_OF_PROJECTS = 5; // Constant naming