c 程序 递归函数 奇数

发布于 2024-10-25 20:27:28 字数 1067 浏览 4 评论 0原文

我需要 C 中的一个递归函数,它检查/比较奇数位置的值(整数)的总和与数组偶数位置的值的总和。还打印(在函数内部)更大的总和!

例如:

printf("\nThe bigger sum is %d. \n evensum = %d , oddsum = %d \n",bigger, evensum, oddsum);

假设该数组有 8 个位置,我们从 main() 中用随机值填充它。

这是我到目前为止所拥有的:

#include <stdio.h>
#define N 4


int checksum(int matrix[], int position, int sum1, int sum2); 

int main(void)
{
    int mat[N];
    int i,j;

    printf("\nEnter the %d numbers:\n", N); 

      for(i = 0; i < N; i++)
      {
        printf("%2d> ",i);
        scanf("%d", &mat[i]);
      }

      checksum(mat, 0, 0, 0);       


}

int checksum(int m[], int pos, int s1, int s2){

if(pos<N){
    if(pos==0){
            s1 = m[pos];    
            checksum(m, pos+1, s1, s2);         
        }else{
            if(pos%2){
                return s1 + checksum(m, pos+1, s1, s2);     

            }else{
                return s2 + checksum(m, pos+1, s1, s2);                         
            }

        }

    }

}

I need a recursive function in C, that checks/compare the sum of values(integers) in odd positions, with the sum of values in even positions of an array. Also print(inside the function) the bigger sum!

Like:

printf("\nThe bigger sum is %d. \n evensum = %d , oddsum = %d \n",bigger, evensum, oddsum);

Suppose that array has 8 positions and we fill it from main() with random values.

This is what I have so far:

#include <stdio.h>
#define N 4


int checksum(int matrix[], int position, int sum1, int sum2); 

int main(void)
{
    int mat[N];
    int i,j;

    printf("\nEnter the %d numbers:\n", N); 

      for(i = 0; i < N; i++)
      {
        printf("%2d> ",i);
        scanf("%d", &mat[i]);
      }

      checksum(mat, 0, 0, 0);       


}

int checksum(int m[], int pos, int s1, int s2){

if(pos<N){
    if(pos==0){
            s1 = m[pos];    
            checksum(m, pos+1, s1, s2);         
        }else{
            if(pos%2){
                return s1 + checksum(m, pos+1, s1, s2);     

            }else{
                return s2 + checksum(m, pos+1, s1, s2);                         
            }

        }

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独自唱情﹋歌 2024-11-01 20:27:28

除了他们取笑你之外,我知道你是诚实地标记“家庭作业”。我要向您展示的是您的解决方案。为了充分利用这一点,您应该理解它。如果没有,那你只是在欺骗自己。我真的希望这能以其他方式帮助你,而不仅仅是完成作业。

您仍然需要用随机值填充向量,并打印更大的值。但这是小菜一碟。递归可以这样进行:

#include <stdio.h>
#define MAX 10

int sum(int *x, int n, int odd)
{

    if(odd==-1)
    {
        printf("odd = %d, even = %d\n", sum(x, n, 1), sum(x, n, 0));
        return 0;
    }

    if(n%2!=odd)
        return sum(x, n+1, odd);

    if(n>=MAX)
        return 0;

    return x[n]+sum(x, n+1, odd);
}



int main(void)
{
    int x[MAX]={0,1,2,3,4,5,6,7,8,9};

    sum(x, 0, -1);

    return 0;
}

一方面,您的问题缺少...如果您将鼠标光标暂停在标签作业上阅读定义,您将阅读“并表现出足够的努力”。

那么,请在根据需要调整代码后,在这里显示最终答案作为您学习 c 语言努力的象征如何? ;)

小心!
贝科。

Besides all the fun they picked at you, I understand you was honest tagging "homework". What I'm about to show you is your solution. To make the most of this, you should understand it. If not, you are just fooling yourself. I really hope this can help you in other ways than just a homework done.

You still need to fulfill the vector with random values, and to print the bigger. But this is piece of cake. The recursion can be made like this:

#include <stdio.h>
#define MAX 10

int sum(int *x, int n, int odd)
{

    if(odd==-1)
    {
        printf("odd = %d, even = %d\n", sum(x, n, 1), sum(x, n, 0));
        return 0;
    }

    if(n%2!=odd)
        return sum(x, n+1, odd);

    if(n>=MAX)
        return 0;

    return x[n]+sum(x, n+1, odd);
}



int main(void)
{
    int x[MAX]={0,1,2,3,4,5,6,7,8,9};

    sum(x, 0, -1);

    return 0;
}

For one thing your question lacks... If you read the definition pausing your mouse cursor at the tag homework, you will read "and show sufficient effort".

So, please, after adapting the code as you need, how about you show the final answer here as a token of your effort to learn c language? ;)

Take care!
Beco.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文