c - 协助查找数组的高点和低点并将数组设置为零

标签 c

我遇到的问题是,即使我在 main 中将名为 exam[] 的数组设置为零,但当我调用函数 displayAllExam() 时,只有一些设置为零,而其他设置为可能代表 ram 中的空格的随机数.这弄乱了我的 displayExamAverage() 函数,因为当我运行代码时,我可以向数组添加值,但如果我不添加 50 个值,那么那里就会有随机数。我也真的坚持 displayHighLow 函数,该函数用于查找数组的最高值和最低值

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define pause system("pause")
#define cls system("cls")
#define flush fflush(stdin)
#define SIZE 50

//Lets Prototype
int getChoice();
void displayMenu();
void addScores(int a[],int *c);
void displayAllExam(int a[]);
void displayExamAverage(int a[], int *c);
void displayHighLow(int a[], int *c);


main(){
    int exams[SIZE] = {0}, counter = 0;
    char choice;
    do {
        choice = getChoice();
        switch(choice){
        case 1:
            addScores(&exams[SIZE],&counter);
            break;
        case 2:
            displayExamAverage(&exams[SIZE],&counter);
            break;
        case 3:
            displayHighLow(&exams[SIZE], &counter);
            break;
        case 4:
            displayAllExam(&exams[SIZE]);
            break;
        }

    }while (choice != 5);
}//end main

 int getChoice(){
    int result = 0;
    do{
        displayMenu();
        scanf_s("%i", &result);
        if(result < 1 || result >5 ){
            printf("Invalid Selection, please enter a number between 1 and 5\n");
        pause;}
    } while(result < 1 || result > 5);
    return result;
}//end getChoice

void displayMenu() {
    cls;
    printf("MAIN MENU\n\n");
    printf("1: add an exam score\n");
    printf("2.Display exam average\n");
    printf("3.Display High and Low score\n");
    printf("4.Display all scores\n");
    printf("5.Quit\n\n");
    printf("Enter your choice: \n\n");
    return;
}//end display menu

void addScores(int a[SIZE],int *c){
    int i=0;
    printf("please enter your score..");
    scanf_s("%i", &a[i]);
}//end add scores

void displayExamAverage(int a[],int *c){
    float average, total = 0.0;
    int i;
    for(i=0; i < *c; i++)
        total = total + a[i];
     average = total/ *c;
     printf("%f",average);
     pause;
}//end displayExamAverage

void displayHighLow(int a[],int *c){
    int high= a[0], low= a[0];
    int i,x,y;
    for(i=0; i < *c; i++){
        if(a[*c]> a[0])
            high = a[i];
    }
    for(i=0; i < *c; i++){
            if(a[*c]<a[0])
            low = a[i];
    }
    printf("The low is %i", low);
    printf("The high is %i", high);
    pause;

}//end DisplayHighLow

void displayAllExam(int a[SIZE]){
    int i;
    for(i=0;i < SIZE; i++)
        printf("the %i score is %i\n",i+1,a[i]);
    pause;
}//end displayAllExam

最佳答案

您传递的指针指向数组末尾之后的位置。这是一个很大的禁忌,如果你打算读取或写入它:

displayAllExam(&exams[SIZE]);

相反,只需像这样传递它:

displayAllExam(exams);

所有其他电话也是如此。

关于c - 协助查找数组的高点和低点并将数组设置为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19672866/

相关文章:

c - 什么进入 __init_array?

c - 从函数返回的奇怪 int

c - 为什么 C 程序第一次运行时,运行速度慢了 10 倍

c - OCIDate 在进入 Oracle 的过程中被破坏

c - 如何在C中从这种类型的字符串中获取数字

c - 响应 isatty(3) 的文件

c - C中的strtok函数错误

C 的 scanf 函数。如果使用宽度修饰符,例如 : %5d. 如何防止用户输入的任何多余数字被读入下一个 %d 说明符?

c - ELF 二进制分析静态与动态。汇编代码如何|指令内存映射变化?

c - 读取代码并计算其中的行数的程序,不包括注释和空行