ASSIGNMENT

P

Phil Carmody

COULD YOU PLEASE SOLVE THE FOLLOWING QUESTION.

WRITE A PROGRAM IN C THAT WILL CALCULATE THE SUM OF EVEN NUMBERS FROM
0 TO 20?

Untested, may not even compile, but if so, getting it to run
will be good exercise for you:

I've tried to make it as readable as possible, unlike what
you'll get from some of the downright rotters in this group,
and have even helpfully commented it.

-- 8< --
#include <stdio.h>

int main(void)
{
int i=0, s=0;
s+=i; i+=2; /* s is 0, i is 2 */
s+=i; i+=2; /* s is 2, i is 4 */
s+=i; i+=2; /* s is 6, i is 6 */
s+=i; i+=2; /* s is 12, i is 8 */
s+=i; i+=2; /* s is 20, i is 6 */
/* We've now summed the even numbers from 0 to twenty, so stop */
printf("%d\n", s);
return 0;
}
-- 8< --


Hope that helps. Good luck with your assignment - I suggest changing
the variable names so that your instructor doesn't find this post if
he goes searching.

Cheers,
Phil
 
K

Kaz Kylheku

What pretentious nonsense for "pseudo code". Makes hardly any sense to
me without real thought and scrutiny and I've been professionally

So you want sense without thought?
programming various languages for almost 30 years.

And yet you can't grok a trivial little Scheme function which follows directly
from the mathematics of defining this function recursively. Oops!
 
K

Keith Thompson

Kenneth Brody said:
Richard said:
No he didn't. The question was line #1 of the body. What is it with
people trying to put people down here? Has the IQ plummeted in people's
haste to be "reg-like"?

If line #1 is a question, why does it not end in a question mark?
[...]

Given that the OP was "do my homework for me, please", without even a
simple "please" (actually, it was "DO MY HOMEWORK FOR ME!"),
[...]

Actually, there was a simple "please", or rather a simple "PLEASE".

Nevertheless, I agree that politeness in response to such a "request"
is not warranted. (Personally, I tend to think that even those who
gave an outline of a solution, even without providing any code, were
being more helpful than they should, but YMMV.)

With luck, what the OP will learn from this will be more valuable than
an actual solution would be.
 
C

Carl

Phil said:
Untested, may not even compile, but if so, getting it to run
will be good exercise for you:

I've tried to make it as readable as possible, unlike what
you'll get from some of the downright rotters in this group,
and have even helpfully commented it.

-- 8< --
#include <stdio.h>

int main(void)
{
int i=0, s=0;
s+=i; i+=2; /* s is 0, i is 2 */
s+=i; i+=2; /* s is 2, i is 4 */
s+=i; i+=2; /* s is 6, i is 6 */
s+=i; i+=2; /* s is 12, i is 8 */
s+=i; i+=2; /* s is 20, i is 6 */
/* We've now summed the even numbers from 0 to twenty, so stop */
printf("%d\n", s);
return 0;
}
-- 8< --


Hope that helps. Good luck with your assignment - I suggest changing
the variable names so that your instructor doesn't find this post if
he goes searching.

Cheers,
Phil
If his prof did see your post, he would still notice the same variables.
Also, it should be clear that this is a tutorial of a for loop. Why
don't you leave the unrolling to the compiler?
 
K

Kenny McCormack

No he didn't. The question was line #1 of the body. What is it with
people trying to put people down here? Has the IQ plummeted in people's
haste to be "reg-like"?

I think you're either with them or you're against them (them = the regs).
And it is pretty clear what happens to people who make it known that
they are against them.
"Avoid hyperbole at all costs, its the most destructive argument on
the planet" - Mark McIntyre in comp.lang.c

I still love this quote. It is simply incredible to realize that it is
entirely genuine (no hyperbole added on your part) and posted with, as
far as anyone can tell, an entirely straight face (by Mr. McInTired).
 
S

Sjouke Burry

COULD YOU PLEASE SOLVE THE FOLLOWING QUESTION.

WRITE A PROGRAM IN C THAT WILL CALCULATE THE SUM OF EVEN NUMBERS FROM
0 TO 20?
Whats the name and email address of your teacher?
We will mail the solution there.
 
L

Lew Pitcher

Fair comment. Could also be done as an exercise in recursive functions.

Yes. But if the OP can't solve the problem with a for() loop, then
he's /really/ in no shape to understand recursion ;-)
Depending on his tutor, he could get bonus points for finding a "better"
solution.

IIRC, there's a precedent for that.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
O

osmium

Keith Thompson said:
With luck, what the OP will learn from this will be more valuable than
an actual solution would be.

For a computer science course I have my doubts; if it happens to be in
abnormal psychology, I agree.
 
U

user923005

If line #1 is a question, why does it not end in a question mark?

Since the rest of the post is not a question, despite line #1 saying that a
question was to follow, why does that part end with a question mark?

Given that the OP was "do my homework for me, please", without even a simple
"please" (actually, it was "DO MY HOMEWORK FOR ME!"), why do you feel he
needs any respect from anyone here?  Note another recent thread, "Working
with char", which was also an assignment question.  Here, however, the OP
said "here is what I've done so far, can you please help me with it", and
real help was given.

Finally, if line #1 really was the question, then the answer is nothing more
than a simple "yes".

I swear, some people are *SO* unhelpful. Let's give the poor O.P. a
break.
Here is my {optimal} solution:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
puts("THE SUM OF ALL EVEN NUMBERS FROM 2 TO 20 INCLUSIVE IS
110.");
return EXIT_SUCCESS;
}

;-)
I screamed, because he screamed. I figure he likes screaming.

Of course, there is the straight-forward method:

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

int main(void)
{
int i;
int sum = 0;
for (i = 1; i <= 20; i+=2)
sum += i;
printf("SUM ALSO EQUALS %.0f\n",
exp(log((double)(((20 * 20 + 20) / 2) - sum))));
return EXIT_SUCCESS;
}

Not only have we found an alternate way to calculate the sum
effectively, but we have also verified that math works (to the great
relief of mathematicians world-wide).
 
U

user923005

Carl said:
Phil said:
(e-mail address removed) writes:
COULD YOU PLEASE SOLVE THE FOLLOWING QUESTION.
WRITE  A PROGRAM IN C THAT WILL CALCULATE THE SUM OF EVEN NUMBERS FROM
0 TO 20? [...]
int main(void)
{
        int i=0, s=0;
        s+=i; i+=2;     /* s is 0, i is 2 */
        s+=i; i+=2;     /* s is 2, i is 4 */
        s+=i; i+=2;     /* s is 6, i is 6 */
        s+=i; i+=2;     /* s is 12, i is 8 */
        s+=i; i+=2;     /* s is 20, i is 6 */
        /* We've now summed the even numbers from 0 to twenty, so stop */
        printf("%d\n", s);
        return 0;
}
[...]
Also, it should be clear that this is a tutorial of a for loop.  Why
don't you leave the unrolling to the compiler?

It should also be clear that this tutorial is assigned so that the student
in the class can learn from implementing it. Most of the responders have
respected this, and avoided posting the kind of solution appropriate, so
as to avoid robbing the student of the learning experience.

It also isn't obvious that the instructor did want a for() loop. If
it were truly a requirement, the instructor would have stated it.

Probably, a while (cond) {} construct or do {} while (cond); construct
would have been just as nice. As someone who once taught the C
language in college, I can say that I very rarely told the students
exactly what construct to use, and in those few cases it was usually
to show contrasts like:

1. Perform the following task /* Task defined here */ with a for()
loop.
2. Perform the same task with a while (condition) {} loop.
3. Perform the same task with a do {} while (condition) loop.
4. Which way did you like better and why?

I have found that when you try to be restrictive, it limits the
creativity of the students, who will often find a better solution than
the one that you imagined.

I did have a few unreasonable restrictions (such as no use of goto in
the program whatsoever -- not even in a nested loop where an inner
break was needed). I found this condition necessary, because Olympic
College in Bremerton Washington is by a Navy base and more than half
of the students were in the Navy. Nothing wrong with that per se, but
a lot of them were Fortran and COBOL programmers from way, way back in
the day and they simply were in love with the goto.
 
U

user923005

By the way, Azo, the solution I gave you is a serious one. If you
have any more assignments to do, just let me know on this newsgroup,
and I'll help you as best I can. I consider opinions about learning
theory, plagiarism, ethics, etc., off-topic for this newsgroup, so
you can count on me to address the C content of your posts. Suffice
it to say, I have my own views about learning and education, views
I picked up from a number of 20th-century thinkers who were experts
in the field and whose opinions I respect. I'd suggest making sure
you understand the solutions I give you, and perhaps after reading
them, try your hand at coding the solutions without looking at mine.
All the best with your studies, buddy.

Do you really imagine that doing someone's assignment for them is a
benefit?

I literally cannot imagine anything more damaging. You are robbing
that student of his/her education. The teacher already knows the
answer to the question and they simply want the student to reason
through the process and find out what bits are hard and what bits are
easy. It is likely that the student does not know how to complete the
task. But if you simply perform the task for them, they are no better
off than before they started.

Besides the fact that they are avoiding the learning of the necessary
skill (seeing someone else do it does not teach the skill) they are
also doing something that is dishonest and will get them expelled from
most universities if caught. It is not improbable that becoming a
liar and a cheat may carry on forward in life as well if they practice
at it and are encouraged to do it.

Consider also the money that either they or their parents are spending
on an education that is simply being flushed down the toilet. When
you act in an unhelpful manner it is really robbing *someone* of money
that they are spending to learn.

I think you are defining yourself in a bad way (being a whinging twit
at times pales in comparison -- because we are all whinging twits from
time to time -- just some more than others). But perhaps only to some
people. Others may consider you a hero, though their opinion may
change down the road.

IMO-YMMV
 
C

CBFalconer

COULD YOU PLEASE SOLVE THE FOLLOWING QUESTION.

WRITE A PROGRAM IN C THAT WILL CALCULATE THE SUM OF EVEN NUMBERS
FROM 0 TO 20?

#include <stdio.h>

int main(void) {
int i, sum;

for (i = sum = 0; i < 20; i++)
if (!(i & 1)) sum += i;
printf ("Sum = %d\n", sum);
return 0;
}

but you can't enter that in your machine with your broken shift
key.
 
U

user923005

Are you surprised that someone can come up with a different view than you
have, or do you assume that any differing views must not have been as
well-thought as yours? In matters like this, it's always an issue of what
the long-term effect is, which is hard to predict. Now if you were going
by actual data on the long-term effect this has had in specific cases,
it'd be hard to argue against.

And yet people would argue anyway. It would seem fairly obvious that
student exercises are meant to be worked out by the students and also
that there is value in student exercises. But it is certainly
possible that the entire world's educational system is done completely
wrong and that the right way to solve problems is to cheat and avoid
practice. Somehow I doubt that this will work well in the long run
because I personally know people who have tried this method and it did
not work out well for them. Of course, a few experiences and what I
consider common sense do not amount to a clinical and scientific
study. I will leave that to someone else to conduct.
Nobody forced the student to post here. He most likely went somewhere else
and found the answer anyway.

I see no fault in the student posting here. I see no harm, either, in
redirection answers, hints as to a procedure, gag answers and the
like. I do see harm in giving the student the answer such that they
are not required to think on their own or try things by themselves.

If the student were to have posted differently (e.g. after describing
the assignment asked something like 'Where do I start?') I suspect
that they would have gotten better help. When problems are posed in a
way that *looks* like an attempt to avoid learning, the answers given
are often dripping with derision. Probably, it would have been a lot
better if everyone had assumed that the student was simply making an
honest appeal for help and not looking for cheating assistance.
Depends on the student's goal.

If cheating is the answer, then the goal is a bad one.
[...]> Consider also the money that either they or their parents are spending
on an education that is simply being flushed down the toilet.  When
you act in an unhelpful manner it is really robbing *someone* of money
that they are spending to learn.

[...]

Who again is wasting the money? Not the student, but people on a Usenet
newsgroup?

Do you know what tuition costs at even a public university? Add in
books, room and board and we are talking serious money. The loss does
not stop there. This person may eventually come to work for you (or
may even become your boss). This will cost *you* money eventually if
they have avoided learning.
Come on! The main reason I give sarcastic replies to such posts
is that I find them somewhat rude, and don't think very highly of people
wasting the group's time with requests for spoon-feeding.

I have no problem with requests for spoon feeding. At some point
everyone is a complete neophyte at everything. We add skills over
time. Asking questions is a good way to learn, and neophyte questions
are what we get out of neophytes. To expect otherwise is not
realistic. It's not absurd to imagine that the O.P. was simply
frustrated at not knowing where to start. If the question had been
phrased better {IOW it does not look like a rephrasal of "please help
me cheat"}, I suspect that the student would receive genuine help.
When it comes to homework assignments, I think that we can see some
excellent guidelines here:
http://home.att.net/~jackklein/ctips01.html
If students were to follow these guidelines, then I suspect that
everyone would benefit. Students will not always follow those
guidelines, and so they are going to get a mix of good answers, bad
answers, gag answers and etc.
The best
exchanges with students here are those where the student tells us his
goal, how he is currently trying (but failing) to achieve it, and then
pays attention to replies and gives feedback as to what insights that
gives him.

I agree.
These exchanges are then of value to other readers facing
similar problems, or who simply enjoy reading such fruitful exchanges.

This is also true. Certainly, well phrased questions that demonstrate
willingness to learn on the part of the student will gather the best
responses.
 
L

Lew Pitcher

#include <stdio.h>

int main(void) {
int i, sum;

for (i = sum = 0; i < 20; i++)
if (!(i & 1)) sum += i;
printf ("Sum = %d\n", sum);
return 0;
}

First observation: this only counts even integers that range from 0 to 19.
Was this intentional?

2nd observation: this seems a bit obfuscated to me (given that the for()
loop can take /any/ expression as the third expression). A
clearer "addition" solution might be

#include <stdio.h>
int main(void)
{
int i, sum;

for (i = sum = 0; i <= 20; i = i + 2) /* even numbers from 0 to 20 */
sum = sum + i;
/* yes, I know about +=. But, does the OP? */

printf("Sum = %d\n",sum);
return 0;
}


--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
U

user923005

user923005 said:




I don't see the problem.

If somebody here wishes to take on another person's workload, that's
their business. If they wish that person to become dependent on
them, that's their business. If they wish that person to be
hassling them every five minutes with requests for modifications,
that's their business. And if they take on, say, a dozen such OPs,
I doubt very much whether they'll have any more time for posting
anything to Usenet other than their spoonfeeding articles.

So I don't see the problem - there doesn't seem to be a downside.

5 years from now the person who failed to learn gets hired by your
company or even becomes your boss.
Can you see a downside now? (Dilbert comics spring to mind -- they
are not always completely removed from reality).

Imagine that this person goes to work somewhere else. Their
productivity is low. This lowers the productivity of the company that
they work for. Now, one person with low productivity will have little
impact on an economy. But millions of them will.

I do realize that your response is very much tongue-in-cheek. I think
it is also possible that the O.P. was not simply hoping to cheat.
They may have really wanted *actual* help, but simply phrased the
question in the worst possible manner. It may even be a complete
troll. We'll probably never find out. But I do recall (in the past)
where students have -- with a bit of prodding -- made substantial
progress even after starting off on the wrong foot.

Let's be honest also -- we have *all* done something lazy some time in
our lives. It would have been better to think things out carefully or
to put more effort into a problem that we faced. And we made a bad
choice (the lazy choice is almost always a bad one). If those choices
received plenty of encouragement, we could possibly have received some
damage from it.

In most places you have to be pretty smart even to get into college.
So the capabiltiy to solve these sorts of problems is probably
present, somewhere lurking under the skin. If we can coax out that
ability out of those asking the simple questions then they will have
learned. I am one of those people who see something noble in
teaching. In fact, when I retire from "actual work" I want to teach a
class at a junior college, just because it's such a wonderful and fun
thing to do.

In addition, I suspect that the hapless harmers are not detained from
making annoying posts any more than if they were doing some other kind
of harmful posting. Bad posting comes from bad posters. In general
the only real harm is to neophytes anyway, since they are the ones
that do not understand why the posts are either wrong or harmful.

IMO-YMMV
 
R

Richard Tobin

COULD YOU PLEASE SOLVE THE FOLLOWING QUESTION.

WRITE A PROGRAM IN C THAT WILL CALCULATE THE SUM OF EVEN NUMBERS FROM
0 TO 20?

#include <stdio.h>

int total(int n)
{
return n < 0 ? 0 : n + total(n-2);
}

int main(void)
{
printf("%d\n", total(20));

return 0;
}

Unfortunately this program does not conform to the C standard.

-- Richard
 
U

user923005

#include <stdio.h>

int total(int n)
{
    return n < 0 ? 0 : n + total(n-2);

}

int main(void)
{
    printf("%d\n", total(20));

    return 0;

}

Unfortunately this program does not conform to the C standard.

OK. I puzzled and puzzled and I still don't see anything wrong with
it.
In what way is the program non-conforming?
 
U

user923005

First observation: this only counts even integers that range from 0 to 19..
Was this intentional?

Peter Seebach had a knack to throw in mistakes that were *really*
funny (the tone of which can easily be found in the IAQ[1]). I sure
miss his posts.
2nd observation: this seems a bit obfuscated to me (given that the for()
loop can take /any/ expression as the third expression). A
clearer "addition" solution might be

  #include <stdio.h>
  int main(void)
  {
    int i, sum;

    for (i = sum = 0; i <= 20; i = i + 2) /* even numbers from 0 to 20 */
      sum = sum + i;
    /* yes, I know about +=. But, does the OP? */

    printf("Sum = %d\n",sum);
    return 0;
  }
Yes, but where's the joke?
When it comes to a bare request for homework answers, I see the
following sensible replies:
1. Flames
2. Gag answers
3. Requests for clarification
4. Nudges in the right direction.

and the following insensible replies:
1. Correct and accurate completed assignements.

IMO-YMMV


[1] http://www.seebs.net/faqs/c-iaq.html
 
K

Kenny McCormack

Han from China said:
Dan, I've read all your posts in this thread, and I understand your
view and respect your right to hold it. I used to have a similar
view, but then I became largely disillusioned with traditional
education. Moreover, every new poster here has a back story about
which we can make only guesses. There are an incalculable number
of variables at play, and there's no way to predict whether the
causal chain that's set into motion by supplying the OP with an
answer will lead to good or evil. I find it prudent to restrict
myself to answering posters' C questions and not bothering with
the unknowns.

Think about the following scenario:
1) Somebody's almost failing a course, and subsequently flunking
out of school, and they're getting desperate (and face it,
you'd have to be pretty desperate to be looking for help in
this loony place known as CLC).
2) Said person's parents are threatening to cut off funding if
said person flunks out. Also comments to the effect of
bringing shame on the family...
3) Said person looks for help on CLC and gets the usual
treatment, and subsequently kills himself.

You want that on your conscience?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,113
Latest member
Vinay KumarNevatia
Top