I am teaching the basics of C++. I already provided an assignment on Vector, but I would…
- User
I am teaching the basics of C++. I already provided an assignment on Vector, but I would give something more that should be beneficial to do this one. Please provide feedback and in case any useful introduction assignment. Here the assignment I am already providing: ~ Assignment 1 - Vectors Basics
Exercises:
Create a program that initializes an empty integer vector of size n with all elements as zero using the fill constructor.
Write a function to add new element at end of vector, insert new element in middle and delete last element from vector.
Implement a function to sort a given vector (use any algorithm, e.g.: std::sort).
Create another program that initializes an empty string vector with size n and perform similar operations as above.
Deliverables:
C++ code hosted in a git repository, with two branches: main and string-exercise. Each method must be Unit Tested using Catch2.
Hint: Use reference for vector. Lectures: Section 5 - 6- 7 - 8 - 9 - 10.
- Assistant
Your assignment on vector basics is well-structured and introduces students to a variety of vector operations, from simple initialization to basic manipulation and sorting. Here is some feedback and suggestions for additional introductory assignments that can build a strong foundation for understanding C++.
Feedback on the Current Assignment
-
Initialization and Basic Operations:
- Introducing both integer and string vectors is a good way to demonstrate type-agnostic behavior of C++ vectors.
- Including the fill constructor encourages students to explore constructors beyond the default.
-
Functionality:
- Adding, inserting, and deleting elements are core operations. Make sure students understand iterators when inserting elements.
- Sorting can be expanded by encouraging students to explore different sorting algorithms provided by the standard library as well as implementing a simple sorting algorithm themselves.
-
Git and Unit Testing:
- Introducing version control and unit testing early is vital. Catch2 is a user-friendly framework, so it's a good choice.
- Make sure to explain the importance of branching in Git and how it can be used for maintaining different versions of the code.
Additional Introductory Assignments
To further enhance your curriculum on C++ basics with a focus on vectors, consider the following assignments:
Assignment 2 - Vector Iterators and Algorithms
Exercises:
-
Iterators:
- Write a program that initializes a vector of integers and uses iterators to traverse and display its contents.
- Modify the same program to reverse traverse the vector using reverse iterators.
-
Algorithm Usage:
- Implement a program to find the maximum and minimum elements in the vector using
std::max_element()andstd::min_element(). - Use
std::accumulate()to calculate the sum of all elements in the vector.
- Implement a program to find the maximum and minimum elements in the vector using
-
Custom Algorithm:
- Implement a simple searching algorithm to find if a number exists in the vector, returning its position if found, using iterators.
Deliverables:
- Code hosted in your existing git repository. Create a new branch named
iterators-algorithms. - Unit tests using Catch2 for each operation (traversing, finding min/max, sum, and search).
Assignment 3 - Advanced Vector Operations
Exercises:
-
2D Vectors:
- Create a program to initialize a 2D vector and perform operations such as setting a specific value, adding a row, and deleting a column.
-
Vector Capacity:
- Write a program to explore and display the vector's capacity, size, and how they change with add/remove operations using methods like
capacity(),size(), andreserve().
- Write a program to explore and display the vector's capacity, size, and how they change with add/remove operations using methods like
-
Performance Testing:
- Implement a program that compares the performance of operations on vectors with operations on native arrays (use timing functions to measure and compare).
Deliverables:
- Code with a new branch
advanced-vector-operations. - Well-documented outputs demonstrating capacity behaviors and performance metrics.
These assignments aim to give students a robust understanding of C++ vectors and prepare them to handle more complex data structures and algorithms. Feel free to adjust the complexity according to your students' current knowledge level.
-
- User
Assignment 2 looks good, however, I am looking for something less "advanced" to Assignment 1, preparatory to Assignment 1.
- Assistant
For a preparatory assignment to Assignment 1 that focuses on introducing basic concepts, you could consider creating exercises that familiarize students with fundamental C++ syntax and programming concepts. Here is an idea for such an assignment that leads nicely into working with vectors:
Assignment 0 - C++ Basics and Control Structures
Exercises:
-
Basic Input/Output:
- Write a C++ program that asks the user for their name and age, then outputs a greeting message using the collected inputs.
-
Simple Arithmetic:
- Create a program that computes the sum, difference, product, and quotient of two numbers entered by the user.
-
Conditional Statements:
- Implement a program that takes an integer as input and checks whether it's even or odd using
if-elsestatements.
- Implement a program that takes an integer as input and checks whether it's even or odd using
-
Loop Structures:
- Write a program that prints the numbers from 1 to 10 using a
forloop. - Modify the program to use a
whileloop and then ado-whileloop.
- Write a program that prints the numbers from 1 to 10 using a
-
Basic Function:
- Create a simple function that takes two integers as parameters and returns their average. Demonstrate calling this function in
main()and display the result.
- Create a simple function that takes two integers as parameters and returns their average. Demonstrate calling this function in
Deliverables:
- C++ code hosted in a git repository under a branch named
cpp-basics. - Demonstrations of each program running with example inputs and outputs documented in a
README.mdfile.
Key Concepts Introduced:
-
Basics of C++ Syntax:
- Introduction to
iostreamfor input and output. - Use of
int,std::string, and other basic data types.
- Introduction to
-
Control Flow:
- Understanding of
if-elsestatements for decision making. - Basic use of loops (
for,while,do-while).
- Understanding of
-
Functions:
- Definition and use of simple functions to compartmentalize logic.
-
Basic Arithmetic and Logical Operators:
- Operators for mathematical and logical operations.
-
Git Basics:
- An introduction to using Git for version control with simple command practice like
init,commit,branch.
- An introduction to using Git for version control with simple command practice like
Providing this assignment helps students get comfortable with C++ programming before they dive into vectors and more complex data structures, ensuring a smoother transition and clearer understanding of future assignments.
-
- User
Sounds good. Please provide text for Assignement 2 and Preparatory Assignment to be include in the Assignment list.
- Assistant
Certainly! Below are the texts you can include in your assignment list for both the Preparatory Assignment and Assignment 2.
Preparatory Assignment - C++ Basics and Control Structures
Exercises:
-
Basic Input/Output:
- Write a C++ program that prompts the user to enter their name and age. Display a greeting message using these inputs.
-
Simple Arithmetic:
- Create a program to compute the sum, difference, product, and quotient of two numbers entered by the user.
-
Conditional Statements:
- Implement a program that reads an integer and determines whether it is even or odd using
if-elsestatements.
- Implement a program that reads an integer and determines whether it is even or odd using
-
Loop Structures:
- Write a program that uses a
forloop to print numbers from 1 to 10. - Modify your program to use a
whileloop and then ado-whileloop for the same task.
- Write a program that uses a
-
Basic Function:
- Create a function that takes two integers as parameters, calculates their average, and returns it. Demonstrate the function's use in
main()by displaying the result.
- Create a function that takes two integers as parameters, calculates their average, and returns it. Demonstrate the function's use in
Deliverables:
- C++ code hosted in a Git repository on a branch named
cpp-basics. - Include a
README.mdwith demonstrations of each program, example inputs, and outputs.
Assignment 2 - Vector Iterators and Algorithms
Exercises:
-
Iterators:
- Write a program that initializes a vector of integers and uses iterators to traverse and display its contents.
- Modify the same program to reverse traverse the vector using reverse iterators.
-
Algorithm Usage:
- Implement a program to find the maximum and minimum elements in the vector using
std::max_element()andstd::min_element(). - Use
std::accumulate()to calculate the sum of all elements in the vector.
- Implement a program to find the maximum and minimum elements in the vector using
-
Custom Algorithm:
- Implement a simple searching algorithm to find if a number exists in the vector, returning its position if found, using iterators.
Deliverables:
- Host your code in the existing Git repository. Create a new branch named
iterators-algorithms. - Each operation should be unit tested using Catch2.
These assignments are designed to gradually build your students' skills in C++, providing an incremental learning path from basic concepts to more advanced topics involving vectors and standard library algorithms.
-