Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Modifying Array Values | Using Arrays in Programs
C++ Arrays

bookModifying Array Values

Arrays in C++ are not just for storing dataβ€”you can also change their contents as your program runs. Modifying array elements is essential for many tasks, such as updating scores, tracking inventory, or performing calculations on sets of numbers. To change the value of a specific element, you use its index and the assignment operator (=). This allows you to update data in-place and use updated values for further processing or output.

main.cpp

main.cpp

copy
12345678910111213141516
#include <iostream> int main() { int numbers[5] = {1, 2, 3, 4, 5}; // Double each value in the array for (int i = 0; i < 5; ++i) numbers[i] = numbers[i] * 2; // Print the modified array for (int i = 0; i < 5; ++i) std::cout << numbers[i] << " "; std::cout << std::endl; }

In this example, you start with an array of five integers. The loop goes through each index from 0 to 4, and on each pass, the value at that index is replaced with its double. The assignment numbers[i] = numbers[i] * 2; takes the current value, multiplies it by 2, and stores the result back into the same position. After the loop, the array holds the values 2, 4, 6, 8, and 10, which are then printed to the console. Assigning to array elements in this way lets you update data based on calculations or other logic.

main.cpp

main.cpp

copy
123456789101112131415
#include <iostream> int main() { int arr[4] = {7, 8, 9, 10}; // Set all elements to zero for (int i = 0; i < 4; ++i) arr[i] = 0; // Print the array to check the result for (int i = 0; i < 4; ++i) std::cout << arr[i] << " "; std::cout << std::endl; }

When changing array values, it is important to use the correct index. If you accidentally use the wrong index, you might overwrite the wrong element or even access memory outside the array, leading to bugs or crashes. Always make sure your loops start at 0 and stop before the array's size, and double-check which index corresponds to the element you want to change.

Note
Note

Always check your loop indices to ensure you are modifying the intended elements.

question mark

Which statement correctly assigns the value 10 to the third element of an int array named arr?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookModifying Array Values

Swipe to show menu

Arrays in C++ are not just for storing dataβ€”you can also change their contents as your program runs. Modifying array elements is essential for many tasks, such as updating scores, tracking inventory, or performing calculations on sets of numbers. To change the value of a specific element, you use its index and the assignment operator (=). This allows you to update data in-place and use updated values for further processing or output.

main.cpp

main.cpp

copy
12345678910111213141516
#include <iostream> int main() { int numbers[5] = {1, 2, 3, 4, 5}; // Double each value in the array for (int i = 0; i < 5; ++i) numbers[i] = numbers[i] * 2; // Print the modified array for (int i = 0; i < 5; ++i) std::cout << numbers[i] << " "; std::cout << std::endl; }

In this example, you start with an array of five integers. The loop goes through each index from 0 to 4, and on each pass, the value at that index is replaced with its double. The assignment numbers[i] = numbers[i] * 2; takes the current value, multiplies it by 2, and stores the result back into the same position. After the loop, the array holds the values 2, 4, 6, 8, and 10, which are then printed to the console. Assigning to array elements in this way lets you update data based on calculations or other logic.

main.cpp

main.cpp

copy
123456789101112131415
#include <iostream> int main() { int arr[4] = {7, 8, 9, 10}; // Set all elements to zero for (int i = 0; i < 4; ++i) arr[i] = 0; // Print the array to check the result for (int i = 0; i < 4; ++i) std::cout << arr[i] << " "; std::cout << std::endl; }

When changing array values, it is important to use the correct index. If you accidentally use the wrong index, you might overwrite the wrong element or even access memory outside the array, leading to bugs or crashes. Always make sure your loops start at 0 and stop before the array's size, and double-check which index corresponds to the element you want to change.

Note
Note

Always check your loop indices to ensure you are modifying the intended elements.

question mark

Which statement correctly assigns the value 10 to the third element of an int array named arr?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2
some-alt