#include<stdio.h>
#include<stdlib.h>
double mean(int i, int arr[])
{
int j, sum = 0;
for (j = 0; j < i; j++)
{
sum = arr[j] + sum;
}
return (float)sum/i;
}
int main()
{
int arr[100] = { NULL };
int i, n, sum = 0;
printf("How many numbers would you like to enter?");
scanf_s("%d", &n);
while (n > 100 || n < 0)
{
printf("Amount of numbers should be less than 0 and more than 100\n");
scanf_s("%d", &n);
}
for (i = 0; i < n; i++)
{
scanf_s("%d", &arr[i + 1]);
}
printf("%f", mean(i-1, arr[i]));
system("pause");
}
コードを実行すると、読み取りアクセスエラーが発生します。私が作成したmean()関数に問題がありますが、何が問題なのか分かりません。助けて?配列の平均を計算する
一つには、誤って 'scanf_s( "%d個"、&編曲:[I + 1])に' + 1 'を追加しました; ''に変更しscanf_s( "%d"、&arr [i]); ' –
警告を有効にしてコンパイルしましたか? 'mean(i-1、arr [i])'は 'mean(n、arr)'であるべきですか? –
Sidenote、 '"数量は0未満、あなたが実際にテストするものとは逆のようです。 – fvu