C++ Arrays
Arrays in C++ are used to store multiple values of the same type in a single variable. They are stored in contiguous memory locations.
C++ प्रोग्रामिंग में Arrays का use तब होता है जब हमें एक साथ कई values (same type की) एक variable में store करनी हों। यह data के continuous memory locations में elements का collection होता है।
Example 1: Accessing Array Elements
#include <iostream>
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;
}
Output:
10 20 30 40 50
Prints all elements of the array using a loop.
यह प्रोग्राम एक array के सभी elements को for loop से print करता है।
Example 2: Sum of Array Elements
#include <iostream>
using namespace std;
int main() {
int arr[4] = {1, 2, 3, 4};
int sum = 0;
for(int i = 0; i < 4; i++) {
sum += arr[i];
}
cout << "Sum = " << sum;
return 0;
}
Output:
Sum = 10
Calculates and prints the sum of elements in an array.
यह प्रोग्राम array के सारे elements का sum निकालता है और print करता है।
Example 3: Find Maximum Element
#include <iostream>
using namespace std;
int main() {
int arr[5] = {12, 34, 56, 23, 45};
int max = arr[0];
for(int i = 1; i < 5; i++) {
if(arr[i] > max) {
max = arr[i];
}
}
cout << "Maximum = " << max;
return 0;
}
Output:
Maximum = 56
Finds and prints the maximum element in the array.
यह प्रोग्राम array में सबसे बड़े element को ढूंढ़कर print करता है।
Example 4: Find Minimum Element
#include <iostream>
using namespace std;
int main() {
int arr[5] = {12, 34, 56, 23, 45};
int min = arr[0];
for(int i = 1; i < 5; i++) {
if(arr[i] < min) {
min = arr[i];
}
}
cout << "Minimum = " << min;
return 0;
}
Output:
Minimum = 12
Finds and prints the minimum element in the array.
यह प्रोग्राम array में सबसे छोटे element को ढूंढ़कर print करता है।
Example 5: Reverse Array Elements
#include <iostream>
using namespace std;
int main() {
int arr[5] = {1, 2, 3, 4, 5};
for(int i = 4; i >= 0; i--) {
cout << arr[i] << " ";
}
return 0;
}
Output:
5 4 3 2 1
Prints array elements in reverse order.
यह प्रोग्राम array के elements को उल्टे क्रम में print करता है।
Example 6: Copy One Array to Another
#include <iostream>
using namespace std;
int main() {
int source[3] = {10, 20, 30};
int dest[3];
for(int i = 0; i < 3; i++) {
dest[i] = source[i];
}
for(int i = 0; i < 3; i++) {
cout << dest[i] << " ";
}
return 0;
}
Output:
10 20 30
Copies elements from one array to another and prints the destination array.
यह प्रोग्राम एक array के elements को दूसरे array में copy करता है और print करता है।
Example 7: Count Even Numbers in Array
#include <iostream>
using namespace std;
int main() {
int arr[6] = {1, 2, 3, 4, 5, 6};
int count = 0;
for(int i = 0; i < 6; i++) {
if(arr[i] % 2 == 0) {
count++;
}
}
cout << "Even numbers count = " << count;
return 0;
}
Output:
Even numbers count = 3
Counts how many even numbers are present in the array.
यह प्रोग्राम array में कितने even numbers हैं, उन्हें count करता है।
Example 8: Linear Search in Array
#include <iostream>
using namespace std;
int main() {
int arr[5] = {5, 10, 15, 20, 25};
int target = 15;
bool found = false;
for(int i = 0; i < 5; i++) {
if(arr[i] == target) {
found = true;
break;
}
}
if(found)
cout << "Element " << target << " found";
else
cout << "Element not found";
return 0;
}
Output:
Element 15 found
Searches for a specific element in the array using linear search.
यह प्रोग्राम array में एक खास element को खोजता है और बताता है कि मिला या नहीं।
Example 9: Modify Array Elements
#include <iostream>
using namespace std;
int main() {
int arr[3] = {1, 2, 3};
for(int i = 0; i < 3; i++) {
arr[i] *= 2;
}
for(int i = 0; i < 3; i++) {
cout << arr[i] << " ";
}
return 0;
}
Output:
2 4 6
Multiplies each element by 2 and prints the modified array.
यह प्रोग्राम array के हर element को 2 से गुणा करता है और print करता है।
Example 10: Initialize Array with Zero
#include <iostream>
using namespace std;
int main() {
int arr[5] = {0};
for(int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
return 0;
}
Output:
0 0 0 0 0
Initializes all array elements to zero and prints them.
यह प्रोग्राम array के सारे elements को zero से initialize करता है और print करता है।