Sunday, 14 January 2018

Array multiply using function

Q :  Write a function which receives two integer arrays and creates a third array by multiplying  corresponding elements (elements with same index) and returns it to main program.


Solution : 

/*==============================
     Girfa Student Help
     Program : Multiply two array using function
     More Program :http://girfahelp.blogspot.in/p/function-programming.html
================================*/
#include<stdio.h>
#include<conio.h>
#define MAX 5
void mul(int *,int *,int *);
void print(int *);
void main()
{

     int ar1[MAX],ar2[MAX],ar3[MAX],i;
     clrscr();
     printf("\nInput f
or first array\n");
     for(i=0;i<MAX;i++)
     {
          printf("Enter %d number>> ",i+1);
          scanf("%d",&ar1[i]);
     }
     printf("\nInput for second array\n");
     for(i=0;i<MAX;i++)
     {
          printf("Enter %d number>> ",i+1);
          scanf("%d",&ar2[i]);
     }
     /* Multiply function */
     mul(ar1,ar2,ar3);
     clrscr();
     printf("\n\nFirst Array");
     print(ar1);
     printf("\n\nSecond Array");
     print(ar2);
     printf("\n\nMultiply Result");
     print(ar3);
     getch();
}
void mul(int *a,int *b,int *c)
{
     int i;
     for(i=0;i<MAX;i++)
     {
          *c=(*a) * (*b);
          a++;
          b++;
          c++;
     }
}
void print(int *p)
{
     int i;
     for(i=0;i<MAX;i++,p++)
          printf("\t%d",*p);
}

No comments:

Post a Comment