📘

Introduction to Programming by Striver

Jul 17, 2024

Lecture Notes: Introduction to Programming by Striver

Introduction

  • Speaker: Striver
  • Platform: YouTube
  • Focus: Interview preparation with Data Structures and Algorithms (DSA)
  • **Resources: ** Striver's SD Sheet (for experienced), Striver's A to Z DSA (for beginners)

Resources

  • Striver's SD Sheet:
    • Curated list of 191 DSA problems
    • Requires basic DSA knowledge
  • **Striver's A to Z DSA Sheet: **
    • 455 steps with problem links and video explanations
    • Covers basic to advanced DSA topics

Social Media

  • Follow Striver on: LinkedIn, Twitter, Instagram, Telegram

Programming Languages

  • C++, Java, Python, JavaScript: Choose one and master it
  • Video Sequence: Covers basic, then detailed topics for each language

C++ Basics

Skeleton of a C++ Code

  • #include <iostream>
    using namespace std;
    int main() { 
        // Code
        return 0; 
    } 
    

Input & Output

  • Use #include <iostream> for I/O operations
  • Example to print a name:
    cout << "Striver" << endl;
    `` 
    

Managing Namespaces

  • Use std:: for using I/O functions like cout and cin
  • To avoid typing std:: each time, use using namespace std;

Data Types

  • Integer Types: int, long, long long
  • Floating-Point: float, double
  • Strings and Characters:
    • char (single character)
    • string (sequence of characters)
  • Input/Output:
    int x;
    cin >> x;
    cout << x << endl;
    ``
    

Including All Libraries

  • #include <bits/stdc++.h> – Includes all standard libraries

Conditional Statements

If-Else Structure

if (condition) {
    // code
} else if (condition) {
    // code
} else {
    // code
}
  • Example: Check if age is an adult
    int age;
    cin >> age;
    if (age >= 18) {
        cout << "You are an adult";
    } else {
        cout << "You are not an adult";
    }
    

Switch Case

  • For fixed multiple choice values
    switch(variable) {
        case 1: // code
            break;
        case 2: // code
            break;
        ...
        default: // code
    }
    

Arrays and Strings

Arrays

  • 1D Array:
    int arr[5];
    for (int i = 0; i < 5; i++) {
        cin >> arr[i];
    }
    for (int i = 0; i < 5; i++) {
        cout << arr[i] << " ";
    }
    
  • 2D Array:
    int arr[3][5];
    // 3 rows, 5 columns
    

Strings

  • Access and modify characters by index
  • Example:
    string s = "Striver";
    cout << s[0] << endl; // Prints 'S'
    s[0] = 'Z';
    cout << s[0] << endl; // Prints 'Z'
    

Loops

For Loop

  • Syntax:
    for (int i = 0; i < n; i++) {
        // code
    }
    
  • Example:
    for (int i = 1; i <= 10; i++) {
        cout << i << endl;
    }
    

While Loop

  • Syntax:
    int i = 0;
    while (i < n) {
        // code
        i++;
    }
    

Do-While Loop

  • Executes at least once
    int i = 0;
    do {
        // code
        i++;
    } while(i < n);
    

Functions

Basics

  • Purpose: Reusability, Readability, and Modularity
  • Syntax:
    return_type function_name(parameters) {
        // Code
        return value;
    }
    

Types

  • Void Functions: Do not return a value
    void printName() {
        cout << "Striver" << endl;
    }
    
  • Parameterized Functions: Accept parameters
    void printName(string name) {
        cout << name << endl;
    }
    
  • Return Functions: Return a value
    int add(int num1, int num2) {
        return num1 + num2;
    }
    

Pass by Value vs Reference

  • Pass by Value: Copy is passed; changes not reflected outside function
    void changeValue(int a) {
        a = 5; // Affect local 'a' only
    }
    
  • Pass by Reference: Original value is passed; changes reflected outside
    void changeValue(int &a) {
        a = 5; // Affect original variable
    }
    
  • Arrays: Always passed by reference
    void modifyArray(int arr[], int size) {
        arr[0] = 10; // Original array modified
    }
    

Final Points

  • Interactive Sessions: Video includes Q&A sessions
  • Practice: Follow the curated sheets for hands-on practice

Conclusion

  • Subscribe: Encourages to subscribe for regular updates
  • Follow: Updates on social media for new content