Saturday, 17 March 2012

C programs using Arrays



In this post i'm gonna give u some c programs written using array concept....

once again reminding u, if u want any C programs just leave ur comments or just check out for the "Suggested links by RK" below this post..

Let me start....

But guys this time i'll give some explanations first and then i'll go for examples...



Visit WriteHow for more Coll stuff..

Array Definition

Array by definition is a variable that hold multiple elements which has the same data type.

Declaring Arrays

We can declare an array by specifying its data type, name and the fixed number of elements the array holds between square brackets immediately following the array name.
The following illustrates how to declare an array:
1data_type array_name[size];
For example, to declare an array that contains 100 integer numbers you can do as follows:
1int a[100];
You must follow the rules below when you declare an array in C:
  • The data type can be any valid C data types including C structure and union.
  • The name of an array has to follow the rule of C variables.
  • The size of array has to be a positive constant integer.

Initializing Arrays

It is like a variable, an array can be initialized. To initialize an array, you provide initializing values which are enclosed within curly braces in the declaration and placed following an equals sign after the array name.
Here is an example of initializing an array of integers.
1int list[5] = {2,1,3,7,8};

Accessing Array Element

You can access array elements via indexes like array_name[index].
Indexes of array starts from 0 not 1 so the highest elements of an array is array_name[size-1].

Array and Pointer

An array is a pointer to the 0th element of the array. When you dreference the array name you will get the 0th element. This give us the possibility of accessing array's element not only via index but also via pointer.
It is note that the array is treated as constant so you can only modify the values in the array but not array itself.
The program below will help you visualize the memory address each array's element and how to access array's elements using pointer.

 #include <stdio.h>
 void main()
 {
   const int size = 5
     int list[size] = {2,1,3,7,8};
    int* plist = list;
     // print memory address of array elements
     for(int i = 0; i < size;i++)
     {
       printf("list[%d] is in %d\n",i,&list[i]);
}
 /* increase memory address of pointer so it go to the next element of the array */




 plist++;

 }}
Here is the output
list[0] is in 1310568
list[1] is in 1310572
list[2] is in 1310576
list[3] is in 1310580
list[4] is in 1310584
list[0] = 2
list[1] = 1
list[2] = 3
list[3] = 7
list[4] = 8
You can store pointers in an array and in this case we have an array of pointers. The following illustrates an array of pointers.
1int *ap[10];

Multidimensional Arrays

An array with more than one index value is called a multidimensional array. All the arrays you used above are called single-dimensional arrays.
To declare a multidimensional array you can do as follows:
1data_type array_name[][][];
The number of square brackets specifies the dimension of the array. For example to declare two dimensions integer array we can do as follows:
1int matrix[3][3];

Initializing Multidimensional Arrays

You can initialize an array as a single-dimension array. Here is an example of initializing an two dimensions integer array:

1int matrix[3][3] =
2{
3  {11,12,13},
4  {21,22,23},
5  {32,31,33},
6};




Visit WriteHow for more Coll stuff..
EXAMPLES :-

Q. Write a C program to input N real numbers in ascending order into a single dimension array. Conduct a binary search for a given key interger number and report success or failure in the form of a suitable message.

#include<stdio.h>
int main()
{
  int n,i,key,low,high,mid,a[20];
  printf("Enter n:\n");
  scanf("%d",&n);
  printf("Enter elements:\n");
for(i=0;i<n;i++)
   scanf("%d",&a[i]);
printf("enter key:\n");
scanf("%d",&key);
 low=0,high=n-1;

while(low<=high)
{
 mid=(low+high)/2;
 if(key==a[mid])
{
 printf("%d is found\n",key);
}
if(key<a[mid])
 high=mid-1;
else
low=mid+1;
}
printf("%d not found\n",key);
}



Q. Write a C program to input N integer numbers into a single dimension array. Sort them in ascending order using bubble sort technique. Print both the given array and the sorted array with suitable headings.

#include<stdio.h>
int main()
{
  int n,i,j,temp,a[20];
  printf("Enter the number of items:\n");
  scanf("%d",&n);
  printf("Enter items to sort:\n");
  for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
 temp=a[j];
 a[j]=a[j+1];
 a[j+1]=temp;
}
}
}
printf("Items are \n");
for(j=0;j<n;j++)
printf("%d\n",a[j]);
}



Q. write a C program to evaluate the given polynomial f(x)= a4x4+a3x3+a2x2+a1x+a
     for a given value of x and the coefficients using Horner's method by using single     dimention arrays to store coefficients.

#include<stdio.h>
int main()
{
float a[30],x,sum,n,i;
printf("%f",&n);
printf("Enter n+1 vals:\n");
for(i=0;i<=n;i++)

scanf("%f",&a[i]);

printf("ENter x:\n");

scanf("%f",&x);

sum=a[n]*x;

for(i=n-1;i>0;i--) 

sum=(sum+a[i])*x;
sum=sum+a[0]; 

printf("result= %f\n",sum);
}



Q. Write a C program to read two matrices A(M x N) and B(P x Q) and compute the product of A and B and sum of A and B after checking compatibility for multiplication output the input matrices and the resultant matrix with suitable headings and format. 

#include<stdio.h>
int main()
{
    int m1[10][10],i,j,k,m2[10][10],add[10][10],mult[10][10],r1,c1,r2,c2;
    printf("Enter number of rows and columns of first matrix MAX 10\n");
    scanf("%d%d",&r1,&c1);
    printf("Enter number of rows and columns of second matrix MAX 10\n");
    scanf("%d%d",&r2,&c2);
    if(r2==c1)
    {
        printf("Enter rows and columns of First matrix \n");
        printf("Row wise\n");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
                scanf("%d",&m1[i][j]);
        }
        printf("You have entered the first matrix as follows:\n");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
                printf("%d\t",m1[i][j]);
            printf("\n");
        }
        printf("Enter rows and columns of Second matrix \n");
        printf("Again row wise\n");
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
                scanf("%d",&m2[i][j]);
        }
        printf("You have entered the second matrix as follows:\n");
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
                printf("%d\t",m2[i][j]);
            printf("\n");
        }
        if(r1==r2&&c1==c2)
        {
            printf("Now we add both the above matrix \n");
            printf("The result of the addition is as follows;\n");
            for(i=0;i<r1;i++)
            {
                for(j=0;j<c1;j++)
                {
                    add[i][j]=m1[i][j]+m2[i][j];
                    printf("%d\t",add[i][j]);
                }
                printf("\n");
            }
        }
        else
        {
            printf("Addition cannot be done as rows or columns are not equal\n");
        }
        printf("Now we multiply both the above matrix \n");
        printf("The result of the multiplication is as follows:\n");
        /*a11xA11+a12xA21+a13xA31 a11xA12+a12xA22+a13xA32 a11xA13+a12xA23+a13xA33*/
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c2;j++)
            {
                mult[i][j]=0;
                for(k=0;k<r1;k++)
                {
                    mult[i][j]+=m1[i][k]*m2[k][j];
                    /*mult[0][0]=m1[0][0]*m2[0][0]+m1[0][1]*m2[1][0]+m1[0][2]*m2[2][0];*/
                }
                printf("%d\t",mult[i][j]);
            }
            printf("\n");
        }
    }
    else
    {
        printf("Matrix multiplication cannot be done");
    }
}

Share this with ur friends, classmates, etc so that all can take advantage from this... 

Filled Under:

1 comments:

  1. Nice Post :)
    Some Tips I would like to give you while writing/coding C-programmes

    Tips:
    1)Do Indent the programmes Which helps in tracing the program,easy understanding,reduces complexity.

    2) Do write some comments which helps others to understand the program very easy.

    3) Use global declaration when the variable is used in more than once in calling functions.

    4) Do use "Mingw Gcc" as your "C-compiler" and Code::blocks as IDE (Inwindows/Linux/Mac)

    ReplyDelete