What is array?
Array is one of important things in C , and it has connection to pointer. Because you can use array for storing a lot of data orderly without making another variable...
For example: if you want to store similar data, you will store it at one place ( folder,etc). That's how array can be use...
When using array,you must give the array name,and you must input the size/capacity (how many data) that can be contain in that array..
Example:
In example above.... name of array is "array1" and the data can be stored is 5..and the data type is integer.You must declare the type of array too.
Array stores data orderly, it always store data start from zero until capacity minus 1.
For example: The array is "a[5]", then it stores data from a[0],a[1],until a[5-1] or 1[4]..
When using array,you must give the array name,and you must input the size/capacity (how many data) that can be contain in that array..
Example:
int array1[5];In example above.... name of array is "array1" and the data can be stored is 5..and the data type is integer.You must declare the type of array too.
Array stores data orderly, it always store data start from zero until capacity minus 1.
For example: The array is "a[5]", then it stores data from a[0],a[1],until a[5-1] or 1[4]..
When we are talking about how to use array... we should use loop to store data..
Why should we use loop?
Of course, we can use manual way to store data in array but what if there is a lot of data that we must insert... it will be troublesome if we input the data manually, then we can use loop to make it easier than using manual way.
Let us see about manual way vs looping:
Manual way:
#include <stdio.h>
int main(){
int a[100];
scanf("%d %d %d %d......",&a[0],&a[1],&a[2]......)// continue until a[99] -_-
}Loop:
#include <stdio.h>
int main{}{
int a[100];
int x;
for(x=0;x<100;x++){
scanf("%d",&a[x]);
}
}
As you can see above, loop is easier than manual way... (=w=)b
When do we need use array?
We need to use array when there is a lot of data and we want to store that data.
For example:
#include<stdio.h>
int main(){
int size=10;
int score[size];
int x;
for(x=0;x<size;x++){
scanf("%d",&score[x]);
}
for(x=0;x<size;x++){
printf("%d",score[x]);
}
}
That will show, that array stores a lot of data..
We can apply to this way to:
#include<stdio.h>
int main(){
int size=10;
int score[size];
int x;
for(x=0;x<size;x++){
scanf("%d",&score[x]);
x+=10;
}
for(x=0;x<size;x++){
printf("%d",score[x]);
}
}
In that way, we can add 10 to every data, without adding 10 one by one to data... There is also multi-dimension array..
The difference with array are the size / capacity and how it stores data.
The capacity of array depend on the size of each dimension.
For Example:
int a[2][5];It means the capacity of array "a" is 2 times 5 =10 data.
It can store 10 data, but it stores data similar matrix structure and you should use loop to store it to make it easier. The process of storing data for example above is like this:
#include<stdio.h>
int main(){
int x,y,a[2][5];
for(x=0;x<2;x++){
for(y=0;y<5;y++){
scanf("%d",&a[x][y]);
}
}
}It will goes from a[0][0]---because array always store from "0". to a[2-1][5-1] or a[1][4]....
if you want to see the matrix form, you only need to use printf command like this:
#include<stdio.h>
int main(){
int x,y,a[2][5];
for(x=0;x<2;x++){
for(y=0;y<5;y++){
scanf("%d",&a[x][y]);
}
}
for(x=0;x<2;x++){
for(y=0;y<5;y++){
printf("%d",a[x][y]);
}
printf("\n");
}
}
Then you can see how multidimension array works.....
Thank you for watching =w=
No comments:
Post a Comment