# Array Of C Programme . Using Of Array (Explanation) .

In previous programs we worked with two variables. We may have to write programs that work on more than one variable. We use arrays in programming to work with more than one variable of the same type.

For example, the first five prime numbers are 2, 3, 5, 7, 11. Now we will use these five numbers in our program. If we want to use normal variables, then we have to declare five variables. But using the array we can use these five prime number programs using a variable.

Arke can think of a box like this, which can contain many small boxes, see the table below.

1 2 3 4 5 … n

Now, to keep our top five prime numbers in our program, we will take a box containing 5 boxes. And take a prime number in each box. The following:

2 3 5 7 11

Instead of 5, we can keep a number of prime numbers in just one array. When we say Arke, what’s in the number one, it will return us two. Similarly, if I say no, then it will return 11.

Declare array:

To use arrays, first declare the array. Arrays are declared just like other general variables. Such as

data_type array_name [size]

The data type tells us what kind of data we will keep in the array. If you keep the intestine, then int, if you put the character, then you have to say char. The integer array can not be placed in the character. Again, floating point can not be placed in the integer array.

The key is to give a name to the array. Then the size. We will keep some alimants in the array by size. For example, to keep five prime numbers, we can write an array like the following:

int prime [5];

Here we have created an array of 5 named Elements. That means we’ll keep 5 integer numbers in it. In this way, we can create arrays for other data type as follows

float gpa [100];

char name [30];

Array Initialize

When we create variables such as a value, we can tell the elimants at the time of creating an array.

int prime [5] = {2, 3, 5, 7, 11};

Called array initialize. When we created the array, we also told its illimant. Comma is to be used for an array of elements from one element to another. And the values are written in the second bracket.

Above we have initialized an array in one line. If we want, we can initialize differently, as follows:

1
2
3
4
5
6

• int prime [5];
• prime [0] = 2;
• prime [1] = 3;
• prime [2] = 5;
• prime [3] = 7;
• prime [4] = 11;

Floating point array

float gpa [4] = {3.5, 3.8, 3.9, 2.9};

Array declares its elimants while not saying the size of the array will not be a problem. Such as:

float gpa [] = {3.5, 3.8, 3.9, 2.9};

Access Array:

Array access to the array to find out an element. Indexing array starts from 0 See the below array

int prime [5] = {2, 3, 5, 7, 11};

The first element of this array is 0 in index 2. The second element or the 1th index contains 3. Thus, the fourth element or the 3th index contains 11.

If we write: prime [0], it will return us two. If I say prime [1] it will return 3. When prime [3] writes, it will return 11. Please see the following program

1
2
3
4
5
6
7
8
9
10
#include
int main ()
{

int prime [5] = {2, 3, 5, 7, 11};

printf (“% d”, prime [0]);

return 0;
}

In the above program, we just printed an array of arrays. You can now write 1, 2, 3, instead of the prime [0] written in the printf function. You will see an index of one instance printed.

Earlier we learned the loop. If we want to access all the elements of an array, we can use the loop. Such as:

1
2
3
4
5
6
7
8
9
10
11
#include
int main ()
{
int i;
int prime [5] = {2, 3, 5, 7, 11};

for (i = 0; i <5; i ++)
printf (“% d \ n”, prime [i]);

return 0;
}

Here if we have a hundred variables in our array, we can easily access it. Just replace i <5 instead of i <100.

Well we now think of a program that will take 6 numbers from the user and add it later and the result will show us. If we had to take 6 variables before then, then we would have to add them and then sum would have to be shown.

Now we can easily do this thing with just one variable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include
int main ()

{

int number [6];
int i, result = 0;

for (i = 0; i <6; i ++) {
printf (“Enter% d no number: \ n”, i + 1);
scanf (“% d”, & number [i]);
result + = Number [i];
}

printf (“result is:% d”, result);

return 0;

}

If we want to print any number of users who have input, then we can print them

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
#include
int main ()

{

int number [6];
int i, result = 0;

for (i = 0; i <6; i ++) {
printf (“Enter% d no number: \ n”, i + 1);
scanf (“% d”, & number [i]);
result + = Number [i];
}

printf (“Result is:% d \ n”, result);

for (i = 0; i <6; i ++) {
printf (“array element% d is:% d \ n”, i, number [i]);

}

return 0;

}

2D arrays

We’ve talked about the array so long that it was a one-dimensional array. Arrays can be multi-dimensional. Such as 2D, 3D etc. Two Dimensional arrays are like the following.

The array is declared as follows

Data_type array_name [i] [j];

There can be any number of values of i and j here. The size of the array will be i * j That means you can keep i * j illimant in the array. Now if i is 5, j is 6, then there are 30 arrays in that array. See the image below

This is a two dimensional character array whose content is 12 Let’s say Array’s name is Character and it should be written in Character [3] [4] The first one is row number, the second is the column number. That means there are 3 rows and 4 columns, the total number is 12.
The first element is the above Character called Two Dimensional Array

To get the first element we need to write Character [0] [0] and it is A

To get the second element, we have to write Character [0] [1] and it is B

To get the third element, we need to write Character [0] [2] and it is C

To get the fourth element, we need to write Character [0] [3] and it is D.

To get the fifth element, we need to write Character [1] [0] and it is E.

To get the sixth element, we need to write Character [1] [1] and it is F
To get the seventh element, we need to write Character [1] [2] and it is G.
In this way we can also get the next element.

If you keep something, then follow the same steps.
Put a character in the first index, for it to be written Character [0] [0]
Already, Array Index starts from zero.
To place a Character in the second index, you must type Character [0] [1]
The rest should be kept in this way.

Think more easily if the above example sounds difficult. We can arrange an array called int num [2] [2]. In this we can keep the maximum 4 elements. For example we took an integer array

int num [2] [2] = {1,2,3,4}

The computer will keep it as follows:

1 2
3 4

1
2
3
4
int [0] [0] = 1;
int [0] [1] = 2
int [1] [0] = 3;
int [1] [1] = 4;
Now if we have an int [0] [1] let’s see, we will see 2 there. Which we can write in this way:

1
2
3
4
int num [2] [2] = {
{1,2},
{3,4}
}
If we take an array named int num [3] [3] then we can keep the maximum 9 elements. The following:

int num [3] [3] = {1,2,3,4,5,6,7,8,9}

The program will keep it as follows:

1 2 3
4 5 6
7 8 9
That is to say

1
2
3
4
5
6
7
8
9
10
11
int [0] [0] = 1;
int [0] [1] = 2;
int [0] [2] = 3;

int [1] [0] = 4;
int [1] [1] = 5;
int [1] [2] = 6 ‘

int [2] [0] = 7;
int [2] [1] = 8;
int [2] [2] = 9;
Which we can initialize in this way:

1
2
3
4
5
6
int num [3] [3] = {
{1,2,3},
{4,5,6}
{7,8,9}
}
In the program, we can easily use 2D arrays using two for loops. See the following program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include

int main ()
{
int i, j;

int num [2] [2] = {10,20,30,40};

for (i = 0; i <2; i ++)
{
for (j = 0; j <2; j ++)
{

printf (“value of num [% d] [% d]:% d \ n”, i, j, num [i] [j]);
}
}
}
When you run that run, you will get the following output

1
2
3
4
value of num [0] [0]: 10
value of num [0] [1]: 20
value of num [1] [0]: 30
value of num [1] [1]: 40
See the following program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include

int main ()
{
int i, j;

int num [2] [2] = {
{10,20},
{30,40}
};

for (i = 0; i <2; i ++)
{
for (j = 0; j <2; j ++)
{

printf (“value of num [% d] [% d]:% d \ n”, i, j, num [i] [j]);
}
}
}
Here’s another way to initialize the array. If you run, you get the same output.

In the above program we worked with a static 2D array. Now we will use input from the user.

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
#include

int main ()
{
int i, j;

int num [2] [2];
for (i = 0; i <2; i ++)
{
for (j = 0; j <2; j ++)
{
printf (“Enter value of num [% d] [% d]:”, i, j);
scanf (“% d”, & num [i] [j]);
}
}

// printing value
for (i = 0; i <2; i ++)
{
for (j = 0; j <2; j ++)
{

printf (“You entered value of num [% d] [% d]:% d \ n”, i, j, num [i] [j]);
}
}
return 0;
}
Here we have an array of 2 * 2 size. If you want, you can work with any size array.

If we do not give value at the time of initializing the array, then it will be filled with null or empty. Look at the following arrow intentions:

1
2
3
4
5
int num [3] [3] = {
{1,2},
{4,5,6}
{7,8}
};
We’ve got an array of 3 * 3 size. But I did not assign value to all. In whatever positions we have not assigned any values, we will get zero in those positions. You can run the following program:

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
#include

int main ()
{
int i, j;

int num [3] [3] = {
{1,2},
{4,5,6}
{7,8}
};

for (i = 0; i <3; i ++)
{
for (j = 0; j <3; j ++)
{

printf (“value of num [% d] [% d]:% d \ n”, i, j, num [i] [j]);
}
}

return 0;

}

Passing an array in the function:

If we want, we can pass an array as an argument to a function. Please see the following program

#include

int getSum (int arr []) {

int sum = 0;
int i;

for (i = 0; i <5; ++ i) {
sum + = arr [i];
}
return sum;
}

int main () {

int balance [5] = {25, 2, 3, 17, 50};

printf (“Sum is:% d”, getSum (balance));

return 0;
}

Here we have written a function named getSum. Which has an integer array as a parameter. And when we call the function we have passed an array.