标题:如何给动态一维数组赋值
只看楼主
荔荔
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-12-22
结帖率:0
已结贴  问题点数:20 回复次数:2 
如何给动态一维数组赋值
求解错因!#include<stdio.h>
#include<stdlib.h>
void*calloc(unsigned numElements,unsigned sizeOFElements);
int Retrieve(int array[],int length, int x);
int Ave(int array[],int length);
int Max(int array[],int length);
void Stand(int *array,int length);
int main()
{  int *s;
   int i,n,ave,max,X=0,retrieve;
   do
   {
       printf("Please input n:\n");
       scanf("%d",&n);
   }while(n<=0);
   s=(int*)calloc(n,sizeof(int));
   if(s=NULL)
   {
       printf("allocation failure");
       exit(1);
   }
   printf("please input n element:\n");
   for(i=0;i<n;i++);
   scanf("%d",s[i]);
   for(i=0;i<n;i++);
   printf("%d",s[i]);
   ave=Ave(s,n);
   max=Max(s,n);
   printf("please input X:\n");
   scanf("%d",X);
   retrieve=Retrieve(s,n,X);
   Stand(s,n);
   printf("after sort the array is:\n");
   for(i=0;i<n;i++);
   printf("%5d",s[i]);
   return 0;   
}
int Ave(int array[],int length)
{
    int i=0,t=0,ave;
    for(i=0;i<length;i++);
    t+=array[i];
    ave=t/length;
    return(ave);
}
int Max(int array[],int length)
{
    int *max;
    int i=0;
    max=array;
    for(i=0;i<length;i++);
    if(array[i]>*max)
        *max=array[i];
    return(*max);
}
int Retrieve(int array[],int length,int x)
{
    int i=0;
        while(i<length)
        {
            if (array[i]==x)
                break;
                i++;
        }
        if(i<length)
           return(i);
        else
           return(-1);
}
void  Stand(int array[],int length)
{
    int index,k,i=0,temp;
    for(k=0;k<length-1;k++);
    {  
        index=k;
        for(i=k+1;i<length;i++);
           if(array[i]<array[index])
               index=i;
        if(index!=k)
        {
            temp=array[index];
            array[index]=array[k];
            array[k]=temp;
        }
    }
}



   

   


搜索更多相关主题的帖子: void ave allocation failure include 
2012-12-22 21:53
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
得分:10 
scanf("%d",&s[i]);

scanf("%d",&X);

My life is brilliant
2012-12-22 22:11
神龙赖了
Rank: 10Rank: 10Rank: 10
来 自:萨塔星
等 级:青峰侠
威 望:2
帖 子:711
专家分:1788
注 册:2012-10-13
得分:10 
楼上正确

I have not failed completely
2012-12-23 20:24



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-392989-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017887 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved