One Dimensional Array (part 2)
Arrays are incredibly versatile, and you can perform various operations on them, such as copying the initial values of an existing array into another one.
Of course we cannot use the following command:
arr2 = arr1;
Because we aren’t assigning a single value. Each array has a collection of values, so if we need to assign a single value in an array we have to specify its index:
arr2[0] = arr1[0];
But what if we need to assign all the values of an array into another array?
Example 1:
#include <iostream> using namespace std; int main() { const int s = 4; int arr1[s] = { 10,20,30,40 }; int arr2[s]; for (size_t i = 0; i <= s-1; i++) { arr2[i] = arr1[i]; } for (size_t i = 0; i < s; i++) { cout << arr2[i] << " "; } cout << endl; return 0; }
This code initializes an array arr1 with values, then copies the contents of arr1 into another array arr2. Finally, it prints the elements of arr2. This demonstrates how to copy the contents of one array into another in C++.
Output:
10 20 30 40
Example 2:
#include <iostream> using namespace std; int main() { const int s = 5; int arr[s] = { 100,200,200,500,0 }; int sum = 0; for (size_t i = 0; i < s; i++) { //sum += arr[i]; sum = sum + arr[i]; } cout << "sum = " << sum << endl; return 0; }
This code calculates the sum of the elements in the integer array arr and then prints the result to the console. It demonstrates how to use a loop to iterate through an array and accumulate values.
Output:
sum = 1000
Example 3:
#include <iostream> using namespace std; int main() { const int s = 5; int arr[s] = { 100,200,200,500,100 }; long long d = 1; for (size_t i = 0; i < s; i++) { d *= arr[i]; } cout << "multi = " << d << endl; return 0; }
- long long d = 1; :This line initializes a long long integer variable d to 1. This variable will be used to accumulate the product of the elements in the array.
- long long is a data type in C++ that represents a 64-bit integer. It’s designed to store very large integer values. It can hold a wider range of values compared to regular int or long integers.
- So, the statement long long d = 1; declares a variable named d with a data type of long long and initializes it with the value 1. This variable can then be used to store and manipulate large integer values in your program.
In summary, this code calculates the product of the elements in the integer array arr and then prints the result to the console. It demonstrates how to use a loop to iterate through an array and accumulate the product of its elements.
Output:
multi = 200000000000
Example 4:
#include <iostream> using namespace std; int main() { const int s = 5; int arr[s] = { 100,200,200,501,101 }; int sumEven, sumOdd = 0; for (size_t i = 0; i < s; i++) { if (arr[i] % 2 == 0) sumEven += arr[i]; else sumOdd += arr[i]; } cout << "Sum of even numbers = " << sumEven << endl; cout << "Sum of odd numbers = " << sumOdd << endl; return 0; }
This code calculates the sum of even and odd numbers separately in the integer array arr and then prints the results to the console.
Output:
Sum of even numbers = 500 Sum of odd numbers = 602
Example 5:
#include <iostream> using namespace std; int main() { const int s = 6; int marks[s] = { 100,99,98,88,70,90 }; int sum = 0; for (size_t i = 0; i < s; i++) { sum += marks[i]; } cout << "Average = " << sum / s << endl; return 0; }
This code calculates the average of the marks stored in the integer array marks and then prints the average to the console.
Output:
Average = 90
Example 6:
#include <iostream> using namespace std; int main() { const int s = 6; int arr[s] = { 22,100,95,101,200,90 }; int max = 0; for (size_t i = 0; i < s; i++) { if (arr[i] > max) max = arr[i]; } cout << "Maximum number = " << max << endl; return 0; }
This code finds the maximum value among the numbers stored in the integer array arr and then prints the maximum value to the console.
Output:
Maximum number = 200
Example 7:
#include <iostream> using namespace std; int main() { int vector[5]; cout << "Enter 5 numbers: "; for (int i = 0; i < 5; i++) cin >> vector[i]; cout << endl; int Element; cout << "What is the element you are looking for? "; cin >> Element; bool Found = false; int i; for (i = 0; i < 5; i++) if (Element == vector[i]) { Found = true; break; } cout << endl; if (Found) cout << Element << " found at position " << i << endl; else cout << Element << " is not in this array!" << endl; return 0; }
In this C++ code, you’re taking user input to populate an integer array `vector` of size 5 and then searching for a specific element within the array. Let’s break down the code step by step:
- int vector[5];
– This line declares an integer array `vector` with a size of 5, which can store up to five integer values. - Input Loop to Populate the Array:
– The code prompts the user to enter five numbers and stores them in the `vector` array using a `for` loop that iterates five times. Each input is assigned to a different element of the array. - int Element;
– This line declares an integer variable `Element`, which will store the value the user is searching for. - Prompt for the Element to Search:
– The code asks the user to input the element they want to search for. - Search Loop:
– The `for` loop is used to search for the element within the array. If the element is found, the loop breaks, and the `Found` flag is set to `true`. The variable `i` keeps track of the position where the element was found. - Output the Result:
– The code then checks if the element was found (i.e., `Found` is `true`). If found, it displays the element and its position in the array. If not found, it informs the user that the element is not in the array.
In summary, this code allows the user to enter five numbers into an array and then search for a specific element within the array. It provides feedback on whether the element was found and its position in the array.