c中的冒泡排序和getchar

bubble sorting and getchar in c

本文关键字:getchar 冒泡排序      更新时间:2023-10-16

我正在使用microsoft visual studio 2012并试图制作一个冒泡排序。下面是我的代码:

#include "stdafx.h"
#include "String.h"
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    int array[100], n, c, d, swap;
    printf("enter numbers of elementsn");
    scanf_s("%d",&n);
    printf("enter %d integersn", n);
    for (c = 0; c < n; c++){
        scanf_s("%d", array);
    }
    for (c = 0; c < (n - 1); c++)
    {
        for (d = 0; d < n - c - 1; d++)
        {
            if (array[d]>array[d + 1]){
                swap = array[d];
                array[d] = array[d + 1];
                array[d + 1] = swap;
            }
        }
    }
    printf("sorted list in ascending order:n");
    for (c = 0; c < n; c++){
        printf("%dn", &array[c]);
    }
    getchar();
    return 0;
}

首先,我不能让控制台停留一个键输入。getchar()似乎不工作,但我没有任何错误。另外,当我看到控制台时,我可以说数字是"-310892"。我不知道为什么。

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main(void)
{
    int array[100],n,c,d,flag,swap;
    printf("Enter the no. of elementsn");
    scanf("%d",&n);
    for(c=0;c<n;c++)
    {
        scanf("%d",&array[c]);  // here you have to add & for assigning a address to variable in memory 
    }

for(c=0;c<(n-1);c++)
{
    flag=0;
    for(d=0;d<n-c-1;d++)
    {
        if(array[d]>array[d+1])
        {
            swap=array[d];
            array[d]=array[d+1];
            array[d+1]=swap;
            flag=1;
        }
    }
    if(flag==0)
    break;
}
printf("sorted elements in ascending order:n");
for(c=0;c<n;c++)
{
    printf("%dt",array[c]);// you want to print the element not its address so no need of &
}
getch();    
return 0;}
  1. 请注意,我添加了额外的变量"标志",这有助于提高程序的效率,因为当你的元素完成排序时循环会中断,但在你的程序中循环可能会做一些额外的迭代。

我修正了别人提到的错别字,并添加了system("pause")。在VS 2010上为我工作得很好。还没有访问VS 2012来测试它。下面是你的代码:

#include <string.h>
#include <stdio.h>
    int main()
    {
        int array[100], n, c, d, swap;
        printf("enter numbers of elementsn");
        scanf("%d",&n);
        printf("enter %d integersn", n);
        for (c = 0; c < n; c++){
            scanf("%d", &array[c]);
        }
        for (c = 0; c < (n - 1); c++)
        {
            for (d = 0; d < n - c - 1; d++)
            {
                if (array[d]>array[d + 1]){
                    swap = array[d];
                    array[d] = array[d + 1];
                    array[d + 1] = swap;
                }
            }
        }
        printf("sorted list in ascending order:n");
        for (c = 0; c < n; c++){
            printf("%dn", array[c]);
        }
        system("pause"); // <---- Added this!!!
        return 0;
    }

关于冒泡排序的实现:

  1. 你的scanf_s在第一个for循环总是读取数字到数组的第一个位置。
  2. 你的printf在最后一个for循环中需要一个整数,但你提供了一个地址。

为了防止控制台消失,您可以将getchar()替换为system("pause"),尽管这是不可移植的。

纠正这些事情,气泡排序对我有用:

#include "stdafx.h"
#include "String.h"
#include <iostream>
using namespace std;
#include <string.h>
int main() {
    int array[100], n, c, d, swap;
    printf("enter numbers of elementsn");
    scanf_s("%d",&n);
    printf("enter %d integersn", n);
    for (c = 0; c < n; c++) {
        scanf_s("%d", &array[c]);
    }
    for (c = 0; c < (n - 1); c++) {
        for (d = 0; d < n - c - 1; d++) {
            if (array[d] > array[d + 1]) {
                swap = array[d];
                array[d] = array[d + 1];
                array[d + 1] = swap;
            }
        }
    }
    printf("sorted list in ascending order:n");
    for (c = 0; c < n; c++){
        printf("%dn", array[c]);
    }
    system("pause");
    return 0;
}