Sunday 4 March 2012

Arrays

Arrays- concepts, 
declaration, 
definition, 
accessing elements, 
storing elements,
Two Dimensional Array (2 D)
Multi-dimensional arrays, 
Arrays and functions, 
Applications of arrays. 
Pointers- concepts, 
Initialization of pointer variables, 
pointers and function arguments, 
address arithmetic, 
Character pointers and functions, 
pointers to pointers, 
pointers and multidimensional arrays, 
dynamic memory managements functions, 
command line arguments, 
c program examples.


Definition: Array is a collection of elements(variables) of same type is known as arrays.
syntax: datatype variable[size];
ex:  int a[10];
      float f[20];
     char [5];
     double[12];  and so..on



It is also known as 'subscripted var"
An array can be:
 Array of integers,
 Array of floats,
 Array of characters( strings)
 Array of Numbers
 Array of Structures
 Array of Pointers.

An array must be declared before it is used.

14 sita 'f' 15000.00//not possible

Types of arrays:
1. Single dimensional (1D)
2. Multi Dimensional (2 D)

Accessing Values in Array:

1. Using subscripts
2. Using addresses.


1. Single dimensional (1D) Array:

Definition: A list (group) of elements can be
given one variable name using only one
subscript.
Syntax:

datatype vari[size];//array declaration

Examples:
int a[5];
int a[‘A’]; //ascii value of A is 65 so int a[65];


#include<stdio.h>
#include<conio.h>
void main()
{
 int i;
 int a[]={1,2,3,4,5};
 clrscr();
 printf("array is\n");
 for(i=0;i<5;i++)
 printf("%d\n",a[i]);
 getch();
}


//Reading an array

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[5],i;
 clrscr();
 printf("enter the values into array\n");
 for(i=0;i<5;i++)
  scanf("%d",&a[i]);
 printf("array is\n");
 for(i=0;i<5;i++)
 printf("%d\n",a[i]);
 getch();
}
Initialization of One Dimensional Array:



Syntax:
Datatype var[size]={list of values};
Ex1:
int a[]={10,20.15,18};
array size is no.of values on right hand side i.e 4
the values in the list are separated by commas.
Ex 2:
int a[7]={10,20,30,40};

Ex3:
int a[3]={10,20,30,40,50};//Error



Invalid Subscripts

m[-1];
m[2.4]
m[“Hyd”];
a(2);

Ex2:

float b[7];
b is a float array of size 7
1 float=4 bytes occupies in the memory
7*4=28 bytes of memory is allocated to array b
Subscripts are 0 to 4
Address are b,b+1,b+2,…b+6.

Ex3:
int n=5;
int a[n];//error bcoz array size must be value but not
 variable.
So value of n is not defined at compilation time.

Ex4:
#define size 5
int a[size];//ok

Ex5:
int a[]; //error




----------------------------------------------------------------------------------------------------------

                                                           //ARRAY OF POINTERS
#include <stdio.h>
#include <conio.h>
void main()
{
  int *a[3];
  int x = 10, y = 20, z = 30;
  int i;
  a[0] = &x;
  a[1] = &y;
  a[2] = &z;
  clrscr();
  for(i=0; i< 3; i++)
  {
   printf("%d\t",*(a[i]));//values
   printf("%u\n",a[i]);//address of pointer
  }
  getch();
}

Note:
output:
10   65518
20   65516
30   65514

-----------------------------------------------------------------------------------------------------------

                                                                2 d arrays
Declaration of an Two Dimensional Array:
defin: collection of 1d array is known as 2d array

int a[3][2];//- matrix 3X2 (3 rows, 2 columns), elements: integers
char c[2][4];// - array of characters (2 rows & 4 columns)
float x[3][4]; // MAXROW X MAXCOL matrix

Initialization of 2d array:


int x[3][4] = {  {1, 2, 3},
                 {4, 5, 6},
                 {7, 8, 9} };
(or)

int x[3][4];

x[0][0]= 1;    x[0][1]= 2;   x[0][2]= 3;   x[0][3]= 0;
x[1][0]= 4;    x[1][1]= 5;   x[1][2]= 6;   x[1][3]= 0;
x[2][0]= 7;    x[2][1]= 8;   x[2][2]= 9;   x[2][3]= 0;

example3:

2d Exmple of declaration with initialization:
int array[3][3] = {1, 2, 3, 4, 5};
/*that means it stores the followin format*/
array[0][0] = 1;
array[0][1] = 2; 
array[0][2] = 3; 
array[1][0] = 4; 
array[1][1] = 5;
array[1][2] = 0; // even though nowhere stated
array[2][0] = 0;
array[2][1] = 0;
array[2][2] = 0;

/*1) wap to read an 2d array and display */

#include<conio.h>
#include<stdio.h>
void main()
{
 int a[2][3];
 int i,j;
 printf("enter the elements in 2d array\n");
 for(i=0;i<=2-1;i++)
 {
  for(j=0;j<=3-1;j++)
  {
   scanf("%d",&a[i][j]);
  }
 }
 printf("2d array is\n");
 for(i=0;i<=2-1;i++)
 {
  for(j=0;j<=3-1;j++)
  {
   printf("%d\t",a[i][j]);
  }
  printf("\n");
 }
 getch();
}
/* transpose of a matrix */


#include<stdio.h>
#include<conio.h>
void main()
{
 int a[5][5];
 int i,j,m,n,temp;
 m=3;n=3;
 clrscr();

 printf("enter the size of the first matrix (m,n)\n");
 scanf("%d%d",&m,&n);

 printf("enter the elements into 1array\n");
 for(i=0;i<=m-1;i++)
  {
   for(j=0;j<=n-1;j++)
   {
    scanf("%d",&a[i][j]);
   }
  }

 //transpose logic
 for(i=0;i<=m-1;i++)
 {
  for(j=0;j<=n-1;j++)
  {
   if(i<j)//swapping values above diagnols with those below diagnols
   {
    temp=a[i][j];
    a[i][j]=a[j][i];
    a[j][i]=temp;
   } //end of if
  }//end of inner loop
 } //end of outer
 //displaying the tranpose matrix
 for(i=0;i<=m-1;i++)
 {
  for(j=0;j<=n-1;j++)
  {
   printf("%d\t",a[i][j]);
  }//inner loop
  printf("\n");
 }//outer loop
 getch();
}
/*
i/p:
o/p:
*/

------------------------------------------------------------------------------------------------------------
/*product of a matrix or Matrix multiplication */

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[3][3];
 int b[3][2];
 int c[5][5];
 int i,j,k,m,n,p,q;
 clrscr();
 printf("enter the size of the first matrix (m,n)\n");
 scanf("%d%d",&m,&n);
 printf("enter the size of the 2nd matrix (p,q)\n");
 scanf("%d%d",&p,&q);
 printf("enter the elements into 1array\n");
 for(i=0;i<=m-1;i++)
  {
   for(j=0;j<=n-1;j++)
   {
    scanf("%d",&a[i][j]);
   }
  }
  printf("enter the elements into 2array\n");
 for(i=0;i<=p-1;i++)
  {
   for(j=0;j<=q-1;j++)
   {
    scanf("%d",&b[i][j]);
   }
  }
 if(n==p)//1st matrix colm must be equal to 2nd matrix row size
 {
  //processing the product of two matrix
  for(i=0;i<=m-1;i++)
  {
   printf("\n");
   for(j=0;j<=q-1;j++)
   {
    c[i][j]=0;//initializing matrix c with zeros
    for(k=0;k<=n-1;k++)
    {
     c[i][j]=c[i][j]+a[i][k]*b[k][j];
    }
    printf("%d\t",c[i][j]);
   }
   printf("\t");
  }//outer loop
 }//if
else
 printf("matrix can not be multiplied");
 getch();
}

What are merits and demerits of array in c?
Merits:
(a) We can easily access each element of array.
(b) Not necessity to declare too many variables.
(c) Array elements are stored in continuous memory location.
Demerit:
(a) Wastage of memory space. We cannot change size of array at the run time.
(b) It can store only similar type of data.

-----------------------------------------------------------------------------------------------------------
                                                                       Review Questions

Short answers type
1) Define an array and give example of array declaration.
2) Show how to initialize 1d array.
3) Show how to initialize 2d array.
4) How to initialize an array.
5) Explain how to declare an array.
6) What is the difference between a simple variable and array.
7) Explain the function of strcmp() & stricmp().
Essay type questions
1) Explain how to access an array elements with the help of a program.
2) Write a C program to find sum of two given numbers.
3) Write a C program to add two matrices.
4) Explain how to pass 1d array as argument to a function
5) Explain how to pass 2d array as argument to a function.
6) Write a C program to print transpose of  a given matrices.

7) Write a C program to check whether a given number is palindrome or not.

8) Write a program In C to multiply matrices A and B, and to store the result in matrix C.
9) Write a C program to sort 10 numbers using bubble sort.
10) Write a ‘c’ program that calculates & points the average of the elements using arrays.ss

11) Write a C program to read a string and its each character in a new line.


------------------------------------------------------------------------------------------------------------



pointers
Interview questions on 1 D Arrays
Download the Arrays PPT

No comments:

Post a Comment