Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
实验室代码逻辑错误,C程序初学者_C_Loops - Fatal编程技术网

实验室代码逻辑错误,C程序初学者

实验室代码逻辑错误,C程序初学者,c,loops,C,Loops,我已经做了一个程序,我的教授希望我特别按照她想要的方式来做,但是输出与正确的输出不一样 这是我的代码: #include <stdio.h> #define N 16 int xchg(int *a, int *b); int main (){ int i, j, k, count; int Num[N] = {7, 1, 993, -5, 0, 16, -451, 12, 89,28, 77, 384, -2, 38, -17, 201}; for(

我已经做了一个程序,我的教授希望我特别按照她想要的方式来做,但是输出与正确的输出不一样

这是我的代码:

#include <stdio.h>
#define N 16

int xchg(int *a, int *b);

int main (){

    int i, j, k, count;
    int Num[N] = {7, 1, 993, -5, 0, 16, -451, 12, 89,28, 77, 384, -2, 38, -17, 201};

    for(i = 0; i <= N - 1; i++){
        for(j = i + 1; j <= N; j++){
        count += xchg(&Num[i], &Num[j]);
        }
        for(k = 0; k < N; k++){
            if(k <= 15){
                printf("%d ", Num[k]);
            }
            else
                printf("%d", Num[k]);
            }
    printf("\n");
    }

    printf("total exchanges: %d\n", count);

}

int xchg(int *a, int *b){

    int c;

    if ( *a > *b )
        c = 1;

    else 
        c = 0;

return c;
}
以下是正确的输出:

-451 7 993 1 0 16 -5 12 89 28 77 384 -2 38 -17 201
-451 -17 993 7 1 16 0 12 89 28 77 384 -2 38 -5 201
-451 -17 -5 993 7 16 1 12 89 28 77 384 0 38 -2 201
-451 -17 -5 -2 993 16 7 12 89 28 77 384 1 38 0 201
-451 -17 -5 -2 0 993 16 12 89 28 77 384 7 38 1 201
-451 -17 -5 -2 0 1 993 16 89 28 77 384 12 38 7 201
-451 -17 -5 -2 0 1 7 993 89 28 77 384 16 38 12 201
-451 -17 -5 -2 0 1 7 12 993 89 77 384 28 38 16 201
-451 -17 -5 -2 0 1 7 12 16 993 89 384 77 38 28 201
-451 -17 -5 -2 0 1 7 12 16 28 993 384 89 77 38 201
-451 -17 -5 -2 0 1 7 12 16 28 38 993 384 89 77 201
-451 -17 -5 -2 0 1 7 12 16 28 38 77 993 384 89 201
-451 -17 -5 -2 0 1 7 12 16 28 38 77 89 993 384 201
-451 -17 -5 -2 0 1 7 12 16 28 38 77 89 201 993 384
-451 -17 -5 -2 0 1 7 12 16 28 38 77 89 201 384 993
total exchanges:51
请帮我把输出转换成正确的输出!我只写了三个月的代码,因此我为基本错误道歉。

\include
#include <stdio.h>

#define N 16

int xchg(int *a, int *b);

int main (void){
    int i, j, k, count = 0;
    int Num[N] = {7, 1, 993, -5, 0, 16, -451, 12, 89,28, 77, 384, -2, 38, -17, 201};

    for(i = 0; i < N - 1; i++){
        for(j = i + 1; j < N; j++){
            count += xchg(&Num[i], &Num[j]);
        }
        for(k = 0; k < N; k++){
            if(k < N-1){
                printf("%d ", Num[k]);
            } else {
                printf("%d", Num[k]);
            }
        }
        printf("\n");
    }
    printf("total exchanges: %d\n", count);
}

int xchg(int *a, int *b){
    int s = (*a > *b);

    if(s){
        int t = *a;
        *a = *b;
        *b = t;
    }
    return s;
}
#定义N 16 int xchg(int*a,int*b); 内部主(空){ int i,j,k,count=0; int Num[N]={7,1993,-5,0,16,-451,12,89,28,77,384,-2,38,-17,201}; 对于(i=0;i*b); 若有(s){ int t=*a; *a=*b; *b=t; } 返回s; }
您的代码中有一些问题

一般来说,你应该问一些你不确定的问题

从你发布的内容。看起来您希望对数组Num执行冒泡排序,并计算发生的eChange/Swap总数

#include <stdio.h>
#define N 16

int xchg(int *a, int *b);

int main (){
    int i, j, k, count = 0;  // Initialize count to be 0 -- Number of exchanges
    int Num[N] = {7, 1, 993, -5, 0, 16, -451, 12, 89,28, 77, 384, -2, 38, -17, 201};

    for(i = 0; i <= N - 1; i++){
        for(j = i + 1; j <= N; j++){
            count += xchg(&Num[i], &Num[j]); // counters number of swaps and exchanges values as necessary
        }
        for(k = 0; k < N; k++){         // Prints out newest order of array
            if (k < N - 1)
                printf("%d ", Num[k]);
            else
                printf("%d", Num[k]);
        }
        printf("\n");
    }
    printf("total exchanges: %d\n", count); // Prints total number of exchanges
}

int xchg(int *a, int *b){ // Function to swap values if *a > *b
    if ( *a > *b ){
        int tmp = *a;
        *a = *b;
        *b = tmp;
        return 1;
    }
    return 0;
}
#包括
#定义N 16
int xchg(int*a,int*b);
int main(){
int i,j,k,count=0;//将count初始化为0——交换次数
int Num[N]={7,1993,-5,0,16,-451,12,89,28,77,384,-2,38,-17,201};
对于(i=0;i*b){
int tmp=*a;
*a=*b;
*b=tmp;
返回1;
}
返回0;
}

当然
xchg
应该,我不知道,交换值@我很确定他的意思是,他从三个月前才开始学习编程(不是专门在这个项目上)。您可能需要
sum+=xchg(&Num[i],&Num[j])进行的交换总数。如果进行了交换,您可能还希望交换这些值。你应该补充一些你不清楚的细节问题。@TimothyMurphy是的,没错,我只学了三个月的代码,所以我知道我不好,如果我能得到一些帮助,也许我会更好地理解代码,到目前为止,要获得此代码的正确输出对我来说很困难。1)
xchg
必须替换指针指示的值。这个程序还有一个数组超出了访问范围。@ooga好吧,从接收变量
sum
判断,它应该把它们相加,或者减法,或者乘法,或者其他什么。结束不特定的问题是有原因的。不确定是否将布尔结果分配给int s。没有必要缩短这段代码,是吗?
(*a>*b)
的结果是
int
0
1
)。@BLUEPIXY您是否意识到,通过回答主题外的问题,您正在降低堆栈溢出(使堆栈溢出更严重)的价值?你为什么这么做?我不认为这是离题的。这是意见分歧。OP不知道如何交换。正如你所看到的,这个问题现在被搁置,因此脱离了主题。如果规则中明确规定了这一点,并且大多数人都同意这些规则,那么很难说这是一个意见问题。OP要求的只是请帮助我将输出转换为正确的输出!。您表示他不知道如何交换,但您仍然提供了完整的代码。假设您相信您的陈述是正确的,那么为什么不只提供交换代码呢?
#include <stdio.h>
#define N 16

int xchg(int *a, int *b);

int main (){
    int i, j, k, count = 0;  // Initialize count to be 0 -- Number of exchanges
    int Num[N] = {7, 1, 993, -5, 0, 16, -451, 12, 89,28, 77, 384, -2, 38, -17, 201};

    for(i = 0; i <= N - 1; i++){
        for(j = i + 1; j <= N; j++){
            count += xchg(&Num[i], &Num[j]); // counters number of swaps and exchanges values as necessary
        }
        for(k = 0; k < N; k++){         // Prints out newest order of array
            if (k < N - 1)
                printf("%d ", Num[k]);
            else
                printf("%d", Num[k]);
        }
        printf("\n");
    }
    printf("total exchanges: %d\n", count); // Prints total number of exchanges
}

int xchg(int *a, int *b){ // Function to swap values if *a > *b
    if ( *a > *b ){
        int tmp = *a;
        *a = *b;
        *b = tmp;
        return 1;
    }
    return 0;
}