types of array in c programming
In this article, today learn types of arrays in c programming one-dimensional array and
two-dimensional array as we have seen some programs on a one-dimensional array
now we will go for a two-dimensional array.
along with
pointers in both arrays of one dimensional and two dimensional two dimensional
array an array with two subscripts is called as two dimensional array it is
nothing but a matrix which consists of rows and columns
1.one dimensional array
2.two dimensional array
1.one dimensional array:
One Dimensional (One-D) Array Programs / Examples - This section contains solved programs on One Dimensional Array, here you will
learn about the declaration, initialization of the array and other basic and
advanced operations on array like reading, printing array elements, sum and
product of array elements, merging two arrays, adding and subtracting two
arrays elements, swapping adjacent elements, sorting, searching, etc.
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int arr[50], n;
printf("How many element you
want to store in the array ? ");
scanf("%d",&n);
printf("Enter %d element to
store in the array : ",n);
for(int i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
printf("The Elements in the
Array is : \n");
for(i=0; i<n; i++)
{
printf("%d ",arr[i]);
}
getch();
}
1.
two
dimensional array:
two dimensional array can be defined as an
array which holds some more arrays in it usually it is in contiguous memory
locations that we'll see in pointers and coming to seeing matrix which consists
of rows and columns that is for our representation sake
the syntax is the type again which is which
can be integer float or character and the variable name of the array along
with the row size and column size so if it is an only single set of brace it is
one dimensional array but here as you can see two dimensional array is nothing
but two set of sizes given for it and this is how we will initialize it an example
offset is so we'll go for a slight initialization of this two dimensional array.
example:
#include<stdio.h>
int
main(){
int
i=0,j=0;
int
arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
//traversing
2D array
for(i=0;i<4;i++){
for(j=0;j<3;j++){
printf("arr[%d] [%d] = %d
\n",i,j,arr[i][j]);
}//end of j
}//end of
i
return
0;
}
the main function normally west. array of
limit 5 this is single-dimensional but if you say this is our two dimensional
array and the initialization happens in this way over a brace now comes our
saying two three four and five the five things are represented by this size
and three C's internally we need to give three values.
four five six seven eight nine now if I stick
to three over here, it will be a three by three array will stick to four by
three arrays so this is the ending brace ending race starting race values 33 our
nth column is what I need row and column 4 are equal to 0 or less than 4 R + +
+ 4 columns equal to 0 columns less than 3 columns plus now say printf the
value comma array of wrote and column value after this parental newline a character so that we can represent it in an array format when you execute it
and he forgot about commas over here okay so this is how your array will be
represented in a matrix format way so this is
a perfect metric shape the scan the values in the array similarly like we are
printing we need to scan it for Rho equal to 0 and Rho less than 4 Rho plus +
and for column equal to 0 columns less than 3 columns plus say scan if
person D comma have a son array of row and column that's it with this we can
scan some values with the values B the values up in this way it's a 4 by 3
matrix as you can see so consider this is a RR with which we are scanning
printf first array newline character and with this will scan for the new
languor faster now similarly go for BRR of again 4 and 3 of size likewise CRR
again of 4 and 3 off sighs so with this we can add a and B RS and place the
addition in C so here I am receiving first array values likewise I'll print
allows the user.
for secondary values in which
fur so R equal to 0 and R less than 4 R plus and likewise for column equal
to 0 columns less than 3 and column plus now say scan if person D comma
ampersand BR r of r and c with this will scan the second values now after this
will clear the entire screen and then now it comes
to printing of ARR likewise, we can go for printing a newline character after
that for R equal to 0 R less than 4 R plus and 4 columns equal to 0 column
less than 3 column plus plus and say a printf person D comma B are operating
rows and columns and say Newland character format lecture presentation sake now
finally in the CR at the array are less than 4 R + + + 4 column equal to 0
column less than 3 columns plus say crl of art and seat should take up the
addition of errors earth and seat + B are ours are then suite column and print
there itself dear dad values person D comma C are earth are and seats value and
print a new line character.
for the matrix of presentation sake so here
thing goes is a loop renting enter first error for that and scan for first
status as say from the user to print secondary else get the secondary contents
and I'll clear the previous outputs and I'll print the first arraign matrix
format after that to newline characters I'll go for printing the secondary in
matrix format than three newline characters finally printing the addition of
both first and second arrays which I can say addition is in this way for clear
screen we should use hashing glue corner dot H now execute the program it is
asking me to enter first error values let it be one all the time now its
secondary let it be two all the time after that it will clear the entire
previous outputs displaying.
the first error in the second array now the
addition is three so this way addition and subtraction are possible in
two-dimensional array which is which we call as matrix addition and subtraction
by using arrays now in match we know we have transfers of a matrix that is
changing the rows and columns if you have an array which is of consider 3 by 2
size 3 by 2 size and will scan certain values for row and column saying for R
equal to 0 R less than 3 and R plus plus likewise for column equal to 0 column
less than 2 columns plus now say scan s person D comma ampersand array of
row and column now printed for R equal to 0 R less than 3 R plus and for
column equal to 0 column less than 2 column plus plus printf person D comma
array of Rouen column after that printer newline character put them in a set of
braces.
now trending in will change the row and column
row is zero and are less than three R + + + 4 column equal to 0 column less
than 2 columns + + now while printing instead of saying array of ruin called a
missile I'll interchange them here it's
row and here it's a column and sees what happens this time printf a new line
character and off main and here we'll say one more newline character.
now execute the program as you can see here it
is expecting me to give out 3 4 5 6 so actually, it is 1 2 3 4 5 6 now it is 1 3
2 4 and 5 3 5 so this way we can interchange our rows and columns making it a
transpose of a matrix character array as we are saying integer array all the
time if we say a character is in 4 in front of it it is a character array and
this is how we define character STR orbit and goodbye all so this is how you
can even define a character array there is a separate chapter on character array
where which is called as strings where we'll be seeing the programming concepts
on strings but as of now we will take pointers in two-dimensional arrays
integer array holds some ten values like going for single-dimensional of certain values and lets I will be the one so that
I can repeat the loop I less than five and I plus very deaf person D comma
ampersand area of I array of I let it be zero the address happens to be and
instead of saying area of zero if you say m % 0 will get the address the
concept of addresses and pointers will see in greater detail when we enter into
pointers.
No comments:
Post a Comment