C++ Programming Labs

Master arrays, strings, and advanced loop patterns in C++.

Arrays, Strings & Loops - Module 3

Learn to work with collections of data and text manipulation in C++.

Lab 7: Arrays
Intermediate
Coding Challenge
Your Task: Create and manipulate an array of integers. Calculate the sum and find the largest element.

Detailed Requirements:
1. Declare and initialize an integer array:
int numbers[5] = {10, 25, 8, 42, 17};
   • int numbers[5] - declares an array that can hold 5 integers
   • = {10, 25, 8, 42, 17} - initializes with these values
   • Array indices start at 0: numbers[0]=10, numbers[1]=25, etc.

2. Print all array elements using a for loop:
for (int i = 0; i < 5; i++) { cout << numbers[i] << " "; }
   • Loop from index 0 to 4 (5 elements)
   • Access each element with numbers[i]

3. Calculate the sum of all elements:
   • Declare int sum = 0; before the loop
   • Inside the loop: sum += numbers[i]; (same as sum = sum + numbers[i])
   • After the loop, print the sum

4. Find the largest element:
   • Declare int largest = numbers[0]; (assume first is largest)
   • Loop through remaining elements
   • If numbers[i] > largest, update: largest = numbers[i];

Key Concepts:
• Arrays are zero-indexed: first element is at index 0
• Array size is fixed at compile time in C++
• Accessing index out of bounds causes undefined behavior (no error, but wrong results)
• Use sizeof(array)/sizeof(array[0]) to get array length dynamically

Expected Output:
Array elements: 10 25 8 42 17 Sum: 102 Largest: 42

Requirements Checklist

Declare an int array with at least 5 elements
Use a for loop to iterate through the array
Print all array elements
Calculate and print the sum of all elements
Find and print the largest element
Code compiles and produces correct output
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Declare array: int numbers[5] = {10, 25, 8, 42, 17};
• Access element: numbers[0] is the first element (value 10)
• Loop through: for (int i = 0; i < 5; i++)
• Add to sum: sum += numbers[i];
• Update largest: if (numbers[i] > largest) largest = numbers[i];
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 8: String Manipulation
Intermediate
Coding Challenge
Your Task: Work with C++ strings to perform common text operations like concatenation, finding length, and accessing characters.

Detailed Requirements:
1. Declare and initialize two strings:
string firstName = "John";
string lastName = "Doe";
   • Include #include <string> at the top
   • Strings use double quotes, not single quotes

2. Concatenate strings to create full name:
string fullName = firstName + " " + lastName;
   • The + operator joins strings together
   • Add a space " " between first and last name

3. Get and print the string length:
cout << fullName.length() << endl;
   • .length() or .size() returns number of characters
   • "John Doe" has length 8 (space counts!)

4. Access individual characters:
cout << fullName[0] << endl; → prints 'J'
   • Strings are like character arrays
   • Index starts at 0

5. Convert to uppercase (loop through characters):
   • Loop through each character
   • Use toupper() to convert: char upper = toupper(fullName[i]);

Key Concepts:
• C++ string is a class with built-in methods
• Strings are mutable (can be changed after creation)
• Common methods: .length(), .substr(), .find(), .append()
toupper() and tolower() are in <cctype> header

Expected Output:
First name: John Last name: Doe Full name: John Doe Length: 8 First character: J Uppercase: JOHN DOE

Requirements Checklist

Declare firstName and lastName string variables
Concatenate strings to create fullName
Print the length of the full name using .length()
Access and print individual characters using []
Convert and print the name in uppercase
Code compiles and runs correctly
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Declare string: string firstName = "John";
• Concatenate: string fullName = firstName + " " + lastName;
• Get length: fullName.length() or fullName.size()
• Access character: fullName[0] gets first character
• Uppercase: toupper(fullName[i]) - needs #include <cctype>
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 9: While Loops
Intermediate
Coding Challenge
Your Task: Use while loops to implement a number guessing game simulation and calculate factorial.

Detailed Requirements:
1. Create a countdown using while loop:
int count = 5;
while (count > 0) { cout << count << "... "; count--; }
   • While loop continues as long as condition is true
   • count-- decrements count by 1 each iteration
   • Without decrementing, you get an infinite loop!

2. Calculate factorial using while loop:
Factorial of n (written as n!) = n × (n-1) × (n-2) × ... × 1
Example: 5! = 5 × 4 × 3 × 2 × 1 = 120
int n = 5;
int factorial = 1;
int i = 1;
while (i <= n) { factorial *= i; i++; }
   • factorial *= i means factorial = factorial * i

3. Sum numbers until a condition is met:
Add numbers 1, 2, 3, ... until sum exceeds 50
int sum = 0, num = 1;
while (sum <= 50) { sum += num; num++; }
   • Print how many numbers were added
   • Print the final sum

Key Concepts - While vs For:
• Use for when you know exactly how many iterations
• Use while when you don't know how many iterations (condition-based)
do-while executes at least once, then checks condition

⚠️ Warning: Always ensure your while loop will eventually terminate! If the condition never becomes false, you have an infinite loop.

Expected Output:
Countdown: 5... 4... 3... 2... 1... Liftoff! 5! = 120 Sum of 1 to 10 = 55 (exceeds 50)

Requirements Checklist

Create a countdown from 5 to 1 using while loop
Print "Liftoff!" after countdown
Calculate factorial of 5 using while loop
Print the factorial result (should be 120)
Sum numbers until sum exceeds 50
Print final sum and how many numbers were added
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• While syntax: while (condition) { ... }
• Decrement: count-- subtracts 1 from count
• Multiply-assign: factorial *= i means factorial = factorial * i
• Add-assign: sum += num means sum = sum + num
• Always ensure loop condition eventually becomes false!
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below