c - 我正在尝试用用户输入填充一个数组,我已经提示用户现在我正在尝试编写一个 fillArray 函数

标签 c

#include <stdio.h>
#include <stdlib.h>
#define MAX 25
int readTotalNums();
void fillArray (int nums[], int total);
void sortIt (int nums[], int total);
int findMean (int nums[], int total);
int findMedian (int nums[], int total);
int findMode (int nums[], int total);
void printResults (mean, median, mode);
int goAgain ();


int main()
{
int nums[MAX];
int total;
double mean, median, mode;

do
{
    total=readTotalNums();
    fillArray(total,nums);
    sortIt(nums, total);
    mean= findMean (nums,total);
    mode=findMode(nums,total);
    median=findMedian(nums, total);
    printResults(mean,mode, median);
}while(goAgain());
return 0;
}

int readTotalNums()
{
int total;
do
{
    printf("How many numbers would you like to enter? (1-25)\n");
    scanf("%i",&total);
    while(getchar()!='\n');

}while (total<1 || total>MAX);
return total;
}

void fillArray (int nums[], int total)
{
int x;
for (x=0; x<total; x++)
    {
        printf("enter your numbers\n");
        scanf("%i", &nums[x]);
    while(getchar()!='\n');
    }
}

所以我决定只提供我已经拥有的东西...因为问题可能出现在我的 fillArray 函数之前... 我不断收到“该程序已停止工作”的消息,我知道如果您不删除回车符,您通常会收到该消息。我是一个新手,只是想通过我唯一的专业编程类(class),所以任何帮助将不胜感激!!

最佳答案

您应该向 scanf 传递一个指向它应该存储输入的位置的指针,而不是已经存在的值。 scanf("%i", &nums[x]);

关于c - 我正在尝试用用户输入填充一个数组,我已经提示用户现在我正在尝试编写一个 fillArray 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9475470/

相关文章:

c++ - 用于 C/C++ 的可移植且简单的 unicode 字符串库?

C 编译警告 : passing argument 1 of ‘insert’ from incompatible pointer type [enabled by default]

c++ - 带括号和不带括号定义的宏 - 重新定义错误

c - 返回VLA和使用情况

c++ - 为什么 C/C++ 编码标准说我们不应该在函数中有多个返回值?

c++ - MSVC 中的 "interface"关键字是什么?

python - 通过 ctypes 从 Python 调用的 C 函数返回错误的值

c - 在 C 中为并行编程 openmp 和 mpi 分配内存的最佳方法

c - 字符串的名称是 char 指针吗?

c - 文件写入问题