C-检查和分段故障(堆芯倾倒)

C-检查和分段故障(堆芯倾倒),c,sum,C,Sum,我试图在C中编写一个简短的a程序,以创建计算数字立方和的函数(例如2*2*2),但当我尝试启动该程序时,收到了以下消息: “分段默认值(核心转储)” 求和偶立方。c,第14行:检查通过的求和偶立方。c,第15行: 检查通过的和偶数立方体.c,第16行:检查通过的和偶数立方体.c, 第17行:检查通过的和偶立方。c,第18行:检查通过 求和偶立方。c,第56行:检查通过的求和偶立方。c,第57行: 检查通过的分段故障(堆芯转储) 在我的程序下方的C:(该错误可能发生在函数sum\u even\u

我试图在C中编写一个简短的a程序,以创建计算数字立方和的函数(例如2*2*2),但当我尝试启动该程序时,收到了以下消息:

“分段默认值(核心转储)”

求和偶立方。c,第14行:检查通过的求和偶立方。c,第15行: 检查通过的和偶数立方体.c,第16行:检查通过的和偶数立方体.c, 第17行:检查通过的和偶立方。c,第18行:检查通过

求和偶立方。c,第56行:检查通过的求和偶立方。c,第57行: 检查通过的分段故障(堆芯转储)

在我的程序下方的C:(该错误可能发生在函数sum\u even\u cubes\u rec()处)

#包括“base.h”
整数和偶立方(整数n);
无效和\u偶数\u立方体\u测试(无效){
//(a)
检查_expect_i(和偶立方(0),0);
检查_expect_i(和偶立方(1),0);
检查_expect_i(和偶立方(2,8));
检查_expect_i(和偶立方(3,8));
检查_expect_i(和偶立方(4),72);
}
整数和偶立方(整数n){
//(b)
整数和偶数立方=0;
如果(n%2==0){

对于(int i=0;i)我不完全清楚这个程序的目的是什么,但是编写一个成功的程序的一个非常简单的方法是用一个坚实的基础并建立它。 下面是一个迭代计算数字立方体的程序。您也可以简单地返回数字乘以自身3倍或
(x*x*x)

如果要将1到n的所有多维数据集相加:

#include <stdio.h>
#include <math.h>

int main(){
    int i, n = 10, result, totalEven = 0, totalOdd = 0;
    for (i = 1; i < n; i++){
        if (i % 2 == 0){
            /* The number is even */
            result = pow(i, 3);
            totalEven += result;
        } else {
            /* The number is odd */
            result = pow(i, 3);
            totalOdd += result;
        }
    }
    printf("The sum of all even cubes from 1 to %d is %d.\n", n, totalEven);
    printf("The sum of all odd cubes from 1 to %d is %d.\n", n, totalOdd);
    printf("The sum of all cubes from 1 to %d is %d.\n", n, totalEven+totalOdd);
    return 0;
}
#包括
#包括
int main(){
int i,n=10,结果,total偶数=0,total奇数=0;
对于(i=1;i
这与Expect脚本工具有什么关系?
sum\u-even\u-cubes\u-rec(n)
in
sum\u-even\u-cubes\u-rec(n)
#include <stdio.h>

int simpleCube(int x){
    return (x*x*x);
}

int iterativeCube(int x, int n){
    int i, total = 1;
    for (i = 0; i < n; i++){
        total *= x;
    }
    return total;
}

int main(){
    printf("2 Cubed is %d\n", iterativeCube(2, 3));
    return 0;
}
#include <stdio.h>
#include <math.h>

int main(){
    int base = 2, exponent = 3, result;
    result = pow(base, exponent);
    printf("%d^%d = %d\n", base, exponent, result);
    return 0;
}
#include <stdio.h>
#include <math.h>

int main(){
    int i, n = 10, result, totalEven = 0, totalOdd = 0;
    for (i = 1; i < n; i++){
        if (i % 2 == 0){
            /* The number is even */
            result = pow(i, 3);
            totalEven += result;
        } else {
            /* The number is odd */
            result = pow(i, 3);
            totalOdd += result;
        }
    }
    printf("The sum of all even cubes from 1 to %d is %d.\n", n, totalEven);
    printf("The sum of all odd cubes from 1 to %d is %d.\n", n, totalOdd);
    printf("The sum of all cubes from 1 to %d is %d.\n", n, totalEven+totalOdd);
    return 0;
}