Reputation: 45
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 around 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...
Any help would be appreciated
Upvotes: 0
Views: 71
Reputation: 2625
This is stupid. The idea of this is to solve the problem but not to match the formatting. The formatting is anyway is very stupid and the code itself is forcing one to write it badly. The design is too complicated for a simple problem which can solved like this below
#include <stdio.h>
int main() {
int start, interval, numbers;
scanf("%d%d%d", &start, &interval, &numbers);
int sum = start;
int finalSum = 0;
int i;
for(i = 1; i <= numbers; i++, sum = sum + interval)
finalSum += sum;
printf("finalSum = %d\n", finalSum);
return 0;
}
In short this is a weird question and only your weird professor can answer it and may be you can put some sense into him/her
Sorry if I have been disrespectful to you or your professor but I had to say this
Upvotes: 1