Thursday, July 14, 2011

DIMENSION OF ARRAY

DIMENSION OF ARRAY

Important Note About Array Dimensions

The C language performs no error checking on array bounds. If you define an array with 50 elements and you attempt to access element 50 (the 51st element), or any out of bounds index, the compiler issues no warnings. It is the programmer's task alone to check that all attempts to access or write to arrays are done only at valid array indexes. Writing or reading past the end of arrays is a common programming bug and can be hard to isolate.
What will happen if a program accesses past the end of an array? Suppose a program has the following code.
int val;
int buffer[10];
val = buffer[10];
    /* Bug, remember that the indexes of buffer run from 0 to 9. */

What value will be in val? Whatever happens to be in memory at the location right after the end of the array. This value could be anything. Worse yet, the program may continue to run with the incorrect value and no warnings are issued.
What will happen if a program writes past the end of an array? Suppose a program has the following code.
int buffer[10];
buffer[593] = 99;
The value of 99 will be written at the memory location, buffer + 593. "buffer" is a pointer to the beginning of the array. buffer + 593 is pointer arithmetic for the address equal to the starting address of the array plus the size of 593 integers. The overwriting of the value at this memory location will change the value of whatever variable is stored there. Some other variable may have its value changed unintentionally. If the program writes unintentionally to memory locations that not valid, the program may crash.

Multidimensional Arrays

The C language also allows multidimensional arrays. They are defined as follows.
int matrix[3][3];

0
1
2
0
x1
x2
x3
1
x4
x5
x6
2
x7
x8
x9
To represent x5 it should be written as follows
matrix[1][1]
Similarly
x1 = matrix[0][0];
x2 = matrix[0][1];
x3 = matrix[0][2];
x4 = matrix[1][0];
and so on
Example:
int x[3][3];
x[0][0] = 5;          x[0][1] = 3;      x[0][2] = 9;
x[1][0] = 6;          x[1][1] = 22;     x[1][2] = 33;
x[2][0] = 32;         x[2][1] = 45;     x[2][2] = 7;

0
1
2
0
5
3
9
1
6
22
33
2
32
45
7
A common way to access the elements of multidimensional arrays is with nested for loops.
#define row 3
#define col 3

int i;
int j;
int x[row][col];

for (i = 0; i < row; i++)
{
    for (j = 0; j < col; j++)
    {
        values[i][j] = whatever;
     }
}
Example: Program to find the sum of matrix of 3x3
#include<stdio.h>
#include<conio.h>
main()
{
    int A[3][3], B[3][3], c[3][3];
    int i, j;
 
    /*taking input values in matrix A and matrix B */
    for(i=0; i<3; i++)
          for(j=1; j<3; j++)
                scanf("%d %d ", &A[i][j], &B[i][j]);
 
/*Adding matrix */
    for(i=0; i<3; i++)
          for(j=0; j<3; j++)
                C[i][j] = A[i][j] + B[i][j];
 
/*Displaying the result in screen */
    for(i=0; i<3; i++)
          for(j=0; j<3; j++)
                printf("%d", C[i][j]);
 
}
Example : Program to find the multiplication of matrix of 3x3
#include<stdio.h>
main()
{
    int A[3][3], B[3][3], c[3][3];
    int i, j, k, sum;
 
    /*taking input values in matrix A and matrix B */
    for(i=0; i<3; i++)
          for(j=0; j<3; j++)
                scanf("%d %d ", &A[i][j], &B[i][j]);
 
    /*multiplying matrix */
    for(i=0; i<3; i++)
          for(j=0; j<3; j++)
          {
                sum = 0;
                for(k=0; k<3; k++)
                      sum = sum + (A[i][k]*B[k][j]);
                C[i][j] = sum;
          }
 /*Displaying the result in screen */
    for(i=0; i<3; i++)
          for(j=0; j<3; j++)
                printf("%d", C[i][j]);
}
Example 3: Program to transpose matrix of 3x3
#include<stdio.h>
main()
{
    int A[3][3], B[3][3];
    int i, j;
 
    /*taking input values in matrix A and matrix B */
    for(i=0; i<3; i++)
          for(j=0; j<3; j++)
                scanf("%d %d ", &A[i][j]);
 
    /*transposing matrix */
    for(i=0; i<3; i++)
          for(j=0; j<3; j++)
                B[j][i] = A[i][j];
         
   /*Displaying the result in screen */
    for(i=0; i<3; i++)
          for(j=0; j<3; j++)
                printf("%d", B[i][j]);
}
Example 4: Write a program to input name and age of person. Reorder record in alphabetical order of name.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
    char name[10][25], temp[25];
    int i, j, age[10], temp1;
 
    /*taking input for name and age*/
    for(i=0; i<10; i++)
          scanf("%s %d ",name[i], &age[i]);
 
    /*reordering record in order of name */
    for(i=0; i<10; i++)
          for(j=i+1; j<10; j++)
          {
            if(strcmp(name[i], name[j]) > 0)
                {
      //swapping name
      strcpy(temp, name[i]);
      strcpy(name[i], name[j]);
      strcpy(name[j], temp)
   
//swapping age
      temp1 = age[i];
      age[i] = age[j];
      age[j] = temp1;
                }    
          }
/*Displaying the reordered name and age*/
    for(i=0; i<10; i++)
          printf("%s %d", name[i], age[i]);
     getch();
}

0 comments:

Post a Comment