Skip to main content

Dynamic arrays ยป More examples

Now that we know how to create and use dynamic arrays, let's see some more examples.

Loops

To take full advantage of what we can do with arrays, you'll need to learn about loops. We're going to learn about them right after arrays. Until then in the following example we'll use only the most basic for loop that we've shown you already.

Example 1:โ€‹

Manipulating list of student grades
#include <iostream>
#include <vector>
#include <string>

int main()
{
std::vector<std::string> student_names(3);
std::vector<int> student_grades(3);

// Ask user to enter student names and grades
std::cout << "Enter the first student's name: ";
std::cin >> student_names[0];
std::cout << "Enter the first student's grade: ";
std::cin >> student_grades[0];

std::cout << "Enter the second student's name: ";
std::cin >> student_names[1];
std::cout << "Enter the second student's grade: ";
std::cin >> student_grades[1];

std::cout << "Enter the third student's name: ";
std::cin >> student_names[2];
std::cout << "Enter the third student's grade: ";
std::cin >> student_grades[2];

// Print out the list of students and their grades
std::cout << "Student list:" << std::endl;

}

Dynamic arrays ยป More examples

Now that we know how to create and use dynamic arrays, let's see some more examples.

Loops

To take full advantage of what we can do with arrays, you'll need to learn about loops. We're going to learn about them right after arrays. Until then in the following example we'll use only the most basic for loop that we've shown you already.

Example 1:โ€‹

Manipulating list of student grades
#include <iostream>
#include <vector>
#include <string>

int main()
{
std::vector<std::string> student_names(3);
std::vector<int> student_grades(3);

// Ask user to enter student names and grades
std::cout << "Enter the first student's name: ";
std::cin >> student_names[0];
std::cout << "Enter the first student's grade: ";
std::cin >> student_grades[0];

std::cout << "Enter the second student's name: ";
std::cin >> student_names[1];
std::cout << "Enter the second student's grade: ";
std::cin >> student_grades[1];

std::cout << "Enter the third student's name: ";
std::cin >> student_names[2];
std::cout << "Enter the third student's grade: ";
std::cin >> student_grades[2];

// Print out the list of students and their grades
std::cout << "Student list:" << std::endl;

}