References

Look into these references for review

Practice Questions

Assisted with Perplexity for the questions only

Appendix

Assume the code is like this

#include <iostream>
#include <vector>
#include <string>
#include <ctype>
 
using namespace std;
 
int main() {
	// Code here for the problems;
	return 0;
}
  1. Ask the user to enter a word, then display the word and its length (number of characters)
  1. Given a string from the user, write code to print the string in reverse order
  1. CHALLENGING: Prompt the user for a sentence and count the number of vowels (a, e, i, o, u) that appear in it
  1. Have the user type two words, then display them joined together with a dash (for example: “hello” and “world” becomes “hello-world”).

Additional questions

  1. Write code that asks the user for a string and prints whether it begins with the letter ‘C’. Display “Yes” or “No”.

  2. Given a string entered by the user, count and print how many times the character ‘e’ appears in it.

  3. Ask for a string input and then print the string in all uppercase letters (do NOT use transform).

// Must have #include <cctype> before the int main() because toupper() is only referenceable from cctype library
// #include <cctype>
 
string input;
getline(cin, input);
 
for (size_t i = 0; i < input.length(); i++) {
	input[i] = toupper(input[i]);
}
 
cout << "Uppercase: " << input;
  1. Prompt for a short sentence and print only the first word (the characters before the first space).

Vector Questions To be worked on in the study group

  1. Ask the user to enter 5 integers, store them in a vector, and then display the numbers in the same order.
vector<double> numbers = {};
cout << "Enter 5 integers (one at a time): " << endl;
 
for (int i = 0; i < 5; i++) {
	double num;
	cin >> num;
	numbers.push_back(num);
}
 
cout << "Listed all the numbers" << endl;
 
for (double num : numbers) {
	cout << num << endl;
}
  1. Read 5 numbers into a vector, then print the sum of the numbers using a loop.

  2. Given a vector of integers, find and display the largest value stored in the vector.

  3. Ask the user to enter words one at a time, storing each input into a string vector, until the user types “done”; print all words the user entered. Wasn’t discussed thoroughly during study session

vector<string> listOfInputs = {};
 
while (true) {
	string word;
	cout << "Enter a word: ";
	cin >> word;
	
	if (word == "done") break;
	listOfInputs.push_back(word);
}
 
cout << "List of words: ";
for (int i = 0; i < listOfInputs.size(); i++) {
	cout << listOfInputs[i];
	if (i != listOfInputs.size() - 1)
		cout << ", ";
}

Additional vector questions

  1. Read 4 numbers into a vector and print out the smallest value.
vector<int> nums = {1, 2, 3, 4};
int lowest;
 
for (int integer : nums) {
	if (lowest <= integer) continue;
	lowest = integer;
}
 
cout << "lowest: " << lowest;
  1. Create a vector of size 5. Ask the user to enter all five values, then print them in reverse order.
vector<float> nums;
float a, b, c, d, e;
 
cout << "Enter five values: ";
cin >> a >> b >> c >> d >> e;
 
string message = "";
for (int i = 5; i>0; i--) {
	message.append();
}
 
  1. Use a vector to store words entered by the user, then print only those words that begin with ‘a’.

  2. Ask the user to input 5 numbers, store them in a vector, and print their average (mean value as a double).

Practice questions

  1. Find the maximum element in a vector without using built-in functions.
vector<string> tasks = {"grocery", "homework", "project", "club"};
 
return tasks[3];
  1. Find the minimum element in a vector without using built-in functions.
vector<string> tasks = {"grocery", "homework", "project", "club"};
 
return tasks[0];
  1. Compute the sum of all elements in a vector.
vector<float> nums = {1,2,3,4,5,6};
float sum = 0;
for (float number: nums) {
	sum += number;
}
cout << sum;
  1. Compute the average of the numbers stored in a vector.
vector<float> nums = {45, 67, 82, 49, 75}
float avg = 0;
float sum = 0;
for (float number: numbers) {
	sum += number;
}
avg = sum/nums.size();
  1. Count how many times a specific number appears in a vector.
vector<float> nums = {1,2,3,4,2,3,2,4,5};
float specNum = 4;
int count = 0;
 
for (float number : nums) {
	if (number == specNum)
		count += 1;
}
 
cout << "Found specific number " << specNum << ": " << count << " time(s)";
  1. Remove all even numbers from a vector and display the result.

  2. Insert a new number at the beginning of a vector.

  3. Append a new number at the end of a vector.

  4. Reverse a vector using a loop.

  5. Check if a string is a palindrome (reads the same forwards and backwards).

string str = "whatever"
string 
  1. Count how many times a specific character appears in a string.

  2. Find the first index of a given character in a string.

  3. Remove all spaces from a string and print the result.

Ask for more coding questions

Ask Perplexity

Ask ChatGPT