Saturday 27 August 2011

Arrays

defn: 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
1. wap to bubble sort.
algorithm:
1. compare the first two elements in the array, a[0], and a[1], if a[1] is smaller than a[0] then interchange(SWAP) their values.
2. compare a[1] and a[2]; interchange them if a[2] is smaller than a[1]. in general if(a[i]>a[i+1])
3. continue this process till the last two elements are compared and interchanged.
4. repeat the above steps n-1 times.


#include<stdio.h>
#include<conio.h>
void main()
{
 int a[10];
 int i,j,temp,n;
 clrscr();
 printf("enter the size of an array\n");
 scanf("%d",&n);
 printf("enter the values %d into array\n",n);
 for(i=0;i<=n-1;i++)
 {
  scanf("%d",&a[i]);
 }
 //sorting logic
 for(i=0;i<=n-1;i++)
 {
  for(j=i+1;j<=n-1;j++)
  {
   if(a[i]>a[j]){
   temp=a[i];
   a[i]=a[j];
   a[j]=temp;
   }
  }
 }
 for(i=0;i<=n-1;i++)
  printf("%d\n",a[i]);
  getch();
 }

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

                                                           //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();
}


-----------------------------------------------------------------------------------------------------------
                                                                       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.
------------------------------------------------------------------------------------------------------------



                                                     Interview Questions on Arrays


                How would you use qsort() function to sort the name stored in an array of pointers to string?   


                Is it better to use a pointer to navigate an array of values,or is it better to use a subscripted array name?             


                What is the difference between null array and an empty array?


                How to remove duplicate elements from an array           


                Can the sizeof operator be used to tell the size of an array passed to a function?              


                Is it better to use a pointer to navigate an array of values,or is it better to use a subscripted array name?             


                WAP to store elements of last col in 1st col and so on.   


                When does the compiler not implicitly generate the address of the first element of an array?    


                Write a program to delete an element from an array?   


                Can the sizeof operator be used to tell the size of an array passed to a function?              


                Code for swapping of two numbers without using temporary variable using C.  


                Write a program to insert an element in a linear array array?      


                how to print 2 dimensional array in descending order?  


                Can the size of an array be declared at runtime?              


                Array is an lvalue or not?             


                Write a program using an array that computes the sum and the average of nth input values from the keyboard and prints the calculated sum and average. 


                what will happen if we try to store more number of elements than specified in array size?          


                Does mentioning the array name gives the base address in all the integers?       


                Can we add the name as "Mixed Arrays" instead of the name given as "Structures" in c?why the name structure is given?  


                 Write a program for n lines 1 2 3 4 5 16 17 18 19 6 15 24 25 30 7 14 23 22 21 8 13 12 11 10 9

c language Arrays



No comments:

Post a Comment