dimanche 27 décembre 2015

Code suggestion needed on this revision

I'm posting question to this site because simply my teacher, whom I can ask about this, is not available for days or so and I don't have anyone aroudn to get help from.

Question : Finish the coding that would result in this numbers

2 3 5 (typed number on command prompt) 2 + 5 + 8 + 11 + 14 == 40

or

22 -2 7 (typed number on command prompt) 22 + 20 + 18 + 16 +14 + 12 + 10 == 112

#include <stdio.h>
int main(void)
    {
    int a, d, n;
    int i, s;
    scanf("_____", __, __, __);
    ___;
    for (i = 1; _____; i = i + 1) {
        _________________;
        printf("____ ", _________);
        _________________
        _________________
        _________________
        _________________
    }
    printf("%d\n", s);
    return 0;
}

Below is my answer

---------------My answer --------------

#include <stdio.h>
int main(void)
    {
    int a, d, n;
    int i, s;
    scanf("%d%d%d", &a, &d, &n);
    s = 0;
    for (i = 1; i <= n; i = i + 1) {
       if (i != n)
           printf("%d + ", a + (i - 1)*d);
       if (i == n)
           printf("%d", a + (i - 1)*d);
       s = (a + a + (i - 1)*d)*i / 2;
    }
    printf(" == %d\n", s);
    return 0;
}

When I submitted my answer to the teacher, he said that the error was "source code format not identical to each other." I couldn't ask further because he was busy and he had to leave for something important. I was wondering what was problem with my answer. Maybe because last print statement includes "== %d\n" instead of "%d\n"? and my statements inside "for" statement total up to 5 which are supposed to be six, as suggested on the question? My codes illustrated right numbers and results but I guess he wants some different coding...

Help please...

Aucun commentaire:

Enregistrer un commentaire