Data_Structure/作業/老師程式/recursive-series=proc.c
2025-01-20 21:30:53 +08:00

34 lines
877 B
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
Program: recursive-series-proc.c (Report comments/bugs to chikh@yuntech.edu.tw)
Function: 以間接遞迴的方式為二個數列進行個別元素相乘,再將個別乘積加總,類似為二個向量進行內積
(inner product)運算。本程式設mul()函數專責進行數列內對應元素之相乘運算把乘積結果交給add()
函式作加總add()內再呼叫mul()要求mul()計算剩餘元素的乘積,如此反覆運行。透過這二個函數互
相呼叫,達到效果
*/
#include <stdio.h>
int mul(int *, int *, int);
int add(int *, int *, int, int);
int mul(int *A, int *B, int i) {
int c;
if ((c=A[i]*B[i]) == 0)
return 0;
else
return add(A,B,i+1,c); //把c累加到
}
int add(int *A, int *B, int i, int c) {
return c+mul(A,B,i);
}
int main() {
int A[] = { 1, 2, 3, 4, 5, 0},
B[] = {-1,-2,-3,-4,-5, 0}; //最末元素0代表數列至此結束相關運算也將在此告一段落
printf("\n二數列個別元素乘積和 = %d",mul(A,B,0));
return 0;
}