0 like 0 dislike

Finish the function inner_product below. The function should return a[0] * b[0] + a[1] * b[1] + ... + a[n-1] * b[n-1]:

``````#include <stdio.h>
double inner_product(double a[],double b[],int n)
{
/*INSERT YOUR CODE HERE*/

/*END OF YOUR CODE*/
//The function should return a[0] * b[0] + a[1] * b[1] + ... + a[n-1] * b[n-1].
}

int main(void)
{
double arrayA[100], arrayB[100];
int c, n;

scanf("%d", &n); //Enter number of elements in array
for ( c = 0 ; c < n ; c++ )     //Enter array A
scanf("%lf", &arrayA[c]);
for ( c = 0 ; c < n ; c++ )     //Enter array B
scanf("%lf", &arrayB[c]);

printf("%g",inner_product(arrayA,arrayB,n));

return 0;
}``````

Example input:

The first input is the number of elements in arrays (3); The array values of A and B follow;

``````3
4 7 8
1 2 3``````

Example output

``42``

Remember: You may correct the cases, but your code always be revised!

[Exercise] Coding (C) - asked
edited | 640 views

0 like 0 dislike
`Hidden content!#include *   *  * *** ** * double  *    *  ** **  * *   b[],int n){  ** * *** *  *  *  ** *  YOUR CODE HERE*/*   * **  ** *  *   ** *  * ** i,sum=0; * * * ******   **    **  *   ** ******   *  **       *  *     ***     **** ** * **    ***   * *    *      *  ** * *    *  * **   * ** * **** ***  *   * ***  *    **  **  *   ** *    ** ***   *   sum;**  **   *  *** *  ** *    OF YOUR CODE*/ ***   * *  * * ** *      function should return a[0] * b[0] + a[1] * b[1] + ... + a[n-1] * b[n-1].}int ** *  **{ ** *  *  *  * * ****** *  ***   ***  * * * * *  ** *    ** c, n;***  *       * ********  ***    * * * *   * *** *   //Enter number of   *   in array* ** * * ** * *  ***  ( c = 0 ; c *   n ; c++ ) *  ********* ***  *   **   *    array A * * ** ***** *    *  **  *** *  * ** ** * * ***   ** *     *  *   ** *  *****  * ** ** *   *    ( c = 0 ; c * ** * n ; c++ )  * *  **  *    **  ***** **  * * * array B* *  *   * ** *   ** **  ****  *   ** ******* ***   **  ***    **       * *   ****** **** ****** ** *** ** * ** **    * **     * ** **  *        *    *****   **   **  ** 0;}`
answered by (-196 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
`Hidden content!#include    *   *     **double     ****    ***    *** b[],int n){ *  *  **  **   * *** answer=0;   *  **** *  **  i;      **** ** ** * *  * **** ***  **     *  *       **    * ******        *   ***** *  **** **  *** *    *** * ** ** **  answer;}int  ** *  {    * ** ** *********   * *  **   **   * *   **    *   *    * *   ****  c, n;***   ** * * * **** *      *  *** *   ****   ** //Enter number of elements in array* *  ********  *   **  ( c = 0 ; c < n ; c++ )   ** *  *** * * ** * *** *  *  * array A*   **  * **  **  * ****   *  *   **     ****  *    * *   **  ****  *  ****  **  *  ( c = 0 ; c < n ; c++ ) *  *** ****   ***   *  * array B  ********** ** ****    *    *******    **    * * *    ****  * **  * **   * * * **  * *   * **  ***   ** ** * * ***  ** * *    *  *  **   * ***  **  ** *** 0;}`
answered by (-249 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
`Hidden content!#include   *** ***double **     *  * **** a[],double b[],int n){  *  * **  **   **   sum;   *  *  *    * *  **** t; ** *** ** *** * ***   **       * *   ** *  ******  **  ** *  ***  **   * *** *** **  ***** * **   ****  *  ** * * *  sum;}int main(void){   * *  * *****  *****  * *    *     * * * * ****  * *  **   * c, n;***    ** *   ***** **  * ***     ***** * ** ** *** //Enter number of elements in array****     * ** *  ( c = 0 ; c < n ; c++ )   *    *  *  **      **  **  *  array A*** * ******    ***** *  * *  *****   * *  **  **   ********  **  *  ***   *** ** * *** ***  *** *  *  ***  ** ( c = 0 ; c < n ; c++ ) ** *****    *  ** ****   **   * ** array B** *******  *****  *  ***  ****  * *** *     * ** ** ***  * *     *****   **** **   *  * * **  *** *  ***  *  * **   ****  * *        ****  ** **       ***  * * 0;}`
answered by (-16 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
`Hidden content!#include  *  *****  *  ** * double * *  ** * *   a[],double b[],int n){** *  ** *   ** ** **  ****  * i,sum=0;**  *   **  * ******** *** *   * **  *** *  *   ** *  ****   **  * **  *   ** *  **   ** ** *  * * *** *   *   *      * **   **    **    **   *  *****   += a[i]*b[i];****     * * **  *   ** * ** ** ** *  * *  sum;}int main(void){       **         * *       *  ***  ** ***  ** * ** * * ***  c, n;* * *** **** *    ***** *   **      *      ** &n); //Enter number of elements in array * * *  *****   **     ( c = 0 ; c < n ; c++ )  ***  **  **  *** *    * **     array A    *** * *    **  * * *     * **  ** * *  ********  *** * * **   * *      *      * * ***    *** ( c = 0 ; c < n ; c++ )   *****   ****** *  **** * * ** array B*  **** ***  * * **    ** **   ** *   * *   *** *  * ** * * *** * ** * *  *   ** *  **  *    *** *** **      *** *  *** * ** * ***** *** **  *  **    *   *  *** 0;}`
answered by (-254 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
`Hidden content!  **    * *****  * ** *  *  ** *   * **   ** *    * *** n){* *     ** ** * * *    ***  YOUR CODE  *   **  * *  * **    OF YOUR ***  *  ** *** * * ****      *** should return a[0] * b[0] + a[1] * b[1] + ... + a[n-1] *   * **}int   *   {**   **    ***** * * **   * ****   ** ***   *** *          * **** c, n; ****  *****    *  ****** *  **    * ** *     * *  number of *   in array*** ****   ***  * ** **  ( c = 0 ; c ** * n ; c++ )  **** *** ** ** ***  **** array A**   ****** * ***  *   ** *** * ***  *  **  **   **** *****    *    * *** *    *    *      *    ***   ( c = 0 ; c *   *  n ; c++ )   *  *  * *     *   * * ** array B**  ***   * *         *  *  * *******    * *  * * ** * * *****  *** *  * *  * *    *  * *   ** *  **   * **  *    ***   **  *   *     ** ***   **   **** **   ******  * 0;}`
answered by (-254 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 like 0 dislike
`Hidden content!  * * **** * *    *  ** * **  **  **     * ***  ** * n){ **  **    *     ***  * ***  YOUR CODE   ****  ** *     **  *  OF YOUR **    * * **** *** **    * **  *  should return a[0] * b[0] + a[1] * b[1] + ... + a[n-1] * *****}int  * ***  {**** *    **  **** **  **   **   * ***  * ** ** **   **   ***   **** *** c, n; * * * * **  ** ***  * * **   **   ** **      *** number of  * *  in array **   **  ***     **  * * ( c = 0 ; c * **** n ; c++ ) *     *   **     * * ***    array A***  **** * ** ** *** ** *  * *  ***** *     *  * *    *  ** *      * * **  *** ***  ***  *    * *****   ( c = 0 ; c * * n ; c++ ) *  * *****  **** **      *   * array B **    * **    * ***  * **    *  ***** ** * *** * ** *  **  * *  * *   * *    * * *   **   ***       *** *   ** *  *** * * **   **  *    *  * ****  **   **  * ** *** ******   0;}`
answered by (-254 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 like 0 dislike
`Hidden content!   *  * **** ** *   **    * *  ***   * **** *   * ** *   * n){** *** i = 0;    *    m = 0;     *  ** k = 0;  * *  **  * = 0; i  *** ** n; * * *  *** **  **  ** ** =  ** *   * * * * *    **** += m;***  * ***    *  *   k;}** *  *  {*   *** ** ** ****   **        *  *  *    c, n;   *   **    *  ***  ***** * ***    *  *   ** of ** *  in   * *   ( c = 0 ; c  * n ;     ***    ** A*   **  *  *  *  ** **  * * **  *   *  * ** ***   **  ** *** ( c = 0 ; c ** n ;  *  ****  ** B **** **  *   **   **   *  ****** * ** ** **  **   ** *     * ***** * ** **  **      * *  *** * ***  ** * 0;}`
answered by (-127 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
`Hidden content!#include *** * ** *    *** *double   ** * ** * ** a[],double b[],int n){* **  *  *    *** ** * **   *  i,sum = 0;* *    **** * *** ******  ** *** *  = 0 ; i < n ; i++)*   ****  ** **    **   ** **   *  ***** * *  * * * **** * *  ****   **    * * *    * +=        * *  *** * ** *  ** ** * ** * * *  *    *    *  **  ***   *  * *** sum;}int  ** {*** *   *     * *  ***  * ***    *  ***   * * * *   * *** c, n;  *  **  ***  *  * ****  ** * ****  ** **  * **  *  //Enter number of elements in array  ****  * ** *     *** ( c = 0 ; c < n ; c++ ) ***  ****** *  *  **   * *   *  array A*  *   * *  *    * * ** *  ******  ** *** **  ** ***  **   *** * *  *  *   ******  ******  * *** ( c = 0 ; c < n ; c++ ) *  **** **   * ** * **   ****     * array B**          **  **** * *  ** * *  **  ****** ****  ** * *  **    **   *** *  **  *    * ********        ***   ** *      * *** * **   **   ** ** *****  ***  *   ** 0;}`
answered by (-255 points)
edited
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
`Hidden content!#include   *   **** **  *double inner_product(double a[],double b[],int n){  *  * ** *** * * * ** YOUR CODE HERE*/* *   *  ** *   ** ***** * * **   *   *  **** *** c,i,j;*  **  **   *** * ***  * * * *  * ** * *****  ** ** k[100];***  **  **      * *   *  *  ** ** ****** ** ****** * *    *    * ***  *    *** **  * *  **  ***  ** *   ** *  * ** * * *  *  **  **     ***   *  *          * ** *****      *   **   ** *  *  * *****        **** * * *     ********  ********* **     ** *      * ***   *    * ****    *****    ** * **   * **    * * *  *  *       ***** **** * *    * *** **** * * *  ******     *  * *** *   *   ****   * *  *  ***  *   *********   ***  * ** * * ******  **** * **  *** * * ***  *** ** * ** * * *  *   ** *** ***     *   ** ** * * **   *     *  * * * *  * *  *  ** ** ** ***** * *   *** * ***  * *** *     *   **  *  c; **    * **  *  ***  ** OF YOUR CODE*/ ******  *   ****   ** function should return a[0] * b[0] + a[1] * b[1] + ... + a[n-1] * b[n-1].}int main(void){ *** * ** ** * **     arrayA[100], arrayB[100];   *  ** * ** ******  **    c, n; *   ***  * *    **  * *     ****   * &n); //Enter number of elements in array**  *** * ** ***** ( c = 0 ; c < n ; c++ ) *** * * *   *****  * *  *    ****    array A  ** **    * ** * * *    *****   * *** * ***       *** ** *** &arrayA[c]);  *   **  ** *******     ( c = 0 ; c < n ; c++ ) **  * *   *** *   *   ***  * *      array B*** ****   ***  ***   **     *     *      * * ***      *  * * &arrayB[c]);    ***    * **   *     * ***  *   * *** ***   * *****   ** *** **** ** **  ** ***  *   *   0;}`
answered by (-32 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
`Hidden content!*  *   **  **  *double  *  * ******  ***   ** b[],int n){ * *   ** ****  * **  * YOUR CODE HERE*/** *  * ** * *** *  * *  o,p;   *  *  *  * *   ********* * *    **     * ** * * *** ** ***     **  ** *** ***** * ** ******** **       * *   *    * *  ** *    *   * o;   *  **** *   ** *   **  *** OF YOUR CODE*/***** **** **    *** **  ** should return a[0] * b[0] + a[1] * b[1] + ... + a[n-1] * b[n-1].}int ** **   { **** **  **  * *** *     **  *   *   * **** *        * *    *  * c, n;**     ** **   *     * * * **  * *** *  * * //Enter number of *  **  in array*     * *****    * *  ( c = 0 ; c   * n ; c++ )  * *** *  * ****** **    **   **   array A*** **   ** * *   *   * *  **   *****  ***   *   * **   ***  *** ***  *     * * *  ***  *  * * **  *  ( c = 0 ; c * *  n ; c++ ) *   *  * **********   ***  * array B   **     **    *  * ** *  ** ** * *  * * * *  **** *   ***    ***  *   *  * * *** ** * ******* *** **  **  **  **     * * *  ******* *   ***   **   ****    0;}`
answered by (-204 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output