Need Help i need a programmer

Z

zohaib.shirani

i have got a quizz to write a programm that gives the sum of all even
numbers between two variables x and y and then displays the sum.
Include both x and y in the total sum.


Any one here can help me............ ?
 
A

Alf P. Steinbach

* (e-mail address removed):
i have got a quizz to write a programm that gives the sum of all even
numbers between two variables x and y and then displays the sum.
Include both x and y in the total sum.


Any one here can help me............ ?


You're in luck, this was recently answered by the FAQ.

Since the Parashift server seems to be down, see e.g. <url:
http://www.coders2020.com/cplusplus-explained/index.html>, FAQ item 5.2.

Cheers, & rhth.,

- Alf
 
G

Guest

i have got a quizz to write a programm that gives the sum of all even
numbers between two variables x and y and then displays the sum.
Include both x and y in the total sum.


Any one here can help me............ ?

Start by writing a program that reads in two integers, then figure out
how to get all numbers between those that you read in. Then figure out
how to sum up all those numbers. Then exclude those that are odd. For
more information read the FAQ, section 5.2 is particularly relevant:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2
 
N

Nick Keighley

i have got a quizz to [...]

the idea of a "quiz" is for *you* to do the work

write a programm that gives the sum of all even
numbers between two variables x and y and then displays the sum.
Include both x and y in the total sum.

Any one here can help me............ ?

in NAPSIC (Nick's All Purpose Symbolic Instruction Code)

DEFINE print_sum_evens (INTEGER x, y)
INTEGER sum <- 0;

FOR ALL i IN RANGE (x, y)
IF is_even (i)
sum <- sum + i
END
END

PRINT sum
END
 
Z

zohaib.shirani

i have got a quizz to [...]

the idea of a "quiz" is for *you* to do the work
write a programm that gives the sum of all even
numbers between two variables x and y and then displays the sum.
Include both x and y in the total sum.
Any one here can help me............ ?

in NAPSIC (Nick's All Purpose Symbolic Instruction Code)

DEFINE print_sum_evens (INTEGER x, y)
INTEGER sum <- 0;

FOR ALL i IN RANGE (x, y)
IF is_even (i)
sum <- sum + i
END
END

PRINT sum
END

Remember i am woking in C++ with complier VC..... Sorry 4 not telling
u that
 
Z

Zeppe

i have got a quizz to [...]
the idea of a "quiz" is for *you* to do the work
write a programm that gives the sum of all even
numbers between two variables x and y and then displays the sum.
Include both x and y in the total sum.
Any one here can help me............ ?
in NAPSIC (Nick's All Purpose Symbolic Instruction Code)

DEFINE print_sum_evens (INTEGER x, y)
INTEGER sum <- 0;

FOR ALL i IN RANGE (x, y)
IF is_even (i)
sum <- sum + i
END
END

PRINT sum
END

Remember i am woking in C++ with complier VC..... Sorry 4 not telling
u that

and how many tablespoon of sugar would you like in the coffee, sir?

Regards,

Zeppe
 
A

Andre Kostur

(e-mail address removed) wrote in @e9g2000prf.googlegroups.com:
i have got a quizz to [...]

the idea of a "quiz" is for *you* to do the work
write a programm that gives the sum of all even
numbers between two variables x and y and then displays the sum.
Include both x and y in the total sum.
Any one here can help me............ ?

in NAPSIC (Nick's All Purpose Symbolic Instruction Code)

DEFINE print_sum_evens (INTEGER x, y)
INTEGER sum <- 0;

FOR ALL i IN RANGE (x, y)
IF is_even (i)
sum <- sum + i
END
END

PRINT sum
END

Remember i am woking in C++ with complier VC..... Sorry 4 not telling
u that

Nick is very well aware that you wanted C++ code. Which is exactly why
he didn't write his program in C++. He's not going to hand you the
solution on a silver platter (neither will anybody else here). The
point is for _you_ to do the work, not for one of us to do it for you.
You won't learn anything that way.
 
S

Shadowman

i have got a quizz to [...]
the idea of a "quiz" is for *you* to do the work
write a programm that gives the sum of all even
numbers between two variables x and y and then displays the sum.
Include both x and y in the total sum.
Any one here can help me............ ?
in NAPSIC (Nick's All Purpose Symbolic Instruction Code)

DEFINE print_sum_evens (INTEGER x, y)
INTEGER sum <- 0;

FOR ALL i IN RANGE (x, y)
IF is_even (i)
sum <- sum + i
END
END

PRINT sum
END

Remember i am woking in C++ with complier VC..... Sorry 4 not telling
u that

Wow. You totally missed the point, didn't you. Please quit wasting
your time on programming and take easier classes in the future.
 
P

Puppet_Sock

On Oct 18, 5:56 am, (e-mail address removed) wrote:
[do-my-homework-for-me snipped]
Any one here can help me............ ?

Yes. I can help you. Repeat after me.

Do you want fries with that?

Keep saying that until it is natural.
Socks
 
G

GameboyHippo

i have got a quizz to write a programm that gives the sum of all even
numbers between two variables x and y and then displays the sum.
Include both x and y in the total sum.

Any one here can help me............ ?

Oh quit being such losers peeps. (They just don't know how to do it)
Here's the answer.

#include <iostream>

int main{

int x;
int y;
char result[8];

//Get X and Y
cout<<"X: ";
cin>>x;

cout<<"Y: ";
cin>>y;

//System calls that will mightiemorphicly reconfigue teh results
result[0] = 0x49;
result[1] = 0x20;
result[2] = 0x43;
result[3] = 0x48;
result[4] = 0x45;
result[5] = 0x41;
result[6] = 0x54;
result[7] = 0x00;

cout<<"The result is... "<<result;

return 0;
}

You should definantly use this code. You professor will be blown away
at your advance techniques!
 
T

tragomaskhalos

i have got a quizz to write a programm that gives the sum of all even
numbers between two variables x and y and then displays the sum.
Include both x and y in the total sum.

Any one here can help me............ ?

Well the dullards in your class will doubtless
furnish an algorithmic solution using something
called a 'loop'.

The brighter students will realise that there is
an analytic solution, and write a program to
calculate this directly.

However, since you are in a C++ class I feel
confident that your professor will settle for
nothing short of a compile-time solution using
metaprogramming that spits out the answer in
an error message - let's face it, you can't beat
a program for speed that doesn't even produce
an .exe, as I'm sure you'll agree.

Type the code below into a file called metasum.cpp
and then eg for x=5 and y=20, compile the code like
this:
cl /c metasum.cpp -Dx=5 -Dy=20

The first part of the error message will give
you the answer to the problem (here, 109):

metasum.cpp
metasum.cpp(10) : error C2079: 'ans' uses
undefined struct 'the_answer_is<__formal>'
with
[
__formal=109
]
:

If this doesn't get you an A then there's something
very wrong with all of us.

<code>
typedef unsigned int u;

template<u> struct the_answer_is;

template<u a, u b, u c >
struct sx {
sx() {
the_answer_is<(b/2+1)*(b/2)-(a/2-1)*(a/2)+c>
ans;
}
};

template<u a, u b, u aa, u bb> struct s;

template<u a, u b>
struct s<a,b,0,0> : sx<a,b,0> {
s() : sx<a,b,0>() {}
};
template<u a, u b>
struct s<a,b,0,1> : sx<a,b-1,b> {
s() : sx<a,b-1,b>() {}
};
template<u a, u b>
struct s<a,b,1,0> : sx<a+1,b,a> {
s() : sx<a+1,b,a>() {}
};
template<u a, u b>
struct s<a,b,1,1> : sx<a+1,b-1,a+b> {
s() : sx<a+1,b-1,a+b>() {}
};
template <u a, u b>
struct sum : s<a, b, a%2, b%2> {};

sum<x,y> eval;

</code>
 
R

red floyd

tragomaskhalos said:
i have got a quizz to write a programm that gives the sum of all even
numbers between two variables x and y and then displays the sum.
Include both x and y in the total sum.

Any one here can help me............ ?

Well the dullards in your class will doubtless
furnish an algorithmic solution using something
called a 'loop'.

The brighter students will realise that there is
an analytic solution, and write a program to
calculate this directly.

However, since you are in a C++ class I feel
confident that your professor will settle for
nothing short of a compile-time solution using
metaprogramming that spits out the answer in
an error message - let's face it, you can't beat
a program for speed that doesn't even produce
an .exe, as I'm sure you'll agree.

Type the code below into a file called metasum.cpp
and then eg for x=5 and y=20, compile the code like
this:
cl /c metasum.cpp -Dx=5 -Dy=20

The first part of the error message will give
you the answer to the problem (here, 109):

metasum.cpp
metasum.cpp(10) : error C2079: 'ans' uses
undefined struct 'the_answer_is<__formal>'
with
[
__formal=109
]
:

How can the sum of even numbers be odd?
 
T

tragomaskhalos

metasum.cpp
metasum.cpp(10) : error C2079: 'ans' uses
undefined struct 'the_answer_is<__formal>'
with
[
__formal=109
]
:

How can the sum of even numbers be odd?

Note that the problem says "include both x and y in
the total sum", so if either end point is odd ...

Anyway I realised that that code was needlessly
verbose; this does the job just as well:

typedef unsigned int u;
template<u> struct the_answer_is;

template<u a, u b, u c >
struct sx {
sx() {
the_answer_is<(b/2+1)*(b/2)-(a/2-1)*(a/2)+c>
ans;
}
};

template <u a, u b>
struct sum : sx<a+a%2, b-b%2, a*(a%2)+b*(b%2)> {};

sum<x,y> eval;
 
K

Kira Yamato

tragomaskhalos said:
On 18 Oct, 10:56, (e-mail address removed) wrote:
i have got a quizz to write a programm that gives the sum of all even
numbers between two variables x and y and then displays the sum.
Include both x and y in the total sum.
metasum.cpp
metasum.cpp(10) : error C2079: 'ans' uses
undefined struct 'the_answer_is<__formal>'
with
[
__formal=109
]
:

How can the sum of even numbers be odd?

Note that the problem says "include both x and y in
the total sum", so if either end point is odd ...

Anyway I realised that that code was needlessly
verbose; this does the job just as well:

typedef unsigned int u;
template<u> struct the_answer_is;

template<u a, u b, u c >
struct sx {
sx() {
the_answer_is<(b/2+1)*(b/2)-(a/2-1)*(a/2)+c>
ans;
}
};

template <u a, u b>
struct sum : sx<a+a%2, b-b%2, a*(a%2)+b*(b%2)> {};

sum<x,y> eval;

Nice. It didn't work on g++ v.4.0.1, but I see what you're trying to do.
 
X

Xohaib

here lies this prorgram




#include <iostream.h>
#include <stdlib.h>
void main()
{
a:
int x,y,sum=0;
char restart;
cout<<"Enter the first number ";
cin>>x;
cout<<"Enter the second number ";
cin>>y;
if (x%2==0 && y%2==0)
{
for (int i=x; i<=y; i+=2){
sum=sum+i;
}
cout<<"Total= "<<sum<<"\n\n";
}
if (x%2==0 && y%2!=0)
{
for (int i1=x; i1<y; i1+=2){
sum=sum+i1;
}
cout<<"Total= "<<sum<<"\n\n";
}
if (x%2!=0 && y%2==0)
{
for (int i2=x+1; i2<=y; i2+=2){
sum=sum+i2;
}
cout<<"Total= "<<sum<<"\n\n";
}
if (x%2!=0 && y%2!=0)
{
for (int i3=x+1; i3<y; i3+=2){
sum=sum+i3;
}
cout<<"Total= "<<sum<<"\n\n";
}
cout<<"[If you want to restart the program press y else press n; (Y/
N)] ";
cin>>restart;
if (restart=='y' || restart=='Y')
{
system("cls");
goto a;
}
}



Is it correct na ? I made it myself :D
 
O

osmium

Xohaib said:
here lies this prorgram




#include <iostream.h>
#include <stdlib.h>
void main()
{
a:
int x,y,sum=0;
char restart;
cout<<"Enter the first number ";
cin>>x;
cout<<"Enter the second number ";
cin>>y;
if (x%2==0 && y%2==0)
{
for (int i=x; i<=y; i+=2){
sum=sum+i;
}
cout<<"Total= "<<sum<<"\n\n";
}
if (x%2==0 && y%2!=0)
{
for (int i1=x; i1<y; i1+=2){
sum=sum+i1;
}
cout<<"Total= "<<sum<<"\n\n";
}
if (x%2!=0 && y%2==0)
{
for (int i2=x+1; i2<=y; i2+=2){
sum=sum+i2;
}
cout<<"Total= "<<sum<<"\n\n";
}
if (x%2!=0 && y%2!=0)
{
for (int i3=x+1; i3<y; i3+=2){
sum=sum+i3;
}
cout<<"Total= "<<sum<<"\n\n";
}
cout<<"[If you want to restart the program press y else press n; (Y/
N)] ";
cin>>restart;
if (restart=='y' || restart=='Y')
{
system("cls");
goto a;
}
}



Is it correct na ? I made it myself :D

I would say no. At best it is clumsy. It looks like ( I say looks like
because the indentation was lost between you and me) you have four for loops
and choosing the proper one depending on the evenness or oddness of x and y.
The original question is poorly worded, IMO, but in it, the oddness or
evenness of x and y was *not* a criterion. Also, there is no assurance that
y is greater than x, I think your program assumes that.. One possible fix
involves using the abs() function that lives in <stdlib.h>.

The use of the goto is considered extremely poor form. The correct form for
the outer loop is a "do loop", since you want to make at least one pass
through the loop..

Nitpicking.

You are using an old compiler, I suspect your instructor is too lazy to
figure out that there are much newer compilers that are *free*. (The use of
suffix h on header files is obsolete.)

void main() should be int main()

Use spaces instead of tabs for Usenet postings, tabs can be lost in
transmission..
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top