ASSIGNMENT

A

azom47

COULD YOU PLEASE SOLVE THE FOLLOWING QUESTION.

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

Carl

COULD YOU PLEASE SOLVE THE FOLLOWING QUESTION.

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

Sure, but you won't learn anything from it.


/***********************/
#include <stdio.h>

int sum(int input);

int main(void)
{
int a;
a = sum(20);
printf("The sum is: %d\n", a);
return 0;
}

int sum(int input)
{
int temp = input / 2;
return temp * (temp + 1);
}

/***********************/
 
O

osmium

COULD YOU PLEASE SOLVE THE FOLLOWING QUESTION.

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

Look into the use of a for statement and the modulo operator, which is
specified in C by the '%' symbol.

Typing in all upper case is considered bad form on Usenet. This IS Usenet.
 
M

mark.bluemel

COULD YOU PLEASE SOLVE THE FOLLOWING QUESTION.

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

In keeping with tradition...

#include <stdio.h>

typedef int IOI;
#define I 0
#define O 1
#define O1O printf

IOI IO1(IOI I01) {
return I01?I01 + IO1(I01-O):0;
}

IOI main(void) {
IOI I0I = IO1(0xa);
O1O("%d\n",I0I << (I0I>I));
}
 
L

Lew Pitcher

COULD YOU PLEASE SOLVE THE FOLLOWING QUESTION.

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


OK, done.

Now, did you have a question about C?

<off-topic>
For what it's worth, you have apparently asked us to do your homework for
you. You were given this homework to help you understand the use of and
practice certain methods and facilities, and if we do your homework for
you, it defeats the purpose of the homework.

We are not a "homework help line", but if you post what you've done so far,
we can help you by making suggestions as to how to fix what you've done.

/I/ will go further than that, though. I will tell you how to write the
program you've asked us about.

First off, don't even think of C.

Get a sheet of paper, and write down all the even numbers between 0 and 20.
Add them up, and write down the total.
Study this for a moment, remembering exactly how you worked out the
solution. How did you pick "even numbers"? How did you know where to start?
How did you know where to stop?

Now, on another sheet of paper, write down the steps that you took. Start
with how you decided where to start. Include how you decided to pick even
numbers, how you added the numbers together, and end with how you decided
to stop.

Once you can /clearly/ explain how to solve the problem /on paper/, you can
then look for programming language tools that you can use to follow that
explanation. Use these tools, and write a short C program that does what
you did manually. Then try it, and see if it gets the same answer you got
when you did it manually. If it doesn't work, show us what you did, and we
can help get you back on track.

A suggestion/hint for this stage: the C "for()" construct has four parts:
1) a part that tells it where to start,
2) a part that tells it how much to change by,
3) a part that tells it where to stop, and
4) a part that tells it what to do at each step.
That sounds a lot like the things I asked you to remember from before,
doesn't it?

</off-topic>


--
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. ------
 
D

Dik T. Winter

> 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 sum;

sum = 0+2+4+6+8+10+12+14+16+18+20;
}
 
M

Mark Bluemel

Lew said:
....
We are not a "homework help line", but if you post what you've done so far,
we can help you by making suggestions as to how to fix what you've done.

/I/ will go further than that, though. I will tell you how to write the
program you've asked us about.

First off, don't even think of C.
Think about mathematics. What have you learnt about sums of series (have
you learnt anything about them)? Can you apply this to the problem in hand?

Hint: If it were all the even numbers from 2 to 10,000,000 you wouldn't
even consider doing individual addition would you?
 
C

Carl

In keeping with tradition...

#include <stdio.h>

typedef int IOI;
#define I 0
#define O 1
#define O1O printf

IOI IO1(IOI I01) {
return I01?I01 + IO1(I01-O):0;
}

IOI main(void) {
IOI I0I = IO1(0xa);
O1O("%d\n",I0I << (I0I>I));
}


The thought had just occurred to me that he probably needs to use a for
loop in this assignment. Sorry about the confusion. Here is the new
fragment:

/************************/
#include <stdio.h>

int main(void)
{
unsigned char nti[2] = {(~0["\355"]^7)};
for(*(nti+1)&=0;--(*nti);nti[1]+= (nti[0]&1)?0:nti[0]);
printf("%d\n", 1[nti]);
}
/************************/
 
L

Lew Pitcher

Think about mathematics. What have you learnt about sums of series (have
you learnt anything about them)? Can you apply this to the problem in
hand?

Hint: If it were all the even numbers from 2 to 10,000,000 you wouldn't
even consider doing individual addition would you?

Of course.

OTOH, an obvious simple homework problem, and an OP who cant solve it,
points to a "beginners" course in C. Homework is given for a reason, and a
C-language homework problem of such a simple nature isn't likely meant to
teach how to apply lateral thinking to mathematical problems. Instead, it
is likely to be re-enforcing the basics of the C language.

So, a lateral reinterpretation of the mathematics could be the solution to
the problem as stated. But then again, so could a simple for() loop which
constructs the solution using simple arithmetic. And the for() loop is more
likely to be what the homework is all about, don't you think?


--
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. ------
 
M

Mark Bluemel

Carl said:
Sure, but you won't learn anything from it.


/***********************/
#include <stdio.h>

int sum(int input);

int main(void)
{
int a;
a = sum(20);
printf("The sum is: %d\n", a);
return 0;
}

int sum(int input)
{
int temp = input / 2;
return temp * (temp + 1);
}

/***********************/

Nice...
 
M

Mark Bluemel

Lew said:
Of course.

OTOH, an obvious simple homework problem, and an OP who cant solve it,
points to a "beginners" course in C. Homework is given for a reason, and a
C-language homework problem of such a simple nature isn't likely meant to
teach how to apply lateral thinking to mathematical problems. Instead, it
is likely to be re-enforcing the basics of the C language.

So, a lateral reinterpretation of the mathematics could be the solution to
the problem as stated. But then again, so could a simple for() loop which
constructs the solution using simple arithmetic. And the for() loop is more
likely to be what the homework is all about, don't you think?

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

Depending on his tutor, he could get bonus points for finding a "better"
solution.
 
M

Mark Bluemel

Carl said:
The thought had just occurred to me that he probably needs to use a for
loop in this assignment. Sorry about the confusion. Here is the new
fragment:

/************************/
#include <stdio.h>

int main(void)
{
unsigned char nti[2] = {(~0["\355"]^7)};
for(*(nti+1)&=0;--(*nti);nti[1]+= (nti[0]&1)?0:nti[0]);
printf("%d\n", 1[nti]);
}
/************************/

You, sir, have a sick mind :)
 
C

Carl

Mark said:
Carl said:
The thought had just occurred to me that he probably needs to use a
for loop in this assignment. Sorry about the confusion. Here is the
new fragment:

/************************/
#include <stdio.h>

int main(void)
{
unsigned char nti[2] = {(~0["\355"]^7)};
for(*(nti+1)&=0;--(*nti);nti[1]+= (nti[0]&1)?0:nti[0]);
printf("%d\n", 1[nti]);
}
/************************/

You, sir, have a sick mind :)

http://www.cise.ufl.edu/~manuel/obfuscate/pi.c

My personal favorite.
 
K

Kenny McCormack

Mark Bluemel said:
Hint: If it were all the even numbers from 2 to 10,000,000 you wouldn't
even consider doing individual addition would you?

Of course, on modern hardware, this could easily be done in a loop, in
less than a second, so why worry?

P.S. I just did it in GAWK (no time to write the C program) and it took
about 9 seconds, still not too bad.
 
C

Chris Dollin

COULD YOU PLEASE SOLVE THE FOLLOWING QUESTION.

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

In the spirit of all the other pseudo-answers:

int main(void)
{ long sum = 0, even_number = 0
; while (even_number < 323081) sum += even_number, even_number += 2
; return 0
; }
 
G

Guest

COULD YOU PLEASE SOLVE THE FOLLOWING QUESTION.

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

rather than give you the C here is a solution is "pseudo-code".
Simply translate into C

(define (sum-even n)
(if (zero? n)
0
(+ (if (even? n) n 0)
(sum-even (- n 1)))))

(sum-even 20)
 
D

dfighter

Kenny said:
Of course, on modern hardware, this could easily be done in a loop, in
less than a second, so why worry?

P.S. I just did it in GAWK (no time to write the C program) and it took
about 9 seconds, still not too bad.
Kenny, why would you bother to do something wrong when you know how to
do it right?
FYI more powerful HW doesn't mean that you can or need to write slow
code. It means that with the same fast code you can calculate even faster.

dfighter
 
K

Keith Thompson

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 (*p(int n))(void);

int main(void)
{
int s = 0;
int t = !++s;
int i;
for (i = 2; i < 13; i ++) {
if (p(i)) {
if (++t&1) {
s *= i;
}
}
}
printf("%d\n", s);
return 0;
}

int (*p(int n))(void)
{
int i;
for (i = 2; i < n; i ++) {
if (n % i) {
continue;
}
return +'/'/(+'/')+(-'/')/'/';
}
return main;
}

Generalizing this solution is left as an exercise.
 
W

Willem

Richard wrote:
) It is. But what about
)
) "return 2468101214161820;"
)
) (in true c.l.c fashion),

You forgot the " % 123 " fudge factor:

return 2468101214161820 % 123;


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top