Pages

Showing posts with label Arrays. Show all posts
Showing posts with label Arrays. Show all posts

ARRAYS UNIT 5 PART 1


Introduction
An array is a data structure used to process multiple elements with the same data type when a number of such elements are known. You would use an array when, for example, you want to find out the average grades of a class based on the grades of 50 students in the class. Here you cannot define 50 variables and add their grades. This is not practical. Using an array, you can store grades of 50 students in one entity, say grades, and you can access each entity by using subscript as grades[1], grades[2]. Thus you have to define the array of grades of the float data type and a size of 50. An array is a composite data structure; that means it had to be constructed from basic data types such as array integers.
Program
#include <stdio.h>
main()
{
    int a[5];  \\A
    for(int i = 0;i<5;i++)
    {
       a[i]=i;\\B
    }
    printarr(a);
}
void printarr(int a[])
{
    for(int i = 0;i<5;i++)
    {
        printf("value in array %d\n",a[i]);
    }
}
Explanation
  1. Statement A defines an array of integers. The array is of the size 5—that means you can store 5 integers.
  2. Array elements are referred to using subscript; the lowest subscript is always 0 and the highest subscript is (size –1). If you refer to an array element by using an out-of-range subscript, you will get an error. You can refer to any element as a[0], a[1], a[2], etc.
  3. Generally, you can use a for loop for processing an array. For the array, consecutive memory locations are allocated and the size of each element is same.
  4. The array name, for example, a, is a pointer constant, and you can pass the array name to the function and manipulate array elements in the function. An array is always processed element by element.
  5. When defining the array, the size should be known.
Note 
The array subscript has the highest precedence among all operators thus a[1] * a[2] gives the multiplication of array elements at position 1 and position 2.
Points to Remember
  1. An array is a composite data structure in which you can store multiple values. Array elements are accessed using subscript.
  2. The subscript operator has the highest precedence. Thus if you write a[2]++,it increments the value at location 2 in the array.
  3. The valid range of subscript is 0 to size −1.
ADDRESS OF EACH ELEMENT IN AN ARRAY
Introduction
Each element of the array has a memory address. The following program prints an array limit value and an array element address.
Program

#include <stdio.h>
void printarr(int a[]);
main()
{
    int a[5];
    for(int i = 0;i<5;i++)
    {
        a[i]=i;
    }
    printarr(a);
}
void printarr(int a[])
{
    for(int i = 0;i<5;i++)
    {
        printf("value in array %d\n",a[i]);
    }
}
void printdetail(int a[])
{
    for(int i = 0;i<5;i++)
    {
        printf("value in array %d and address is %16lu\n",a[i],&a[i]);
\\ A
    }
}
Explanation
  1. The function printarr prints the value of each element in arr.
  2. The function printdetail prints the value and address of each element as given in statement A. Since each element is of the integer type, the difference between addresses is 2.
  3. Each array element occupies consecutive memory locations.
  4. You can print addresses using place holders %16lu or %p.
Point to Remember
For array elements, consecutive memory locations are allocate.

ACCESSING AN ARRAY USING POINTERS

Introduction

You can access an array element by using a pointer. For example, if an array stores integers, then you can use a pointer to integer to access array elements.

Program

#include <stdio.h>
void printarr(int a[]);
void printdetail(int a[]);
main()
{
    int a[5];
    for(int i = 0;i<5;i++)
    {
        a[i]=i;
    }
    printdetail(a);
}
void printarr(int a[])
{
    for(int i = 0;i<5;i++)
    {
        printf("value in array %d\n",a[i]);
    }
}
void printdetail(int a[])
{
    for(int i = 0;i<5;i++)
    {
        printf("value in array %d and address is %8u\n",a[i],&a[i]);
    }
}
void print_usingptr(int a[]) \\ A
{
    int *b;    \\ B
    b=a;               \\ C
    for(int i = 0;i<5;i++)
    {
        printf("value in array %d and address is %16lu\n",*b,b); \\ D
        b=b+2; \\E
    }
}

Explanation

1.      The function print_using pointer given at statement A accesses elements of the array using pointers.
2.      Statement B defines variable b as a pointer to an integer.
3.      Statement C assigns the base address of the array to b, thus the array's first location (a[0]) is at 100; then b will get the value 100. Other elements of the array will add 102,104, etc.
4.      Statement D prints two values: *b means the value at the location specified by b, that is, the value at the location 100. The second value is the address itself, that is, the value of b or the address of the first location.
5.      For each iteration, b is incremented by 2 so it will point to the next array location. It is incremented by 2 because each integer occupies 2 bytes. If the array is long then you may increment it by 4.

Points to Remember

1.      Array elements can be accessed using pointers.
2.      The array name is the pointer constant which can be assigned to any pointer variable.

ARRAYS UNIT 5 PART 2


MANIPULATING ARRAYS USING POINTERS
Introduction
When the pointer is incremented by an increment operator, it is always right incremented. That is, if the pointer points to an integer, the pointer is incremented by 2, and, if it is long, it is incremented by 4.
Program

#include <stdio.h>
void printarr(int a[]);
void printdetail(int a[]);
void print_usingptr(int a[]);
main()
{
    int a[5];
    for(int i = 0;i<5;i++)
    {
        a[i]=i;
    }
    print_usingptr(a);
}
void printarr(int a[])
{
    for(int i = 0;i<5;i++)
    {
        printf("value in array %d\n",a[i]);
    }
}
void printdetail(int a[])
{
    for(int i = 0;i<5;i++)
    {
        printf("value in array %d and address is %8u\n",a[i],&a[i]);
    }
}
void print_usingptr(int a[])
{
    int *b;
    b=a;
    for(int i = 0;i<5;i++)
    {
        printf("value in array %d and address is %16lu\n",*b,b);
        b++;                  // A
    }
}
Explanation
  1. This function is similar to the preceding function except for the difference at statement A. In the previous version, b = b+2 is used. Here b++ is used to increment the pointer.
  2. Since the pointer is a pointer to an integer, it is always incremented by 2.
Point to Remember
The increment operator increments the pointer according to the size of the data type.
ANOTHER CASE OF MANIPULATING AN ARRAY USING POINTERS
Introduction
You can put values in the memory locations by using pointers, but you cannot assign the memory location to an array to access those values because an array is a pointer constant.
Program
#include <stdio.h>
void printarr(int a[]);
void printdetail(int a[]);
void print_usingptr_a(int a[]);
main()
{
    int a[5];
    int *b;
    int *c;
    for(int i = 0;i<5;i++)
    {
        a[i]=i;
    }
    printarr(a);
    *b=2;            \\ A
    b++;             \\ B
    *b=4;            \\ C
    b++;
    *b=6;            \\ D
    b++;
    *b=8;            \\ E
    b++;
    *b=10;
    b++;
    *b=12;
    b++;
    a=c; //error     \\F
    printarr(a);


}
void printarr(int a[])
{
    for(int i = 0;i<5;i++)
    {
        printf("value in array %d\n",a[i]);
    }
}
void printdetail(int a[])
{
    for(int i = 0;i<5;i++)
    {
        printf("value in array %d and address is %16lu\n",a[i],&a[i]);
    }
}

void print_usingptr_a(int a[])
{

    for(int i = 0;i<5;i++)
    {
        printf("value in array %d and address is %16lu\n",*a,a); \\ F
        a++; // increase by 2 bytes         \\ G
    }
}
Explanation
  1. You can assign a value at the location specified by b using statement A.
  2. Using statement B, you can point to the next location so that you can specify a value at that location using statement C. Using this procedure, you can initialize 5 locations.
  3. You cannot assign the starting memory location as given by statement F to access those elements because a is a pointer constant and you cannot change its value.
  4. The function print_usingptr_a works correctly even though you are writing a++. This is because when you pass a as a pointer in an actual parameter, only the value of a is passed and this value is copied to the local variable. So changing the value in the local variable will not have any effect on the outside function.
Point to Remember
The array limit is a pointer constant and you cannot change its value in the program.
TWO-DIMENSIONAL ARRAY
Introduction
You can define two- or multi-dimensional arrays. It is taken as an array of an array. Logically, the two-dimensional array 3 X 2 is taken as
 3     1
 5     2
 8     7
Here there are three arrays, i.e. one array in each row. The values are stored as
3   1  5   2    8  7
This style is called row measure form. Each row array is represented as a[0], which consists of elements 3 and 1. a[1] consists of 5 2 and a[2] consists of 8 7. Each element of a[0] is accessed as a [0] [0] and a[0] [1], thus the value of a[0][0] and a[0][1] is 1.
Program

#include <stdio.h>
void printarr(int a[][]);
void printdetail(int a[][]);
void print_usingptr(int a[][]);
main()
{
    int a[3][2];      \\ A
    for(int i = 0;i<3;i++)
        for(int j=0;j<2 ;j++)
        {
               {
                     a[i]=i;
               }
        }
    printdetail(a);
}
void printarr(int a[][])
{
    for(int i = 0;i<3;i++)
        for(int j=0;j<2;j++)
        {
               {
                      printf("value in array %d\n",a[i][j]);
               }
        }
}
void printdetail(int a[][])
{
    for(int i = 0;i<3;i++)
        for(int j=0;j<2;j++)
        {
               {
                      printf(
                      "value in array %d and address is %8u\n",
                      a[i][j],&a[i][j]);
               }
        }
}
void print_usingptr(int a[][])
{
    int *b;   \\ B
    b=a;                \\ C
    for(int i = 0;i<6;i++)   \\ D
    {
        printf("value in array %d and address is %16lu\n",*b,b);
        b++; // increase by 2 bytes \\ E
    }
}
Explanation
  1. Statement A declares a two-dimensional array of the size 3 × 2.
  2. The size of the array is 3 × 2, or 6.
  3. Each array element is accessed using two subscripts.
  4. You can use two for loops to access the array. Since i is used for accessing a row, the outer loop prints elements row-wise, that is, for each value of i, all the column values are printed.
  5. You can access the element of the array by using a pointer.
  6. Statement B assigns the base address of the array to the pointer.
  7. The for loop at statement C increments the pointer and prints the value that is pointed to by the pointer. The number of iterations done by the for loop, 6, is equal to the array.
  8. Using the output, you can verify that C is using row measure form for storing a two-dimensional array.
Points to Remember
  1. You can define a multi-dimensional array in C.
  2. You have to provide multiple subscripts for accessing array elements.
  3. You can access array elements by using a pointer.