如何找到给出三个分数的最高分?

时间:2015-09-06 10:37:23

标签: c

#include <stdio.h> 
#include<conio.h>
int main () 
{ 
        printf("Enter the Physics ,Chemistry and Maths Marks"); 
        int mark[3]= {40,50,10};
        int s[3];
        int i; 
        int sum = 0, highest = 0; 
        clrscr(); 
        for (i = 0; i < ; i++) 
             { 
                sum += mark[i]; 
                if (mark[i] > highest) 
                        highest = mark[i]; 
             } 

        printf("The Highest Mark is %d: \n", highest); 
        getch(); 

    return 0; 

} 

它工作正常,我需要动态输入并获得输出

怎么做?

Enter the Marks : 30 20 10
output: 30

2 个答案:

答案 0 :(得分:0)

此:

int mark[3];
mark[3] = scanf("%d",mark[3]);
由于几个原因,

是错误的。您似乎不知道如何正确使用scanf。它应该是

int mark[3];
scanf("%d", &mark[0]);
scanf("%d", &mark[1]);
scanf("%d", &mark[2]); /* Get each number from stdin and store it in the address of the variable given */

或更好

int mark[3];
scanf("%d %d %d", &mark[0], &mark[1], &mark[2]);

甚至更好

int mark[3], i;
for(i = 0; i < 3; i++) /* Loop 3 times */
{
    scanf("%d", &mark[i]);
}

答案 1 :(得分:0)

您可以使用scanf()从输入中获取数字,然后使用fmax()找到最高数字:

#include <stdio.h>
#include <math.h>

int main()
{
    int mark[3] = {0, 0, 0};
    int highest = 0;

    printf("Enter the Marks\n");
    for (int i = 0; i < 3; i++) {
        printf("%d: ", i + 1);
        scanf("%d", &mark[i]);
        highest = fmax(highest, mark[i]);
    }

    printf("The Highest Mark is %d: \n", highest);
}