1

Hihi, everyone. Please take a look at my coding and help me fix two things.

First, my 'try again' part is supposed to go back to the original student number count instead of moving to a next student. I think I'm using the loop wrong, but not sure how to fix it. I tried putting the loop outside of my 'for (i=1;i<11;i++)', but it didn't work.

Second, my min and avg work fine, but not max. It keeps giving me a random number and I don't know why.

Thank you so much.

#include<stdio.h>

void main() {

int i,a[10],max=a[0],min=a[0],avg,sum=0;

    printf("please input student score one at a time.\n");

    for (i=1;i<11;i++) {

        printf("Student %d : ",i);
        scanf("%d",&a[i]);

            while (a[i] > 100 || a[i] < 0) {

                printf("Try again!\n");
                i-1//im trying to keep the same student count after 'try again'
                break;
            }

        if (max < a[i]) max = a[i];
        if (min > a[i]) min = a[i];
        sum+=a[i];

    }

avg=sum/10;

printf("\n***************final result ***************\n");
printf("                The maximum score is %d\n",max);//only my max is broken. min & avg works fine
printf("                The minimum score is %d\n",min);
printf("                The average score is %d",avg);

getch();

}
john lee
  • 23
  • 4
  • 3
    1) `max=a[0],min=a[0]` : `a[0]` is uninitialized. 2) `for (i=1;i<11;i++) {` --> `for (i=0;i<10;i++) {` C is **0** origin. – BLUEPIXY Jul 10 '17 at 04:49
  • 3
    `i-1` doesn't change the value of `i`. You need to assign it, `i = i-1` – Norsk Jul 10 '17 at 04:50
  • `for (i=1;i<11;i++)` & `scanf("%d",&a[i]);` will destroy your array – duong_dajgja Jul 10 '17 at 04:51
  • oh, I know what you mean now. I'm just taking out '=a[0]' from both. – john lee Jul 10 '17 at 04:52
  • initiate max variable with 0 `max = 0` – Hadi Farhadi Jul 10 '17 at 04:52
  • 1
    Don't modify `i` inside `for` loop. Try using `do - while` or `while` if you need so – duong_dajgja Jul 10 '17 at 04:55
  • `while (a[i] > 100 || a[i] < 0) { printf("Try again!\n"); i-1; break; }` --> `if (a[i] > 100 || a[i] < 0) { printf("Try again!\n"); --i; continue; }` – BLUEPIXY Jul 10 '17 at 04:59
  • Why `break`?? Just get rid of it, add a `scanf("%d",&a[i]);` and you don't have to worry about decrementing `i`. E.g. `while (a[i] > 100 || a[i] < 0) { printf("Try again!\n"); scanf("%d",&a[i]); }` Also [**See What should main() return in C and C++?**](http://stackoverflow.com/questions/204476/) – David C. Rankin Jul 10 '17 at 05:20
  • Better answer by placing an answer, instead of squeezing more or less incomplete/inaccurate stuff into a comment ... :/ – alk Jul 10 '17 at 05:32

4 Answers4

1

Pleas try this

#include <stdio.h>

int main() {

    int i = 0;
    int a[10];
    int max = -1;
    int min = 101;
    int avg = 0;
    int sum = 0;

    printf("please input student score one at a time.\n");

    while(i < 10) {
        printf("Student %d : ",i);
        scanf("%d", &a[i]);
        if(a[i] > 100 || a[i] < 0) {
            printf("Try again!\n");
            continue;
        }

        if (max < a[i]) max = a[i];
        if (min > a[i]) min = a[i];
        sum += a[i];

        i++;
    }

    avg = sum/10;

    printf("\n***************final result ***************\n");
    printf("                The maximum score is %d\n",max);//only my max is broken. min & avg works fine
    printf("                The minimum score is %d\n",min);
    printf("                The average score is %d",avg);

    return 0;
}
duong_dajgja
  • 4,196
  • 1
  • 38
  • 65
  • 2
    Code looks correct, but still, adding some words/comments on why you changed/added/remove code would make it a good answer finally. – alk Jul 10 '17 at 05:34
0

Looks like homework, so I will just provide hints to how you can solve your issue

  1. Instead of decrementing the counter, just place a printf and scanf in the while loop till it finally satisfies the conditions.
  2. You are getting a random number because you are assigning max and min to a[0] right after declaring the array a[10], and you have not initialized any values in the array. In C, the memory location where the computer assigns space for the array might not necessarily be empty. Thus, your max gets a random value in it from the start. This could happen to min too, just that in your case it happened to max. One way to solve this is to initialize the whole array. Another way is just to assign the integer 0 to max and the integer 100 to min, since it will never go below 0 or above 100.

Hope this helps, all the best with your problem.

Julian Chan
  • 446
  • 2
  • 8
0
#include<stdio.h>

void main() {

int i,a[10],max=a[0],min=a[0],avg,sum=0;

printf("please input student score one at a time.\n");

for (i=1;i<11;i++) {

    printf("Student %d : ",i);
    scanf("%d",&a[i]);

        if (a[i] > 100 || a[i] < 0) {

            printf("Try again!\n");
            i--;
            continue;
        }

    if (max < a[i]) max = a[i];
    if (min > a[i]) min = a[i];
    sum+=a[i];

}

avg=sum/10;

printf("\n***************final result ***************\n");
printf("                The maximum score is %d\n",max);
printf("                The minimum score is %d\n",min);
printf("                The average score is %d",avg);


}

test in this site

Hadi Farhadi
  • 1,773
  • 2
  • 16
  • 33
-3

Thank you everyone for all your help. My code looks better now. I took out '=[0]' from both min and max and changed 'while' to 'if' with --i. Please let me know if I need to change more.

#include<stdio.h>

void main() {

int i,a[10],max,min,avg,sum=0;

    printf("please input student score one at a time.\n");

    for (i=1;i<11;i++) {

        printf("Student %d : ",i);
        scanf("%d",&a[i]);

        if (a[i] > 100 || a[i] < 0) {

            printf("Try again!\n");
            --i;
            continue;
        }

        if (max < a[i]) max = a[i];
        if (min > a[i]) min = a[i];
        sum+=a[i];

    }

avg=sum/10;

printf("\n***************final result ***************\n");
printf("                The maximum score is %d\n",max);
printf("                The minimum score is %d\n",min);
printf("                The average score is %d",avg);

getch();

}
john lee
  • 23
  • 4
  • Well its good if you found your answer but be sure to give back to the ones who helped you. – Pushan Gupta Jul 10 '17 at 05:15
  • 1
    `min` and `max` are use (being read for comparison) uninitialised in the 1st iteration. Not good. Even more, doing so provoke the infamous Undefined Behaviour. – alk Jul 10 '17 at 05:36