我是编程新手,我正在尝试做简单的C程序,使用y / n循环填充数组并获取总和/平均值/条目数/最小值和最大值,但我得到的都是零在末尾。任何提示都将有所帮助,下面是代码。预先感谢!
#include <stdio.h>
int main() {
int N, Number, i= 0,j=0, c;
double sum, average, min, max;
double array[N];
double arr[100];
char YesNo = 'y';
int count = 0;
printf("Please enter maximum size of the array: ");
scanf("%d",&N);
while(1){
printf("Do you want to enter a number? ");
scanf("%c",&YesNo);
//if(YesNo != 'Y' || YesNo != 'y' || YesNo != 'n' || YesNo != 'N'){
//printf("Please enter y or n ");
//break;
//return 0;}
if(YesNo =='y' || YesNo == 'Y'){
printf("Enter number: ");
scanf("%f", &array[i]);
array[i]=arr[j];
i++;
count++;
j++;
}
if(YesNo =='n' || YesNo == 'N'){
break;
}
}
max = arr[0];
for (c = 1; c < count; c++) {
if (arr[c] > max)
{
max = arr[c];
}
}
min = array[0];
for (c = 1; c < count; c++)
{
if (array[c] < min)
{
min = array[c];
}
}
for (i=0; i<count; i++)
{
sum = sum + arr[i];
average = sum/count;
Number = count;
}
printf("\n Sum is: %f",sum);
printf("\n Number of elements is: %d", count);
printf("\n Average is: %f ", average);
printf("\n Maximum is %f",max);
printf("\n Minimum is %f",min);
return 0;
}
答案 0 :(得分:0)
array [N]可能是大小为0的数组,因为未设置N。
将值读入array [i]后,您需要执行以下操作:
array[i]=arr[j];
所以array [I]中的值现在等于arr [j]中的值,由于未设置任何值,所以它将为0。
使用arr的其他循环也将为0,因为arr从未设置为任何值。
答案 1 :(得分:0)
您提到的代码几乎没有问题。 首先在这里
int N;
double array[N];
N
值是多少?由于N
未初始化,因此可能会占用大量垃圾数据并尝试创建包含许多导致未定义行为的元素的数组。因此,首先定义N
值。例如
#define N 5
或者在下面两个语句中做这些
printf(“请输入数组的最大大小:”);
scanf(“%d”,&N);
在声明array
之前。对于例如
int N;
printf("Please enter maximum size of the array: ");
scanf("%d",&N);
double array[N];/* this works if compiler supports VLA */
其次,
array[i]=arr[j];
arr[j]
包含什么?是垃圾所以喜欢
arr[j] = array[i];
array[i]
包含用户已扫描的数据。
还要考虑如下用例,假设用户输入N
为5
,并且如果用户在y
内按while(1)
的次数超过5
倍,访问数组中的边界数据会导致不确定的行为。例如,让我们考虑以下代码
int N;
printf("Please enter maximum size of the array: ");
scanf("%d",&N); /* let say 5 */
double array[N]; /* max you can store 5 elements */
while(1){ /* it breaks when user press n or N */
printf("Do you want to enter a number? ");
scanf(" %c",&YesNo); /* what if user didn't press n or N after 5 times, loop doesn't break which makes array[i] going out of bound */
if(YesNo =='y' || YesNo == 'Y'){
scanf("%f", &array[i]); /* this cause problem if user has press y or Y more than N(5) times */
arr[j] = array[i];
/* some code */
}
}
最后一次求平均值
for (i=0; i<count; i++) {
sum = sum + arr[i];
average = sum/count;
Number = count;
}
为什么在执行average = sum/count;
时执行count
average = sum/count;
次,而在计算sum
之后只执行一次for (i=0; i<count; i++) {
sum = sum + arr[i];
}
average = sum/count; /* do only once */
Number = count;
。对于例如
int main(void) {
int N=0,i=0 ,c=0;
double sum, average, min, max;
printf("Please enter maximum size of the array: ");
scanf("%d",&N);
double array[N];
double arr[N];
char YesNo = 'y';
int count = 0;
while(count < N) {
printf("Do you want to enter a number? ");
scanf(" %c",&YesNo);
if(YesNo =='y' || YesNo == 'Y'){
printf("Enter number: ");
scanf("%lf", &array[count]);
arr[count]=array[count];
count++;
}
else
break;
}
max = arr[0];
for (c = 1; c < count; c++) {
if (arr[c] > max)
{
max = arr[c];
}
}
printf("max ele in array : %lf \n",max);
min = array[0];
for (c = 1; c < count; c++) {
if (array[c] < min)
{
min = array[c];
}
}
printf("min ele in array : %lf \n",min);
for (i=0; i<count; i++) {
sum = sum + arr[i];
}
average = sum/count;
printf("Avg of array elements : %lf\n",average);
return 0;
}
示例代码(根据您的代码进行修改)
10-25 23:41:26.018 20420-20420/soft.aasharib.arcoreintegrate E/Zygote: isWhitelistProcess - Process is Whitelisted
10-25 23:41:26.019 20420-20420/soft.aasharib.arcoreintegrate E/libpersona: scanKnoxPersonas
Couldn't open the File - /data/system/users/0/personalist.xml - No such file or directory
10-25 23:41:26.317 20420-20420/soft.aasharib.arcoreintegrate E/ViewRootImpl@470227[MainActivity]: Surface is not valid.
10-25 23:41:26.467 20420-20487/soft.aasharib.arcoreintegrate E/Unity: Google AR Error: java.lang.ClassNotFoundException: Didn't find class "com.unity3d.unitygar.GoogleAR" on path: DexPathList[[zip file "/data/app/soft.aasharib.arcoreintegrate-PUl65upN4UYH79MhLvu6Ww==/base.apk"],nativeLibraryDirectories=[/data/app/soft.aasharib.arcoreintegrate-PUl65upN4UYH79MhLvu6Ww==/lib/arm, /data/app/soft.aasharib.arcoreintegrate-PUl65upN4UYH79MhLvu6Ww==/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]Didn't find class "com.unity3d.unitygar.GoogleAR" on path: DexPathList[[zip file "/data/app/soft.aasharib.arcoreintegrate-PUl65upN4UYH79MhLvu6Ww==/base.apk"],nativeLibraryDirectories=[/data/app/soft.aasharib.arcoreintegrate-PUl65upN4UYH79MhLvu6Ww==/lib/arm, /data/app/soft.aasharib.arcoreintegrate-PUl65upN4UYH79MhLvu6Ww==/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]
10-25 23:41:26.593 20420-20487/soft.aasharib.arcoreintegrate E/Unity: Unable to find unitygar
10-25 23:41:29.182 20420-20487/soft.aasharib.arcoreintegrate E/Unity: Unable to find arcore_unity_api
10-25 23:41:29.194 20420-20487/soft.aasharib.arcoreintegrate E/Unity: Unable to find arpresto_api
10-25 23:41:29.245 20420-20487/soft.aasharib.arcoreintegrate E/Unity: DllNotFoundException: arcore_unity_api
at (wrapper managed-to-native) GoogleARCoreInternal.ARPrestoCallbackManager/ExternApi:ArCoreUnity_setArPrestoInitialized (GoogleARCoreInternal.ARPrestoCallbackManager/EarlyUpdateCallback)
at GoogleARCoreInternal.ARPrestoCallbackManager._Initialize () [0x00000] in <filename unknown>:0
at GoogleARCoreInternal.ARPrestoCallbackManager.get_Instance () [0x00000] in <filename unknown>:0
at GoogleARCoreInternal.ARCoreAndroidLifecycleManager.get_Instance () [0x00000] in <filename unknown>:0
at GoogleARCoreInternal.LifecycleManager.get_Instance () [0x00000] in <filename unknown>:0
at GoogleARCore.ARCoreSession.Awake () [0x00000] in <filename unknown>:0
(Filename: Line: -1)
10-25 23:41:29.255 20420-20487/soft.aasharib.arcoreintegrate E/Unity: DllNotFoundException: arpresto_api
at (wrapper managed-to-native) GoogleARCoreInternal.ARCoreAndroidLifecycleManager/ExternApi:ArPresto_setEnabled (bool)
at GoogleARCoreInternal.ARCoreAndroidLifecycleManager.DisableSession () [0x00000] in <filename unknown>:0
at GoogleARCore.ARCoreSession.OnDisable () [0x00000] in <filename unknown>:0
(Filename: Line: -1)