Introduction to Programming

This section introduces the fundamental concepts of programming. You will learn about the structure of a C++ program, basic input and output operations, and how to perform arithmetic calculations.

What is Programming?

Programming is the process of writing instructions that a computer can execute to perform specific tasks. These instructions, written in a programming language, form a program. The process involves understanding the problem, designing a solution (algorithm), implementing the solution in code, and testing for correctness.

Core Concepts

  • Algorithm - A finite sequence of well-defined steps to solve a problem
  • Syntax - The rules governing the structure of valid statements in a language
  • Semantics - The meaning of syntactically valid statements
  • Debugging - The process of identifying and correcting errors in code

Program Structure

Every C++ program follows a basic structure. The following template shows the minimal components required:

#include <iostream>
using namespace std;

int main() {
    // Your code here
    
    return 0;
}

Understanding the Code:

  • #include <iostream> - Includes input/output functionality
  • using namespace std; - īAllows us to use standard library functions
  • int main() - The main function where program execution begins
  • code - This is where you write your program logic. The functions (like code snippet outsite main() is called from here either indirectly or directly to be executed)
  • return 0; - Indicates successful program execution

Input and Output

Most programs need to interact with users by taking input and producing output:


#include 
using namespace std;

int main() {
    string name;
    int age;
    
    cout << "Enter your name: ";
    cin >> name;
    
    cout << "Enter your age: ";
    cin >> age;
    
    cout << "Hello, " << name << ". You are " << age << " years old." << endl;
    
    // For reading entire lines including spaces:
    string fullLine;
    cin.ignore(); // Clear the newline from previous input
    getline(cin, fullLine);
    
    return 0;
}
                    

Understanding the Code:

  • cin - function to take input
  • cout - function to output data
  • getline(cin, s) - Reads an entire line of input including spaces

Arithmetic Operations

C++ supports standard arithmetic operations. Note the distinction between integer and floating-point division:


#include 
using namespace std;

int main() {
    int num1, num2;
    
    cout << "Enter two numbers: ";
    cin >> num1 >> num2;
    
    cout << "Sum: " << num1 + num2 << endl;
    cout << "Difference: " << num1 - num2 << endl;
    cout << "Product: " << num1 * num2 << endl;
    cout << "Division: " << (double)num1 / num2 << endl;
    
    return 0;
}
                    

Common Errors

  • Omitting semicolons at statement ends
  • Mismatched brackets or parentheses
  • Case sensitivity errors (C++ distinguishes between uppercase and lowercase)
  • Integer division truncating decimal portions unexpectedly

Practice Problems

Apply the concepts from this section with these exercises:

Introductory

Theatre Square

Calculate the number of flagstones needed to pave a rectangular square. Tests basic arithmetic and input/output.

Math Input/Output
View Problem
Easy

Way Too Long Words

Implement word abbreviation based on length. Introduces string handling and conditionals.

Strings Implementation
View Problem

Recommendations

  • Start with simple problems to build confidence
  • Read problem statements carefully
  • Test your code with the provided examples
  • Don't worry about efficiency at first - focus on correctness