0 like 0 dislike
12.1k views

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

寫inner_product函數。函數會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; 

第一行是陣列大小,第二行A元素,第三行B元素

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 in Chapter 9: Functions by (5.2k points)
ID: 37277 - Available when: 2017-12-14 18:00 - Due to: Unlimited

edited by | 12.1k views

38 Answers

0 like 0 dislike
Hidden content!
**** * * * ***
* ** *** * ** * * * ** n)
{
** * * * * *** int i = 0;

*** ** * * int m = 0;

** * k = 0;

** * * * = 0; i * ** n; **
** *** * * ** {
** ** * ** * *** * m = * *

**** **** * ** * ** *** k += m;
***** * }

** *** ** * * k;
}

** *** *
{
** * * * * ** * * * * **
** ** * * * c, n;

** * * ** *** * * *** * * * * of ** ** in ***
* * ** * * * ( c = 0 ; c *** n ; * ** ** * A
* ** ** *** ** * * * *** ******* **** *** * * * ****
** * * ( c = 0 ; c ** * n ; * * * ** B
* * * * ** * ** * ** ** ****** ** * * ** ***

***** ** * ** ** ****** * * * ****** * * * * *

** * *** * * ** * ** * 0;
}
answered by (-285 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*/
** ** ** * * *** c,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 (-167 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*/
* ****** *** * * * ** ** c=0,d=0;
*** * * * ****** * **
**** * *** ** * ** *
** ** ***** ** * ** *** *** * **** *

* * ** * * * * * ***** * *
* ******* *** * * ****** * d;
**** **** **** 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 (-329 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,d=0;
* * **** ****** ** * *
***** *** * * * *
* ** * *** ***** * * * ** ** *** ***
* ** ** ** ** *
return d;
}

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 (-229 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>
double inner_product(double a[],double b[],int n);

int main(void)
{
** * **** * ** *** * n,i;
* ** * ***** * ** * * * a[100],b[100];
** * * **** ** **** *** * * * * *** *** * ****
** ****** * * * * * ** **** * * * *** * *
* * *** * * ** **** ** * ** *** **** * * * ** ** ** * * ******
** ** * * *** *** *** * * ** * *
* *** * **** ******** ** * ******** **** * *** * ** ** ** * ** *** ** *

* **** * **** * * * ** * **** * ***** ** **

*** * * ***** * **** 0;
}
double inner_product(double a[],double b[],int n)
{
* *** ** * * ** ** *** * * * i,sum=0;
* * ****** ** *** * * * *** * *
* ** ** ** *** *** **
** * * * * * * * * * * *** ** ** * * += a[i]*b[i];
* * ** * ** * **
* ** ** * **** ****** * sum;
}
answered by (-281 points)
edited by
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 0
prog.c: In function 'main':
prog.c:10:18: warning: format '%f' expects argument of type 'float *', but argument 2 has type 'double *' [-Wformat=]
         scanf("%1f",&a[i]);
                  ^
prog.c:12:18: warning: format '%f' expects argument of type 'float *', but argument 2 has type 'double *' [-Wformat=]
         scanf("%1f",&b[i]);
                  ^
0 like 0 dislike
Hidden content!
****** * * *** *
double **** * * * b[],int n)
{
* *** *** ****** ** * * * YOUR CODE HERE*/



******* ** ** * **** ** i;
** ** * * ** * **** * ****** * result = 0;
** * * * * *** * **** (i = 0; i * * n; i++)
**** * *** ** ** * * **
** **** **** * * ** ** * += ** **
* **** * * * * ** * *
*** * * * ******* ** * * **
}

* * * * ** * * * * OF YOUR CODE*/
* *** *** *** ** * *** ****** should return a[0] * b[0] + a[1] * b[1] + ... + a[n-1] * b[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 (-304 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,j,c[999],k;
** * **** ** ****** * *
** ******** * **** **
* * * ** ***** * ** ** * * * **** ** * **
**** *******
* ** * * ** ** *** ***
** ** **** * *** * ***
*** * ** * ** ***
* ** ** * * * ***** *** * *** * *** *
** * * ** *****
** * * ** * * * k;
}

int main(void)
{
**** ** *** ** ****** arrayA[100], arrayB[100];
* * * ** **** * * ** * 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 (-255 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,total=0;
**** *** ** ** * * * * ***** *
*** * ** * ****** *** ***** **** * ** * ** **** **** * * *
* * * * ** *****
* *** * * * ******* ** **** total;
}

int main(void)
{
* *** ** * ** ** arrayA[100], * * *
*** * ** * ** * 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 (54 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
** ******* * *
* * * *** *** ** * * * ** n)
{
* ** * * *** * ** * * ** YOUR CODE HERE*/
int s=0,u;
* * ** ****
{
* ****
* * *** * ** * * *

}
** s;
* ** * * * * ** ** OF YOUR **
* * * *** ** ****** * ** * ** should *** * 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 (-336 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;
**** **** ** **
*** * **** **
**** * * * ** *** * ** * *
* *** * **
**** ** ** * * ** ** * * += 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 (-323 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:104.23.197.94
©2016-2026

Related questions

0 like 0 dislike
50 answers
[Exercise] Coding (C) - asked Dec 13, 2017 in Chapter 9: Functions by semicolon (5.2k points)
ID: 37278 - Available when: 2017-12-14 18:00 - Due to: Unlimited
| 16.8k views
0 like 0 dislike
41 answers
[Exercise] Coding (C) - asked Dec 14, 2017 in Chapter 9: Functions by semicolon (5.2k points)
ID: 37370 - Available when: 2017-12-14 18:00 - Due to: Unlimited
| 11.9k views
0 like 0 dislike
70 answers
[Exercise] Coding (C) - asked Dec 7, 2017 in Chapter 9: Functions by semicolon (5.2k points)
ID: 35783 - Available when: 2017-12-07 18:00 - Due to: Unlimited
| 18.9k views
12,783 questions
183,442 answers
172,219 comments
4,824 users