Master arrays, strings, and advanced loop patterns in C++.
Learn to work with collections of data and text manipulation in C++.
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 valuesfor (int i = 0; i < 5; i++) { cout << numbers[i] << " "; }numbers[i]int sum = 0; before the loopsum += numbers[i]; (same as sum = sum + numbers[i])int largest = numbers[0]; (assume first is largest)numbers[i] > largest, update: largest = numbers[i];sizeof(array)/sizeof(array[0]) to get array length dynamicallyArray elements: 10 25 8 42 17
Sum: 102
Largest: 42
int numbers[5] = {10, 25, 8, 42, 17};numbers[0] is the first element (value 10)for (int i = 0; i < 5; i++)sum += numbers[i];if (numbers[i] > largest) largest = numbers[i];Review feedback below
string firstName = "John";string lastName = "Doe";#include <string> at the topstring fullName = firstName + " " + lastName;cout << fullName.length() << endl;.length() or .size() returns number of characterscout << fullName[0] << endl; → prints 'J'toupper() to convert: char upper = toupper(fullName[i]);string is a class with built-in methods.length(), .substr(), .find(), .append()toupper() and tolower() are in <cctype> headerFirst name: John
Last name: Doe
Full name: John Doe
Length: 8
First character: J
Uppercase: JOHN DOE
string firstName = "John";string fullName = firstName + " " + lastName;fullName.length() or fullName.size()fullName[0] gets first charactertoupper(fullName[i]) - needs #include <cctype>Review feedback below
int count = 5;while (count > 0) { cout << count << "... "; count--; }count-- decrements count by 1 each iterationint n = 5;int factorial = 1;int i = 1;while (i <= n) { factorial *= i; i++; }factorial *= i means factorial = factorial * iint sum = 0, num = 1;while (sum <= 50) { sum += num; num++; }for when you know exactly how many iterationswhile when you don't know how many iterations (condition-based)do-while executes at least once, then checks conditionCountdown: 5... 4... 3... 2... 1... Liftoff!
5! = 120
Sum of 1 to 10 = 55 (exceeds 50)
while (condition) { ... }count-- subtracts 1 from countfactorial *= i means factorial = factorial * isum += num means sum = sum + numReview feedback below