Arrays In C: Understanding Single ,Double And Multi Dimensions With Examples

Understanding Arrays In C

An Array is the part of Data-Structure that store and organize data values into memory. Array comes in Primitive, Static and Linear data-structures category. 
Primitive Data-Structure is that store pre-defined datatypes values, example: int,char,float,double etc. 

Static Data-Structure's memory size is pre-defined so that we can not store data values beyond the limit. 
Linear Data-Structure stores data elements in sequential or linear form.

Click on link to know more about Data-Structures Basics   

Arrays in C
Arrays In C: ByTechAchievers.com

Array memory allocation starts with 0 index it stores same data-type values in each index, while programming we have to define the data-type and size of the array. 

Above example of an array is like linked list where data storing in linear form.

This is also known as 1-D or "One Dimensional Array"In C there is no limit of array dimensions, it depends on implementation only complex dimension can cause problems with array operations.

We can store large amount of data elements into array dimensions.

Two Dimensional Array is like Table "array of arrays". Same as Three Dimensional
Array is array of tables and so on.


One Dimensional Array -


"One Dimensional" array stores data in form of row. First define the array variable name then it's size in [] brackets.


Program 1 : 

Write a program to store 5 student's marks in array and print them.

    

#include <stdio.h>


void main() {

  
int marks[5]; // 1-D Array Declaration


for(int i =0; i<=4;i++) {
  

printf("Enter student's marks\n");

scanf("%d",&marks[i]); 


}


for(int i =0; i<=4;i++) {
   

printf("%d ",marks[i]);

  
}

}


Program 2 : 

Write a program to enter any string and check whether it is palindrome or not.
example: Madam is palindrome because it is same from backward and forward. 


#include <stdio.h>


void main() {
  

int j = 0,count =0,length;

char string[5]={'m','a','d','a','m'};


length = strlen(string);
  

for(int i =length-1; i>=0;i--) {
  

if(string[j] == string[i]){
   

count++;


}


j++;
    

}

if(count == length) 
 

 printf("String is pallindrome");


else


 printf("String is not pallindrome");
 

}

In the example strlen is the pre-defined method of "C" it finds the length of any string. Length will be calculated from 1 so that in loop we take length-1 because array reads from 0 index.

Note: We can also define character constant within "", ex: string[5] = "madam";

Two Dimensional Array -

"Two Dimensional" array stores data in form of row and column. When we define array declaration one [] shows number of rows and second [] shows number of columns. It is like table. 

As we know array stores data sequentially so that each row's columns will be filled-up first and then 2nd row's turn will come.  

Program 3 : 

Write a program to enter 5 student's details name,address and marks of 3 subjects and calculate total,percentage of each student.



#include <stdio.h>

#include <string.h>



void main() {

  
char name[10],address[100],div[100];


int marks [5][3];

int total,per;


for(int i=0;i<=4;i++) {

total =0;

per = 0;


printf("Please enter student's name\n");

scanf("%s",name);


printf("Please enter student's address\n");

scanf("%s",address);


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

printf("Please enter student's marks \n");

scanf("%d",&marks[i][j]);

total = total+marks[i][j];
}


per = (total*100)/300;


printf("Total is = %d\n",total);

printf("Per is = %d\n",per);

} 

}

Explanation:


Marks[5][3] - storing 3 subjects marks for 5 students.


i loop runs for number of students and j loop runs for each subjects.

1st student's marks entry - marks[0][0], marks[0][1], marks[0][2]

2nd student's marks entry - marks[1][0], marks[1][1], marks[2][2]

3rd student's marks entry - marks[2][0], marks[2][1], marks[2][2]


Note: Two-Dimension array's row and column formation also known as Matrix.

Multi Dimensional Array -

Multi Dimension that is more than two dimension, three dimension, four dimension etc.


Three Dimension : A[2][3][3] - 

2 representing index and each index will have 3 rows and 3 column


1st index - 1st row and 3 columns A[0][0][0],A[0][0][1],A[0][0][2] 

1st index - 2nd row and 3 columns A[0][1][0],A[0][1][1],A[0][1][2] 

1st index - 3rd row and 3 columns A[0][2][0],A[0][2][1],A[0][2][2]
 
2nd index - 1st row and 3 columns A[1][0][0],A[0][0][1],A[0][0][2] 

2nd index - 2nd row and 3 columns A[1][1][0],A[0][1][1],A[0][1][2] 

2nd index - 3rd row and 3 columns A[1][2][0],A[0][2][1],A[0][2][2]

Four Dimension : A[3][2][3][3] 


Five Dimension : A[6][3][2][3][3]  


There is no limit in C for array dimensions.  


Thanks for reading, please do share and comment your suggestions.

4 comments:

Powered by Blogger.