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:
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
#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:
#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
#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
#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];
}
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.
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:
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:
#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
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:
#include
int main ()
1
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:
#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.
Thank you so much
No comments:
Post a Comment