Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Saturday, November 14, 2020

Array in C | part-2

Array is a collection of elements of similar data type stored at contiguous memory location. An array can be of any type like int, char, float, double

Array in C - part-2 | welcome2protec


Memory representation of an array:

All the array elements in C occupy sequential block of memory spaces and each memory block is of same size. For example, int num[6]; /* An integer array of 6 elements*/ it's length is 6, so this array will get 6 sequential memory block in the RAM and each memory block has a unique address in byte. 



There is a difference of 4 among the address of subsequent neighbors, since an integer type array holds 4 bytes of memory, therefor each block will occupy memory of 4 bytes. For example, address of 1st block is 1000, so 2nd block will be 1004, 3rd block 1008, 4rth block 10012, 5th block 1016, and last block will be 1020 as you can see in below diagram. 

Memory representation of array | welcome2protec
Memory representation of array

Array Indexing:

The location of an elements in an array is represented by numerical value which is called array index. In programming languages, the indexes always start with 0, meaning that the first elements has index 0, and second has index 1, third has index 2, similarly it continue through the natural numbers until the last index (N-1). Where N represents the length of an array. Let's have a look at the below example:

Array Indexing | welcome2protec.com

Note: The elements of an array share the same variable name but each element has its own unique index number (also known as a subscript) see the above diagram.


Accessing elements of an array:

you can use index number (subscript) to access any elements stored in array. Since we know that, The elements of an array share the same variable name but each element has its own unique index number (also known as a subscript) see array indexing. This is how you can access the array elements.

/* Array Declaration with initialization */
int num[] = {2, 4, 6, 8, 10, 12};

num[0] = 2;  // 1st element of array, since num[0] represent 1st index
num[3] = 8;  // 4th element of array, since num[3] represent 4th index

/* Last element of array, since num[5] represent 6th index i.e last index of array.*/
num[5] = 12;

Let's understand with below example


Example of Array: Program to find average of  4 numbers.

#include <stdio.h> 
int main()
{
    int avg = 0;
    int sum =0;
    int x=0;

    /* Array- declaration – length 4 */
    int num[4];

    /* We are using a for loop to traverse through the array
     * while storing the entered values in the array
     */
    for (x=0; x<4; x++)
    {
      printf("Enter number %d \n", (x+1));
      scanf("%d", &num[x]);
    }
    
    /* Displaying elements of array */
    for (x=0; x<4; x++)
    {
        sum = sum+num[x];
    }

    avg = sum/4;
    printf("Average of entered number is: %d", avg);
    return 0;
}

Output

Enter number 1 
10
Enter number 2 
10
Enter number 3 
20
Enter number 4 
40
Average of entered number is: 20

How to store elements in array?

Here, we are traversing the array from 0 to 9 since length of array is 10. Inside the loop we are taking input form user of any 10 numbers. All the input numbers will be stored in corresponding array using scnaf() function. Look at the below statements taken from the above example.

/* We are using a for loop to traverse through the array
 * while storing the entered values in the array */
for (x=0; x<4; x++)
{
  printf("Enter number %d \n", (x+1));
  scanf("%d", &num[x]);
}

How to display elements from an array?

Again you need to run a for loop to display the stored elements of an array. Look at the below statements taken from the above example.

/* Displaying elements of array */
for (x=0; x<4; x++)
{
  sum = sum + num[x];
}

How to calculate the size of an array?

You can calculate the size of an array in bytes by using sizeof operator.

Example: Size of array in bytes using sizeof() operator

/* Calculating size of an int array using sizeof operator */
#include <stdio.h> 
int main()
{
  int arr[6] = {2, 4, 6, 8, 10, 12};
  printf("size of arr = %d bytes", sizeof(arr));
  return 0;
}
  

Output

Size of arr = 24 bytes

How to calculate the length of an array?

There are various technique to find length of array in C, by using sizeof() operator and without using sizeof() operator. Let's see the both technique.

Length of array = sizeof (arr) / sizeof (arr[0])

Example 1: Length of an array using sizeof operator

/* Calculating the length of int array using sizeof operator */
  #include <stdio.h>
  int main()
  {
    int arr[6] = {2, 4, 6, 8, 10, 12};
    printf("Length of arr = %d", sizeof(arr) / sizeof(arr[0]));
return 0; }

Output

Length of arr = 6 

Example 2: Length of an array without sizeof() operator

/* Calculating the length of int array without sizeof() operator */
  #include <stdio.h>
  int main()
  {
    int arr[6] = {2, 4, 6, 8, 10, 12};
    int i = 0;
    while(arr[i] !=12)
    {
      i++;
    }
    printf("Length of arr = %d",i+1);
    return 0;
  }
  

Output

Length of arr = 6 










Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us