ARRAY

 Chapter 2.

ARRAY








some 1-dimension array related problem are mentioned below.

Q1.WAP in c to read n number of values in array and display it in reverse order.

#include<stdio.h>
#include<conio.h>
int main()
{
   int a[100],i,j,n;
   printf("enter the size of array:");
   scanf("%d",&n);
   printf("enter the elements of array:");
   for(i=0;i<n;i++)
   {
      scanf("%d",&a[i]);
   }
   printf("reverse elements of array is\n");
   for(j=n-1;j>=0;j--)
  {
   printf("%d\n",a[j]);
  }
     return 0;
}


Q2.WAP to find the sum of all elements of array.

#include<stdio.h>
#include<conio.h>
int main()
{
   int a[100],i,sum=0,n;
   printf("enter the size of array:");
   scanf("%d",&n);
   printf("enter the elements of array:");
 for(i=0;i<n;i++)
   {
      scanf("%d",&a[i]);
   }
   for(i=0;i<n;i++)
   {
      sum=sum+a[i];
   }
    printf("sum of an array is %d",sum);
     return 0;
}


Q3.WAP to count a total number of duplicate elements in a array.

#include<stdio.h>
#include<conio.h>
int main()
{
   int a[100],i,j,n,c=0;
   printf("enter the size of array:");
   scanf("%d",&n);
   printf("enter the elements of array:");
 for(i=0;i<n;i++)
   {
      scanf("%d",&a[i]);
   }
   for(i=0;i<n;i++)
   {
     for(j=i+1;j<n;j++)
     {
      if(a[i]==a[j])
      {
         c=c+1;
         break;
      }
     }
   }
     printf("TOtal duplicated elements in an array is %d ",c);
         return 0;
}


Q4. WAP in c to find the maximum and minimum elements in an array.

#include<stdio.h>
#include<conio.h>
int main()
{
   int a[100],i,j,n,c=0;
   printf("enter the size of array:");
   scanf("%d",&n);
   printf("enter the elements of array:");
 for(i=0;i<n;i++)
   {
      scanf("%d",&a[i]);
   }
   for(i=0;i<n;i++)
   {
     for(j=i+1;j<n;j++)
     { int temp=0;
      if(a[i]<a[j])
      {
         temp=a[i];
         a[i]=a[j];
         a[j]=temp;
      }
     
     }
   }
   printf("Maximum elements of an array is %d\n",a[0]);
      printf("Minimum elements of an arrayy is %d\n",a[n-1]);
     
         return 0;
}


Q5.WAP to find the unique elements of an array.

#include<stdio.h>
#include<conio.h>
int main()
{
   int a[100],i,j,n,c=0;
   printf("enter the size of array:");
   scanf("%d",&n);
   printf("enter the elements of array:");
 for(i=0;i<n;i++)
   {
      scanf("%d",&a[i]);
   }
   printf("unique elements are\n");
     
   for(i=0;i<n;i++)
   {c=0;
     for(j=0;j<n;j++)
     { if(i!=j)
       {
         if(a[i]==a[j])
         {
         c=c+1;
         }
       }  
     }
   
   if(c==0)
   {
      printf("%d ",a[i]);
   }
   }
         return 0;
}


Q6.WAP to find the second smallest and second largest elements in an array.

#include<stdio.h>
#include<conio.h>
int main()
{
   int a[100],i,j,n,c=0;
   printf("enter the size of array:");
   scanf("%d",&n);
   printf("enter the elements of array:");
 for(i=0;i<n;i++)
   {
      scanf("%d",&a[i]);
   }
   for(i=0;i<n;i++)
   {
     for(j=i+1;j<n;j++)
     { int temp=0;
      if(a[i]<a[j])
      {
         temp=a[i];
         a[i]=a[j];
         a[j]=temp;
      }
     
     }
   }
   printf("second smallest elements of an array is %d\n",a[n-2]);
   printf("second largest elements of array is %d",a[1]);
     
         return 0;
}


Q7.WAP in c to find even and odd elements in seperate way.

#include<stdio.h>
#include<conio.h>
int main()
{
   int a[100],o[100],e[100],i,n,Ec=0,Oc=0;
   printf("enter the size of array:");
   scanf("%d",&n);
   printf("enter the elements of array:");
 for(i=0;i<n;i++)
   {
      scanf("%d",&a[i]);
   }
   for(i=0;i<n;i++)
   {
  if(a[i]%2==0)
{
    e[Ec]=a[i];
    Ec=Ec+1;
}
 else
 {
    o[Oc]=a[i];
    Oc=Oc+1;
 }
   }
 printf("even elements are\n");
 for(i=0;i<Ec;i++)
 {
    printf("%d ",e[i]);
 }
 printf("\n");
 printf("Odd elements are\n");
 for(i=0;i<Oc;i++)
 {
    printf("%d ",o[i]);
 }

         return 0;
}


Q8.WAP in c to find the two elements whose sum is 20.

#include<stdio.h>
#include<conio.h>
int main()
{
  int a[100],i,j,n,p;
  printf("enter the size of array:");
  scanf("%d",&n);
  printf("enter the elements of array");
  for(i=0;i<n;i++)
  {
   scanf("%d",&a[i]);
  }
  for(i=0;i<n;i++)
 {
   for(j=i+1;j<n;j++)
   {
      if(a[i]+a[j]==20)
      {
       printf("%d and %d is equal to 20\n",a[i],a[j]);
      }
   }
 }

         return 0;
}


#2 dimensional array.

Q9.WAP in c to find the sum of right diagonal of a matrix.

#include<stdio.h>
#include<conio.h>
int main()
{
  int a[100][100],i,j,m,sum=0;
  printf("enter the size of square matrix:");
  scanf("%d",&m);
  printf("enter the elements of array");
  for(i=0;i<m;i++)
  {
    for(j=0;j<m;j++)
    {
   scanf("%d",&a[i][j]);
    }
  }
  printf("Given matrix is \n");
  for(i=0;i<m;i++)
  {
    for(j=0;j<m;j++)
    {
   printf("%d\t",a[i][j]);
    }
    printf("\n");
  }
  for(i=0;i<m;i++)
  {
    for(j=0;j<m;j++)
    {
     if(i+j==m-1)
     {
      sum=sum+a[i][j];
     }
    }
  }
  printf("sum of right diagonal of matrix is %d",sum);
         return 0;
}


Q10.WAP in c to find the sum of left diagonal of a matrix.

#include<stdio.h>
#include<conio.h>
int main()
{
  int a[100][100],i,j,m,sum=0;
  printf("enter the size of square matrix:");
  scanf("%d",&m);
  printf("enter the elements of array");
  for(i=0;i<m;i++)
  {
    for(j=0;j<m;j++)
    {
   scanf("%d",&a[i][j]);
    }
  }
  printf("Given matrix is \n");
  for(i=0;i<m;i++)
  {
    for(j=0;j<m;j++)
    {
   printf("%d\t",a[i][j]);
    }
    printf("\n");
  }
  for(i=0;i<m;i++)
  {
    for(j=0;j<m;j++)
    {
     if(i==j)
     {
      sum=sum+a[i][j];
     }
    }
  }
  printf("sum of left diagonal of matrix is %d",sum);
         return 0;
}


Q11.WAP in c to display the lower triangular of a given matrix.

#include<stdio.h>
#include<conio.h>
int main()
{
  int a[100][100],i,j,m,sum=0;
  printf("enter the size of square matrix:");
  scanf("%d",&m);
  printf("enter the elements of array");
  for(i=0;i<m;i++)
  {
    for(j=0;j<m;j++)
    {
   scanf("%d",&a[i][j]);
    }
  }
  printf("Given matrix is \n");
  for(i=0;i<m;i++)
  {
    for(j=0;j<m;j++)
    {
   printf("%d\t",a[i][j]);
    }
    printf("\n");
  }
  printf("lower triangular matrix is \n");

  for(i=0;i<m;i++)
  {
    for(j=0;j<m;j++)
    {
     if(i<j)
     {
       printf("%d\t",0);
     }
     else
     {
      printf("%d\t",a[i][j]);
     }
    }
    printf("\n");

  }
         return 0;
}


Q12.WAP in c to display the upper triangular of a given matrix.

#include<stdio.h>
#include<conio.h>
int main()
{
  int a[100][100],i,j,m,sum=0;
  printf("enter the size of square matrix:");
  scanf("%d",&m);
  printf("enter the elements of array");
  for(i=0;i<m;i++)
  {
    for(j=0;j<m;j++)
    {
   scanf("%d",&a[i][j]);
    }
  }
  printf("Given matrix is \n");
  for(i=0;i<m;i++)
  {
    for(j=0;j<m;j++)
    {
   printf("%d\t",a[i][j]);
    }
    printf("\n");
  }
  printf("upper triangular matrix is \n");

  for(i=0;i<m;i++)
  {
    for(j=0;j<m;j++)
    {
     if(i>j)
     {
       printf("%d\t",0);
     }
     else
     {
      printf("%d\t",a[i][j]);
     }
    }
    printf("\n");

  }
         return 0;
}


Q13.WAP in c to accept a matrix and determine whether it is a spare matrix or not.

#include<stdio.h>
#include<conio.h>
int main()
{
  int a[100][100],i,j,m,n,sum=0,c=0;
  printf("enter the size of  matrix:");
  scanf("%d%d",&m,&n);
  printf("enter the elements of array");
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
   scanf("%d",&a[i][j]);
    }
  }
  printf("Given matrix is \n");
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
   printf("%d\t",a[i][j]);
    }
    printf("\n");
  }
 

  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
     if(a[i][j]==0)
     {
      c=c+1;
     }

  }
       
  }
  if(c>(m*n)/2)
{
  printf("Given matrix is spare matrix");

}
else
{
  printf("not a spare matrix");
}
return 0;
}


Q14.WAP to find given matrix is identity matrix or not.

#include<stdio.h>
#include<conio.h>
int main()
{
  int a[100][100],i,j,m,f=0;
  printf("enter the size of square matrix:");
  scanf("%d",&m);
  printf("enter the elements of array");
  for(i=0;i<m;i++)
  {
    for(j=0;j<m;j++)
    {
   scanf("%d",&a[i][j]);
    }
  }
 

  for(i=0;i<m;i++)
  {
    for(j=0;j<m;j++)
    {
     if(i==j && a[i][j]!=1)
     {
       f=1;
       break;
       }
       else if(i!=j && a[i][j]!=0)
      {
        f=1;
        break;
      }
     }

  }
       

  if(f==0)
{
  printf("Given matrix is identity matrix");

}
else
{
  printf("not a identity matrix");
}
return 0;
}

Q15.WAP in c to transpose the given matricx.

#include<stdio.h>
#include<conio.h>
int main()
{
  int a[100][100],i,j,m,n;
  printf("enter the size of  matrix:\n");
  scanf("%d%d",&m,&n);
  printf("enter the elements of array\n");
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
   scanf("%d",&a[i][j]);
    }
  }
  printf("given matrix is \n");
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
   printf("%d\t",a[i][j]);
    }
    printf("\n");
  }
  printf("transpose of matrix is \n");
 
   for(i=0;i<n;i++)
  {
    for(j=0;j<m;j++)
    {
   printf("%d\t",a[j][i]);
    }
    printf("\n");
  }

 
return 0;
}


#Some question related 2-dimensional array.

Q1.WAP in c to check whether a given matrix is equal or not.

#include<stdio.h>
#include<conio.h>
int main()
{
  int a[100][100],b[100][100],i,j,m,n,c=1;
  printf("enter the size of square matrix:");
  scanf("%d%d",&m,&n);
  printf("enter the elements of array");
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
   scanf("%d",&a[i][j]);
    }
  }
  printf("enter the other elements of array:");
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
   scanf("%d",&b[i][j]);
    }
  }
 

  for(i=0,j=0;i<m,j<m;i++,j++)
  {
    for(i=0,j=0;i<n,j<n;i++,j++)
    {
     if(a[i][j]==b[i][j])
     c=0;
   
     }

  }
       

  if(c==0)
{
  printf("Given matrix is equal");

}
else
{
  printf("not a equal");
}
return 0;
}


Q2.WAP to print the sum of two matrix using array.

#include<stdio.h>
#include<conio.h>
int main()
{
  int a[100][100],b[100][100],c[100][100],i,j,m,n;
  printf("enter the size of square matrix:\n");
  scanf("%d%d",&m,&n);
  printf("enter the elements of array");
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
   scanf("%d",&a[i][j]);
    }
  }
  printf("enter the other elements of array:\n");
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
   scanf("%d",&b[i][j]);
    }
  }
 
  printf("Sum of two matrix is\n");

  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
     c[i][j]=a[i][j]+b[i][j];
     printf("%d\t",c[i][j]);
    }
    printf("\n");
  }
       

 
return 0;
}








































































Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.