為什么這個我在VC上能運行,可到這就不行呢
#include <stdio.h>
int total(int score[]);
int high(int score[]);
int low(int score[]);
int average(int score[]);
void sorting(int score[]);
int main()
{
? ? const int N=10;
? ? int score[N]={67,98,75,63,82,79,81,91,66,84};
? ? printf("total:%d\n",total(score));
? ? printf("high:%d\n",high(score));
printf("low:%d\n",low(score));
?printf("average:%d\n",average(score));
?sorting(score);
? ? return 0;
}
int total(int score[])
{
? ??
? ? int i,sum=0;
? ? for(i=0;i<10;i++)
? ? {
? ? ? ? ? ?sum+=score[i];
? ? ? ? }
? ? return sum;
}
int high(int score[])
{
? ? int i,max=score[0];
? ? for(i=1;i<10;i++)
? ? {
? ? ? ?if(score[i]>max)
? ? ? ?{
? ? ? ? ? ?max=score[i];
? ? ? ?}
? ? }
? return max;
}
int low(int score[])
{
? ? int i,min=score[0];
? ? for(i=1;i<10;i++)
? ? {
? ? ? ? if(score[i]<min)
? ? ? ? {
? ? ? ? ? ? min=score[i];
? ? ? ? }
? ? }
? ?return min;
}
int average(int score[])
{
? ? int i,sum=0,Ave;
? ? for(i=0;i<10;i++)
? ? {
? ? ? ? sum+=score[i];
? ? }
? ? Ave=sum/10;
? return Ave;
? ? }
? ? void sorting(int a[])
? ? {
? ? ? ? int i,j, t;
? ? ? ? for(i=0;i<9;i++)
? ? {
? ? ? ? for(j=0;j<10-i;j++)
? ? ? ? {
? ? ? ? ? ? if(a[j]<a[j+1])
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ?t=a[j];
? ? ? ? ? ? ? ? a[j]=a[j+1];
? ? ? ? ? ? ? ? a[j+1]= t;
? ? ? ? ? ? ? ? }
? ? ? ? }
? ? ? ? }
? ? ? ? for(i=0;i<10;i++)
? ? ? ? {
? ? ? ? ? ? printf("%d\t",a[i]);
? ? ? ? }
? ? }
2015-02-26
我在VC上咋不行呢,你把?int score[N]={67,98,75,63,82,79,81,91,66,84};這句話,換成?int score[10]={67,98,75,63,82,79,81,91,66,84};就應該可以了
2015-02-28
稍微多說一點:
C語言:
在傳統的C中,數組的大小必須能在編譯時確定。
所以在定義數組時如果指定大小只能使用大于0的整型常量(字面量、枚舉常量)。在C中用const修飾的變量并不被視作“常量”,const只是向編譯器強調該變量不能修改。
要么就是不寫大小,而用初始化列表來定義數組,這樣編譯器就根據你提供的列表中元素的個數來確定數組大小,顯然這個數在編譯時是可以確定的。
或者也可以同時指定大小和提供初始化列表,不過列表中的元素個數不能超過你指定的數組大小。
在函數參數列表中聲明的數組還有更多的規定,不過通常不會寫這么復雜的,你先不用管它。
后來C99標準引入了“變長數組”(VLA),允許在定義數組時用變量指定大小,但只限于局部作用域定義的數組(在global scope定義的數組不行)。順便說一句,引入這個特性爭議極大。
C++語言:
C++并沒有變長數組的概念。不過const修飾的變量被視作“常量”,所以可以用const修飾的變量來指定數組大小。
2015-02-27
在C語言中,用變量來確定大小的這種數組,在定義的同時不能被初始化。
不過在C++中可以,所以你是不是用VC++來編譯了?