What is right way to initialize 1d array?

                        How to initialize an 1d array?

  • Array is a collection or group similar types of data elements. 
  • All the elements of c array are homogeneous (similar). It has contiguous memory location.
  • C array is beneficial if you have to store similar elements. Suppose you have to store marks of 50 students, one way to do this is allotting 50 variables. So it will be typical and hard to manage that's why we use array. 
  • For example we can not access the value of these variables with only 1 or 2 lines of code.
  • Another option is to use an array. We may quickly retrieve the elements by using an array. To access the elements of an array, only a few lines of code are required..
How to declare an array?

dataType arrayName[arraySize];
  • For example,
float mark[5];
  • Here, we declared an array, mark, of floating-point type and its size is 5. 
  • Meaning, it can hold 5 floating-point values.
Note:- It's important to note that the size and type of an array cannot be changed once it is declared.
1D Array

Note:-Some important Points to be noted
  • Arrays have 0 as the first index not 1. So, mark[0] is the first element.
  • If the size of an array is n, to access the last element, (n-1) index is used. So, mark[4] is the last element.
  • Suppose the starting address of mark[0] is 2140. Then, the next address, a[1], will be 2144 (Floating number have 4 bytes space in memory), address of a[2] will be 2148 and so on. It's because the size of float is 4 bytes.
Let's discuss how to initialize an array in C programming.
  • It's possible to initialize an array during declaration. For example,
             int mark[5] = {19, 20, 21, 26, 28};
  • Another method to initialize array during declaration:
             int mark[] = {19, 20, 21, 26, 28};

Let's know how to insert and print array elements
int mark[5] = {19, 20, 21, 26, 28};

For Example

#include<stdio.h>
int main()
{
 int n[5]={19,20,21,26,28};
 printf("%d", n[0]);
 printf("%d", n[1]);
 printf("%d", n[2]);
 printf("%d", n[3]);
 printf("%d", n[4]);
}

Output: 19 20 21 26 28

Example:
#include<stdio.h>
int main()
{
 int a[5],i;
 for(i=0;i<5;i++);
 {
  scanf("%d",&a[i]);
 }
 printf("display");
 {
  for(i=0;i<5;i++);
  {
   printf("%d",a[i]);
  }
 }
}
Output:
99

Example of One Dimensional Array in C
#include <stdio.h>    
#include <conio.h>    
void main(){    
int i=0;  
int marks[5];//declaration of array  
clrscr();    
marks[0]=80;//initialization of array  
marks[1]=60;  
marks[2]=70;  
marks[3]=85;  
marks[4]=75;  
//traversal of array  
for(i=0;i<5;i++){    
printf("%d \n",marks[i]);  
}//end of for loop  
getch();    
}

Output
80
60
70
85
75

Array example with for loop.
#include<stdio.h>
int main()
{
    int arr[5];
    int i;
    printf("\n Enter the array elements : ");
    for(i = 0; i<5; i++)
    {
        scanf("%d", &arr[i]);
    }
    printf("\n The array elements are : ");
    for(i = 0; i<5; i++)
    {
        printf(" %d ", arr[i]);
    }
    return 0;
}
Output:
Enter the array elements : 10 20 30 40 50
The array elements are : 10 20 30 40 50

Post a Comment

Previous Post Next Post