Tuesday, March 22, 2011

Program for Addition of the two lists in with Example


#include<stdio.h>
   #include<conio.h>
   void main()
   {
    void read(int *,int);
    void dis(int *,int);
    void add(int *,int *,int * ,int);
    int a[5],b[5],c[5],i;

    clrscr();
    printf("Enter the elements of first list \n");
    read(a,5);       /*read the first list*/
    printf("The elements of first list are \n");
    dis(a,5);  /*Display the first list*/
    printf("Enter the elements of second list \n");
    read(b,5);      /*read the second list*/
    printf("The elements of second list are \n");
    dis(b,5);  /*Display the second list*/
    add(a,b,c,i);
    printf("The resultant list is \n");
    dis(c,5);
    getch();
   }

   void add(int a[],int b[],int c[],int i)
   {
    for(i=0;i<5;i++)
      {
       c[i]=a[i]+b[i];
      }
   }
   void read(int c[],int i)
   {
    int j;
    for(j=0;j<i;j++)
    scanf("%d",&c[j]);
    fflush(stdin);
   }

   void dis(int d[],int i)
   {
    int j;
    for(j=0;j<i;j++)
    printf("%d ",d[j]);
    printf("\n");
   }

Explanation

  1. Repeat step (2) for i=0,1,2, (n1), where n is the maximum number of elements in a list.
  2. c[i] = a[i]+b[i], where a is the first list, b is the second list, and c is the resultant list; a[i] denotes the ith element of list a.

Example

Input
Enter the elements of the first list
1
2
3
4
5
Output
The elements of the first list are
2 3 4 5
Input
Enter the elements of the second list
6
7
8
9
10
Output
The elements of the second list are
6 7 8 9 10
The resultant list is
7 9 11 13 15

No comments:

Post a Comment