C++ Array Exercises for Beginners
Arrays are fundamental data structures in programming. They allow you to store collections of elements of the same data type in a contiguous memory location. This makes them incredibly efficient for various tasks, such as storing lists of numbers, characters, or even objects.
If you're a beginner in C++, getting comfortable with arrays is crucial. The following exercises will help you grasp the basics of array manipulation and get you started on your journey of mastering C++:
Exercise 1: Initializing and Printing an Array
Objective: Create an array of integers, initialize it with values, and print each element.
Instructions:
- Declare an integer array named
numbers
with a size of 5. - Initialize the array with the following values: 10, 20, 30, 40, 50.
- Use a
for
loop to iterate through the array and print each element on a separate line.
Code Example:
#include
using namespace std;
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; ++i) {
cout << numbers[i] << endl;
}
return 0;
}
Exercise 2: Finding the Sum of Array Elements
Objective: Calculate the sum of all elements in an array.
Instructions:
- Declare an integer array named
scores
with a size of 10. - Initialize the array with random integer values using a loop.
- Use another loop to iterate through the array and add each element to a variable called
sum
. - Print the final value of
sum
.
Code Example:
#include
#include // For using rand()
using namespace std;
int main() {
int scores[10];
int sum = 0;
// Initialize the array with random values
for (int i = 0; i < 10; ++i) {
scores[i] = rand() % 100; // Generate random values between 0 and 99
}
// Calculate the sum of array elements
for (int i = 0; i < 10; ++i) {
sum += scores[i];
}
cout << "The sum of array elements is: " << sum << endl;
return 0;
}
Exercise 3: Finding the Largest Element in an Array
Objective: Find the largest element in an array.
Instructions:
- Declare an integer array named
numbers
with a size of 8. - Initialize the array with random integer values using a loop.
- Set a variable
largest
to the first element of the array. - Iterate through the array using a loop.
- Inside the loop, compare each element to the current
largest
value. If an element is greater thanlargest
, update thelargest
value. - Print the final value of
largest
.
Code Example:
#include
#include
using namespace std;
int main() {
int numbers[8];
int largest = numbers[0];
// Initialize the array with random values
for (int i = 0; i < 8; ++i) {
numbers[i] = rand() % 100;
}
// Find the largest element
for (int i = 1; i < 8; ++i) {
if (numbers[i] > largest) {
largest = numbers[i];
}
}
cout << "The largest element in the array is: " << largest << endl;
return 0;
}
Exercise 4: Sorting an Array in Ascending Order
Objective: Sort the elements of an array in ascending order using the bubble sort algorithm.
Instructions:
- Declare an integer array named
numbers
with a size of 6. - Initialize the array with random integer values using a loop.
- Implement the bubble sort algorithm:
- Iterate through the array using a nested loop.
- In the inner loop, compare adjacent elements. If they are in the wrong order, swap them.
- Print the sorted array.
Code Example:
#include
#include
using namespace std;
int main() {
int numbers[6];
// Initialize the array with random values
for (int i = 0; i < 6; ++i) {
numbers[i] = rand() % 100;
}
// Bubble Sort
for (int i = 0; i < 6 - 1; ++i) {
for (int j = 0; j < 6 - i - 1; ++j) {
if (numbers[j] > numbers[j + 1]) {
// Swap elements
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
// Print the sorted array
cout << "Sorted array in ascending order: ";
for (int i = 0; i < 6; ++i) {
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}
Exercise 5: Searching for an Element in an Array
Objective: Find a specific element in an array using a linear search.
Instructions:
- Declare an integer array named
numbers
with a size of 7. - Initialize the array with random integer values using a loop.
- Prompt the user to enter a number to search for.
- Implement a linear search algorithm:
- Iterate through the array using a loop.
- If the search number is found, print its index and break out of the loop.
- If the number is not found after iterating through the entire array, print a message indicating that the element is not present.
Code Example:
#include
#include
using namespace std;
int main() {
int numbers[7];
int searchNumber;
// Initialize the array with random values
for (int i = 0; i < 7; ++i) {
numbers[i] = rand() % 100;
}
// Get the number to search for from the user
cout << "Enter the number to search for: ";
cin >> searchNumber;
// Linear Search
for (int i = 0; i < 7; ++i) {
if (numbers[i] == searchNumber) {
cout << "The number " << searchNumber << " is found at index " << i << endl;
return 0; // Exit the program
}
}
// If the number is not found
cout << "The number " << searchNumber << " is not present in the array." << endl;
return 0;
}
These exercises are just a starting point for your C++ array exploration. You can continue building upon them by creating more complex programs that involve multidimensional arrays, dynamic allocation, and more advanced algorithms. Remember, practice is key!