-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.html
More file actions
401 lines (338 loc) · 15.2 KB
/
array.html
File metadata and controls
401 lines (338 loc) · 15.2 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
<title>CodingElite</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark navm">
<div class="container-fluid">
<a class="navbar-brand" href="index.html">CodingElite</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link " aria-current="page" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link " href="/codingelite.org/blog.html">Blog</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link active dropdown-toggle" href="#" id="navbarDropdown" role="button"
data-bs-toggle="dropdown" aria-expanded="false">
C Tutorials
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="/codingelite.org/introduction.html">introduction</a></li>
<li><a class="dropdown-item" href="/codingelite.org/branching.html">Branching Statements</a></li>
<li><a class="dropdown-item" href="/codingelite.org/looping.html">looping statements</a></li>
<li><a class="dropdown-item" href="/codingelite.org/function.html">Function</a></li>
<li><a class="dropdown-item" href="/codingelite.org/array.html">Array</a></li>
<li><a class="dropdown-item" href="/codingelite.org/string.html">string</a></li>
<li><a class="dropdown-item" href="/codingelite.org/pointer.html">pointers</a></li>
<li><a class="dropdown-item" href="/codingelite.org/structure.html">structures</a></li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="/codingelite.org/blog.html">More Topics</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="/codingelite.org/book.html">Books</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/codingelite.org/about.html">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/codingelite.org/contact.html">Contact us</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="bg-light">
<div class="container bg-c">
<h1 class="hed hedi-h1"><strong> Array </strong></h1>
<h3>Array</h3>
<p>Array is a collection of data elements, all of same data type, accessed using a common name. Array
elements are placed in contiguous memory location. Array is one type of data structure that can store a
fixed-size sequential collection of items of the same type. An array is a variable that can store multiple
values. Array is the collection of homogeneous data elements.
</p>
<img class="img-fluid" src="img/Array12.png" alt="Array structure">
<h5>Declaration:</h5>
<p class="2">To declare array in C, a programmer specifies the data type of items, array name and numbers of
items
required by an array as follows - <br>
<h6>One dimensional array:</h6>
<div class="row">
<div class="col-md-6">
<pre class="pre-bl">type arrayname[arraysize];</pre>
</div>
</div>
<br>This is single-dimentional array. Array size must be positive integer. <br>
<br>
<h6>Two dimensional array:</h6>
<div class="row">
<div class="col-md-6">
<pre class="pre-bl">type arrayname[i][j];</pre>
</div>
</div>
<br>here: i= numbers of rows and j= numbers of columns
<br>This is called 2-Dimentional array. 2D array represent matrices.
<br><b>Example:</b>
<ul>
<li>int a[5];</li>
<li>int a[3][2];</li>
</ul>
<h5>Types of array:</h5>
<p>In C programming language, there is two type of array-</p>
<ol>
<li>One dimentional(1-D) Array</li>
<li>Two dimentional(2-D) Array or Multi-Dimentional array</li>
</ol>
<h4>Advantages of array:</h4>
<ol>
<li> Array represent multiple data elements of the same type using a single name.</li>
<li>In arrays, the elements can be accessed randomly by using the index number.</li>
<li> Array consist of contiguous memory locations therefore there is no chance of extra memory being
allocated in case of arrays</li>
<li>2D Array is used to represent matrices.</li>
<li> Using arrays, we can implement other data structure like stack, queues, linked list, trees,
graphs etc.</li>
</ol>
<h4>Disadvantages of Array:</h4>
<ol>
<li> Using arrays, we can implement other data structure like stack, queues, linked list, trees,
graphs etc.</li>
<li>Allocating more memory than the requirement leads to wastage of memory and less allocation of memory
leads problem.</li>
<li>Insertion and deletion quit difficult in an arrays.</li>
<li> The array is homogeneous(means only one type of value can be store in the array.</li>
</ol>
<h4>Applications:</h4>
<ul>
<li> A basic application of arrays can be storing data in tabular format.</li>
<li> 2D arrays known as matrix,are used in image processing.</li>
<li> Arrays can be used for CPU scheduling.</li>
<li> Arrays are used to implement mathematical vectors, matrices and rectangular table.</li>
<li>Arrays are used to implement data structures like stack, queue, linked list, trees, graphs etc.</li>
</ul>
<h4>Sample Programs</h4>
<div class="col-md-8">
<h5 class="hed-m">1. Initialization of 1-D array at compile time</h5>
<pre class="pre-bl" title="compile time initialization">
// Print the elements stored in the array
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
void main()
{
int a[5]={1,2,3,4,5};
clrscr();
// printing elements of an array
for(int i = 0; i < 5; i++)
{
printf("%d\n", a[i]);
}
getch();
}</pre>
<h6>output:</h6>
<pre class="pre-bl" title="">
1
2
3
4
5
</pre>
</div>
<div class="col-md-8">
<h5 class="hed-m">2. Initialization of 1-D array at run time </h5>
<pre class="pre-bl" title="Run time Initialization">
// Print the elements stored in the array
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
<span class="hljs-meta-keyword">#include<conio.h></span>
</span>
void main()
{
int a[5];
clrscr();
printf("Enter 5 elements in the array/n");
scanf("&d", &a[i]);
// taking input and storing it in an array
for( i = 0; i < 5; i++) {
scanf("%d", &a[i]);
}
printf("Displaying array elements/n");
// printing elements of an array
for(int i = 0; i < 5; i++)
{
printf("%d\n", a[i]);
}
getch();
}
</pre>
<h6>Output:</h6>
<pre class="pre-bl" title="">
Enter 5 elements in the array
10
20
30
40
50
Displaying array elements
10
20
30
40
50
</pre>
</div>
<div class="col-md-8">
<h5 class="hed-m">3.Sum of array 1-D elements</h5>
<pre class="pre-bl" title="Sum of array elements">
//Print sum of array elements
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
</span>
int main()
{
int array[5];
int sum, i;
sum = 0;
printf("Enter values in 1-D array/n")
for(i = 0; i < 6; i++);
{
scanf("%d",array[i]);
}
for(i = 0; i < 6; i++)
{
sum = sum + array[i];
}
printf("Sum of array is %d", sum);
return 0;
}
</pre>
<h6>Output:</h6>
<pre class="pre-bl" title="">
Enter values in 1-D array
1
2
3
4
5
Sum of array is 15
</pre>
</div>
<div class="col-md-8">
<h5 class="hed-m">4. Initialization of 2-D array at compile time</h5>
<pre class="pre-bl" title="">
//program for compile time array initialization
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
</span>
void main()
{
int a[2][2]={{1,2},{3,4}};
int i,j;
printf(“Array elements are\n”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“%d\n”,a[i][j]);
}
}
return 0;
}
</pre>
<h6>Output:</h6>
<pre class="pre-bl" title="">
Array elements are
1 2
3 4
</pre>
</div>
<div class="col-md-8">
<h5 class="hed-m">5. Initialization of 2-D array at run time</h5>
<pre class="pre-bl" title="">
// Print the elements stored in the array
<span class="hljs-meta">
<span class="hljs-meta-keyword">#include<stdio.h></span>
</span>
void main()
{
int a[2][2];
int i,j;
printf(“Enter the array elements \n”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“Array elements are \n”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“%d\n”,a[i][j]);
}
}
return 0;
}
</pre>
<h6>Output:</h6>
<pre class="pre-bl" title="">
Enter the array elements
5
6
7
8
Array elements are
5 6
7 8
</pre>
</div>
<h5>Examples For Practice</h5>
<ol>
<li>Implement C program to find smallest element in a 1-D array</li>
<li>Implement C program to find the average of N elements of 1-D array. (Read N from user).</li>
<li>Implement C program to find the sum of all elements in a 2-D array.</li>
<li>Implement C program to display even and odd numbers from given 2-D array</li>
</ol>
<a href="/codingelite.org/function.html"> <button type="button" class="btn btn-primary btn-m"> <<Previous</button> </a>
<a href="/codingelite.org/string.html"> <button type="button" class="btn btn-primary btn-m btn-r">Next >></button></a>
</div>
</div>
<footer class="page-footer font-small blue navbar-dark bg-light footer-ptt">
<div class="footer-copyright text-center py-3">
© Copyright 2021. All Right Reserved. | Designed by the <a href="/codingelite.org/about.html">CodingElite team</a>. |
<a class="color-b" href="/codingelite.org/privacyPolicy.html">Privacy policy</a> | <a class="color-b" href="/codingelite.org/contact.html">Contact us</a>
</div>
</footer>
<!-- Optional JavaScript; choose one of the two! -->
<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW"
crossorigin="anonymous"></script>
<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js" integrity="sha384-q2kxQ16AaE6UbzuKqyBE9/u/KzioAlnx2maXQHiDX9d4/zp8Ok3f+M7DPm+Ib6IU" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.min.js" integrity="sha384-pQQkAEnwaBkjpqZ8RU1fF1AKtTcHJwFl3pblpTlHXybJjHpMYo79HY3hIi4NKxyj" crossorigin="anonymous"></script>
-->
</body>
</html>