-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathARRAY
More file actions
119 lines (85 loc) · 2.37 KB
/
ARRAY
File metadata and controls
119 lines (85 loc) · 2.37 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
Arrays are like containers that hold multiple values of the same type and stored together under one name.
Instead of creating separate variables like a, b, c, we can declare a single array (e.g., marks[]) that can hold all those values in a structured way.
1] Array :
#include <stdio.h>
int main() {
int arr[5] = {2, 4, 8, 12, 16};
// Print each element of
// array using loop
printf("Printing Array Elements\n");
for(int i = 0; i < 5; i++){
printf("%d ", arr[i]);
}
printf("\n");
// Printing array element in reverse
printf("Printing Array Elements in Reverse\n");
for(int i = 4; i>=0; i--){
printf("%d ", arr[i]);
}
return 0;
}
2] Update Array Element:
#include <stdio.h>
int main() {
int arr[5] = {2, 4, 8, 12, 16};
// Update the first value
// of the array
arr[0] = 1;
printf("%d", arr[0]);
return 0;
}
3] Accessing Array Elements:
#include <stdio.h>
int main() {
// array declaration and initialization
int arr[5] = {2, 4, 8, 12, 16};
// accessing element at index 2 i.e 3rd element
printf("%d ", arr[2]);
// accessing element at index 4 i.e last element
printf("%d ", arr[4]);
// accessing element at index 0 i.e first element
printf("%d ", arr[0]);
return 0;
}
4] Size of array :
#include <stdio.h>
int main() {
int arr[5] = {2, 4, 8, 12, 16};
// Size of the array
int size = sizeof(arr)/sizeof(arr[0]);
printf("%d", size);
return 0;
}
5] Double dimensional array:
// C program to demonstrate working of 2D arrays
#include <stdio.h>
int main() {
/*
Declare and initialize a 2×2 integer array.
arr[0][0] = 10, arr[0][1] = 20,
arr[1][0] = 30, arr[1][1] = 40
*/
int arr[2][2] = { {10, 20}, {30, 40} };
printf("2D Array Elements:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main() {
// Create and initialize an array with 3 rows
// and 2 columns
int arr[3][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
// Print each array element's value
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
printf("arr[%d][%d]: %d ", i, j, arr[i][j]);
}
printf("\n");
}
return 0;
}