473,322 Members | 1,232 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Need Help i need a programmer

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

Oct 18 '07 #1
16 1653
On 2007-10-18 11:56, zo************@gmail.com 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.
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-lit...t.html#faq-5.2

--
Erik Wikström
Oct 18 '07 #2
On 18 Oct, 10:56, zohaib.shir...@gmail.com wrote:
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
--
Nick Keighley

Oct 18 '07 #3
On Oct 18, 4:03 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
On 18 Oct, 10:56, zohaib.shir...@gmail.com wrote:
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

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

Oct 18 '07 #4
zo************@gmail.com wrote:
On Oct 18, 4:03 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
>On 18 Oct, 10:56, zohaib.shir...@gmail.com wrote:
>>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

--
Nick Keighley

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
Oct 18 '07 #5
LR
zo************@gmail.com wrote:
On Oct 18, 4:03 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
>On 18 Oct, 10:56, zohaib.shir...@gmail.com wrote:
>>i have got a quizz to [...]
the idea of a "quiz" is for *you* to do the work
>
Remember i am woking in C++ with complier VC..... Sorry 4 not telling
u that
I think you're far more likely to get help here by showing what you have
done so far rather than making a request for someone else to show you
how to do it.

What have you tried so far?

LR


Oct 18 '07 #6
zo************@gmail.com wrote in news:1192705580.055095.159270
@e9g2000prf.googlegroups.com:
On Oct 18, 4:03 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
>On 18 Oct, 10:56, zohaib.shir...@gmail.com wrote:
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

--
Nick Keighley

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.
Oct 18 '07 #7
zo************@gmail.com wrote:
On Oct 18, 4:03 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
>On 18 Oct, 10:56, zohaib.shir...@gmail.com wrote:
>>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

--
Nick Keighley

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.

--
SM
rot13 for email
Oct 18 '07 #8
On Oct 18, 5:56 am, zohaib.shir...@gmail.com 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

Oct 18 '07 #9
On Oct 18, 5:56 am, zohaib.shir...@gmail.com 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.

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!

Oct 18 '07 #10
On 18 Oct, 10:56, zohaib.shir...@gmail.com 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.

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<ustruct 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 bbstruct 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,yeval;

</code>

Oct 18 '07 #11
tragomaskhalos wrote:
On 18 Oct, 10:56, zohaib.shir...@gmail.com 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.

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?
Oct 18 '07 #12
On 18 Oct, 22:28, red floyd <no.s...@here.dudewrote:
tragomaskhalos wrote:
On 18 Oct, 10:56, zohaib.shir...@gmail.com 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<ustruct 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,yeval;
Oct 18 '07 #13
On 2007-10-18 18:35:27 -0400, tragomaskhalos
<da*************@logicacmg.comsaid:
On 18 Oct, 22:28, red floyd <no.s...@here.dudewrote:
>tragomaskhalos wrote:
>>On 18 Oct, 10:56, zohaib.shir...@gmail.com 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<ustruct 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,yeval;
Nice. It didn't work on g++ v.4.0.1, but I see what you're trying to do.

--

-kira

Oct 19 '07 #14
THANX every1 4.............................

Oct 23 '07 #15
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

Oct 31 '07 #16
"Xohaib" wrote:
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..
Oct 31 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sofia | last post by:
My name is Sofia and I have for many years been running a personals site, together with my partner, on a non-profit basis. The site is currently not running due to us emigrating, but during its...
55
by: Alex | last post by:
Hello people, The following is not a troll but a serious request. I found myself in a position where I have to present a Pro/Con list to management and architects in our company with regard to...
29
by: RAY | last post by:
Hi , my boss has asked I sit in on an interview this afternoon and that I create some interview questions on the person's experience. What is C++ used for and why would a company benefit from...
3
by: E.F. | last post by:
I have a Matrox RT.X100 graphic card connected to a digital camera. Now i need to develop a Vision Robotic System in C++ and i need to use some DLL to caprture frames from the graphic card. ...
2
by: Jon Davis | last post by:
The garbage handler in the .NET framework is handy. When objects fall out of scope, they are automatically destroyed, and the programmer doesn't have to worry about deallocating the memory space...
13
by: Trint Smith | last post by:
Ok, On my website, tribidz.com, there is one page that needs to not have any buttons or menu or address bar. How can I do this? What is the html code or whatever? Thanks, Trint ..Net...
4
by: Ashish | last post by:
Hi, Can someone recommend a VB.NET book for an experienced VB programmer? I have extensive experience in programming in VB, but lately I decided to try VB.NET too. I think I have pretty good...
2
by: mike | last post by:
Hi. I need a web programmer to set up interactive features on a static web site. The project needs are described at http://vitalkids.org My email address shows near the top of the page at...
2
by: robnhood00 | last post by:
I need a python programmer that can integrate graphics into an existing python application. The application is a basic application and the job should be pretty easy for an experienced Python...
0
by: Michael J. Yanowitz | last post by:
Hello: I think I can do it but would like more details. I have been doing GUI work for years. I have been doin Python only for the last 2 and 1/2 years. The GUI work I have done there has been...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.