编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛  
全能 ASP / PHP / ASP.NET 主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
发新话题
打印

求助

求助

小弟初学编程,编了一个程序,功能是,把输入的数排序,然后插入一个数,请高手指点问题出在什么地方。感激不尽
#include <stdio.h>

void main()
{
    int a[11];
    int i,j,n,end,temp,temp_1,temp_2;
    printf("Enter six numbers:\n");
    for(i=1; i<=6; i++)
    {
        printf("a[%d]=",i);
        scanf("%d",&a[i]);
        printf("\n");
    }
    printf("The Origin number:\n");
    for(i=1; i<=6; i++)
        printf("%d  ",a[i]);
    for(i=1; i<=5; i++)    /*sort*/
        for(j=1; j<=6-i; j++)
        {
            if(a[j]>=a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    printf("\nThe sorted number:\n");
    for(i=1; i<=6; i++)
        printf("%d  ",a[i]);
    
    printf("\nEnter the number you want to insert: ");
    scanf("%d ",&n);
    end=a[6];
    if(n >= end)
        a[7]=n;
    else
    {
        for(i=1; i<=6; i++)
        {
            if(a[i] >= n)
            {
                temp_1=a[i];
                a[i]=n;
                for(j=i+1; j<=7;j++) /*用两个变量temp_1,temp_2 可以实现数组元素后移*/
                {
                    temp_2=a[j];
                    a[j]=temp_1;
                    temp_1=temp_2;
                }
                break;
            }
        }
        
    }
    
    printf("The result:\n");
    for(i=1; i<=7; i++)
    {
        printf("a[%d]=%6d  ",i,a[i]);
    }
    
}

TOP

....

[ 本帖最后由 himpo 于 2008-7-6 15:20 编辑 ]

TOP

#include <stdio.h>

void main()
{
    int a[11];
    int i,j,n,end,temp,temp_1,temp_2;
    printf("Enter six numbers:\n");
    for(i=1; i<=6; i++)
    {
        printf("a[%d]=",i);
        scanf("%d",&a[i]);
        printf("\n");
    }
    printf("The Origin number:\n");
    for(i=1; i<=6; i++)
        printf("%d  ",a[i]);
    for(i=1; i<=5; i++)    /*sort*/               这里怎么还使用I循环?上面的FOR就是使用I了啊!
        for(j=1; j<=6-i; j++)
        {
            if(a[j]>=a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }                                   这里是把位置掉转!
    printf("\nThe sorted number:\n");
    for(i=1; i<=6; i++)
        printf("%d  ",a[i]);                  这里不加{}?
   
    printf("\nEnter the number you want to insert: ");
    scanf("%d ",&n);
    end=a[6];
    if(n >= end)
        a[7]=n;
    else
    {
        for(i=1; i<=6; i++)
        {
            if(a[i] >= n)
            {
                temp_1=a[i];
                a[i]=n;
                for(j=i+1; j<=7;j++) /*用两个变量temp_1,temp_2 可以实现数组元素后移*/
                {
                    temp_2=a[j];
                    a[j]=temp_1;
                    temp_1=temp_2;
                }
                break;
            }
        }
        
    }
   
    printf("The result:\n");
    for(i=1; i<=7; i++)
    {
        printf("a[%d]=%6d  ",i,a[i]);
    }
   
}
编程小菜

TOP

为什么你们总是在 main()前面加一个void, 不加是完全可以的呀,不懂?

TOP

加不加无所谓拉,只是标准规定要有返回值类型
大家一起来编程吧!

TOP

输入不匹配!

scanf("%d ",&n);

这里输入n值,输入后要加空格再回车。。。

改成scanf("%d",&n);也就是去掉空格就可以了。。

TOP

i 没问题

楼上说的是对的!i没有问题,作用域不同,不冲突.第一个i作用域仅仅限于本for 循环

TOP

发新话题