-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_2.cpp
More file actions
44 lines (37 loc) · 1009 Bytes
/
array_2.cpp
File metadata and controls
44 lines (37 loc) · 1009 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <algorithm> // for fill()
using namespace std;
int main() {
// 1. Using fill() to fill array with a fixed value
int arr[5];
fill(arr, arr + 5, 7); // fills all 5 elements with 7
cout << "Array after fill(): ";
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout << "\n\n";
// 2. Fill an array with user input
int userArr[5];
cout << "Enter 5 integers: ";
for (int i = 0; i < 5; i++) {
cin >> userArr[i];
}
cout << "Array filled by user: ";
for (int i = 0; i < 5; i++) {
cout << userArr[i] << " ";
}
cout << "\n\n";
// 3. Multidimensional array (2D array)
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
cout << "2D Array (Matrix):\n";
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}