码迷,mamicode.com
首页 > 编程语言 > 详细

数组依然有序,求看下程序问题在哪儿?在有序数组中插入一个数

时间:2018-04-01 16:09:06      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:i+1   main   print   void   body   个数   有序   put   out   

有若干整数按从小到大顺序放在数组中,用户输入一个数插入到此数组中,数组中的数依然按从小到大排列。求大佬看一下这个程序哪里有问题?
#include<stdio.h>
#include<stdlib.h>
void inserer(int *s,int x,int *n){
    int i,j=0;
    while(j<*n && s[j]<x) j++;
    for(i=(*n)-1;i>=j;i--) s[i+1]=s[i];
    s[i]=x;
    *n=(*n)+1;
}
void output(int *s,int *n){
    int i;    for(i=0;i<*n;i++) printf("%d  ",s[i]);
    printf("\n");
}
int main(void){
    int s[10]={1,6,7,10,14,18,22,29,36,47},x;
    int n=10;    printf("Entrez un chiffre : ");
    scanf("%d",&x);    
    printf("Le tableau precedent : ");
    output(s,&n);
    inserer(s,x,&n)
    printf("Le nouveau tableau : ");
    output(s,&n);
    return EXIT_SUCCESS;
}

假如输入12,运行结果:
Entrez un chiffre : 12
Le tableau precedent : 1  6  7  10  14  18  22  29  36  47
Le nouveau tableau : 1  6  7  12  14  14  18  22  29  36  48  12  9900928  0  4199400  0  0  0  46  0  4225568  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

--------------------------------http://huyunsong1314.blog.163.com/

数组依然有序,求看下程序问题在哪儿?在有序数组中插入一个数

标签:i+1   main   print   void   body   个数   有序   put   out   

原文地址:https://www.cnblogs.com/5aafa/p/8686729.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!