P: n/a

I found the code for a program that is suppose to be able to work out
the sumation of i from 1 to a certain integer n.
however, when I implemented to code into Visual C++, it doesn't work.
The original sample code provided by the website:
unsigned int Sum (unsigned int n) {
unsigned int result = 0;
for (unsigned int i=1; i<=n; i++) {
result+=1;
return result;
}
}
and the weblink for this code is http://www.brpreiss.com/books/opus4/html/page37.html
now, I tried to include the whole main function, cin and cout all that
stuff so I can run it and test it. Now the code looks like this:

1#include <iostream.h//assignment number 5087
2
3unsigned int Sum (unsigned int n);
4
5unsigned int Sum (unsigned int n) {
6
7 cout<<"Please enter n: "<<endl;
8 cin>>n;
9
10 unsigned int result = 0;
11 for (unsigned int i=1; i<=n; i++) {
12 result+=1;
13 return result;
14 }
15}
16
17 void main {
a=Sum;
cout<<"Sum is "<<a<<endl;
}

Now the compiler tells me I have got two errors. The error messages
are:
C:\Documents and Settings\Owner\My Documents\University\Data
Structures and Algorithms\sample 1\Cpp1.cpp(17) : error C2182:
'main' : illegal use of type 'void'
C:\Documents and Settings\Owner\My Documents\University\Data
Structures and Algorithms\sample 1\Cpp1.cpp(17) : error C2239:
unexpected token '{' following declaration of 'main'.
In other words, there are two things wrong with line 17. one is and
illegal use of type void, which is weird since just about all of my
assignments so far have been done with void main. The other is some
"unexpected token". Somebody help me! I am about the drown in the vast
sea of confusion! :}  
Share this Question
P: n/a
 da******@brentwood.bc.ca wrote:
16
17 void main {
a=Sum;
cout<<"Sum is "<<a<<endl;
}

Now the compiler tells me I have got two errors. The error messages
are:
C:\Documents and Settings\Owner\My Documents\University\Data
Structures and Algorithms\sample 1\Cpp1.cpp(17) : error C2182:
'main' : illegal use of type 'void'
C:\Documents and Settings\Owner\My Documents\University\Data
Structures and Algorithms\sample 1\Cpp1.cpp(17) : error C2239:
unexpected token '{' following declaration of 'main'.
In other words, there are two things wrong with line 17. one is and
illegal use of type void, which is weird since just about all of my
assignments so far have been done with void main. The other is some
"unexpected token". Somebody help me! I am about the drown in the vast
sea of confusion! :}
Ignoring the rest of the code; main returns int, so void main() is
illformed. You are also missing the parenthesis after "main".

Ian Collins.  
P: n/a

>
Ignoring the rest of the code; main returns int, so void main() is
illformed. You are also missing the parenthesis after "main".

Ian Collins.
LOL. I can't believe it is something that simple. Thank you.  
P: n/a

What does this mean?
C:\Documents and Settings\Owner\My Documents\University\Data
Structures and Algorithms\sample 1\Cpp1.cpp(14) : warning C4715:
'Sum' : not all control paths return a value  
P: n/a
 da******@brentwood.bc.ca wrote:
What does this mean?
C:\Documents and Settings\Owner\My Documents\University\Data
Structures and Algorithms\sample 1\Cpp1.cpp(14) : warning C4715:
'Sum' : not all control paths return a value
Please retain some context.
If you look again, you will see that you have your return inside the for
loop. You probably want it outside of the loop.

Ian Collins.  
P: n/a

On May 24, 10:23 pm, Ian Collins <iann...@hotmail.comwrote:
davy....@brentwood.bc.ca wrote:
What does this mean?
C:\Documents and Settings\Owner\My Documents\University\Data
Structures and Algorithms\sample 1\Cpp1.cpp(14) : warning C4715:
'Sum' : not all control paths return a value
Please retain some context.
If you look again, you will see that you have your return inside the for
loop. You probably want it outside of the loop.

Ian Collins.
how is it that your can reply like right after someone makes a post?
not that I am nor grateful or something, I am very grateful, but do
you like get paid to do this?  
P: n/a

How do I clear a variable at the end of a while loop?  
P: n/a
 da******@brentwood.bc.ca wrote:
On May 24, 10:23 pm, Ian Collins <iann...@hotmail.comwrote:
>davy....@brentwood.bc.ca wrote:
>>What does this mean? C:\Documents and Settings\Owner\My Documents\University\Data Structures and Algorithms\sample 1\Cpp1.cpp(14) : warning C4715: 'Sum' : not all control paths return a value
Please retain some context.
If you look again, you will see that you have your return inside the for loop. You probably want it outside of the loop.
 Ian Collins.
Please don't quote people's signatures, the lines after the " ".
>
how is it that your can reply like right after someone makes a post?
not that I am nor grateful or something, I am very grateful, but do
you like get paid to do this?
I just happened to be sitting here!

Ian Collins.  
P: n/a

On May 25, 1:58 pm, davy....@brentwood.bc.ca wrote:
unsigned int Sum (unsigned int n) {
unsigned int result = 0;
for (unsigned int i=1; i<=n; i++) {
result+=1;
return result;
}
}
and the weblink for this code ishttp://www.brpreiss.com/books/opus4/html/page37.html
You've made several transcription errors:
* You added extra { }
* You changed "result += i" to "result+=1"
* You changed ++i to i++
The first two drastically change the effect of the code.
1#include <iostream.h//assignment number 5087
(Please don't post linenumber listings , it just
makes it harder for us to put your code in our
compilers).
There is no such standard header as <iostream.h.
The C++ standard came out in 1998 so that suggests
your learning materials are 10 years old or more.
You could remedy this by writing:
#include <iostream>
using namespace std;
which has a similar effect to what some old compilers
did when you wrote #include <iostream.h.
3unsigned int Sum (unsigned int n);
4
5unsigned int Sum (unsigned int n) {
Line 3 is redundant (although this isn't a terrible error).
17 void main {
Should be:
int main() {
main must return an int in Standard C++. While there
are some compilers that also allow it to return void,
you may find that your code fails when you try it on
a different compiler.
a=Sum;
Should be:
a = Sum();
You need to use the brackets to indicate that the
function should be called.  
P: n/a

On May 25, 4:03 pm, Old Wolf <oldw...@inspire.net.nzwrote:
On May 25, 1:58 pm, davy....@brentwood.bc.ca wrote:
unsigned int Sum (unsigned int n) {
a=Sum;
Should be:
a = Sum();
In fact, it should be:
a = Sum(5);
or some other number as an argument to the function.  
P: n/a
 da******@brentwood.bc.ca wrote:
How do I clear a variable at the end of a while loop?
Kind of depends on what you mean by 'clear a variable', could you be
more specific?
john  
P: n/a

On May 25, 5:58 am, davy....@brentwood.bc.ca wrote:
Now the compiler tells me I have got two errors.
when you make a single mistake C++ nags a lot.That is most of time
when you fix an error several birds get killed with one stone.this
time is not an exception and you have one syntactical mistake:
17 void main {
looks like you have forggoten the pharanteses after function main:
line_17: void main(){//etc
(also you need remove line numbers.)
10 unsigned int result = 0;
11 for (unsigned int i=1; i<=n; i++) {
12 result+=1;
13 return result;
14 }
this algorithm wont work and the result will always be 1 .I suggest
that you move the return statement to the line after the end of the
for loop:
line_10: unsigned int result = 0;
line_11: for (unsigned int i=1; i<=n; i++) {
line_12: result+=1;
line_13: };
line_14: return result;
in fact since your loop has a singleline statement romoving the '{'
and '}' on line_11 and line_13 will not harm.
finally it is recommended though not nessesary that main returns
int.
cheers,
FM  
P: n/a

This is my own C++ code for finding the sum of an arithmetics
sequence,
#include <iostream.h>
void main () {
int i, n, result=0;
while (true) {
cout<<"enter n"<<endl;
cin>>n;
if ((n==999)  (n==0)) {
break;
}
for (i=1; i<=n; i++) {
result+=i;
}
cout<<"result is"<<result<<endl;
}
}
As you can see the variable "result" needs to be cleared at the end of
each while loop or the result from the previous loop will be carried
on. hence, the summation of 1 through 4 will be greater then the
summation of 1 through 5 if I worked with 1 through 5 first, 1 through
4 second.  
P: n/a

On Fri, 25 May 2007 19:01:46 0700, davy.zou wrote:
This is my own C++ code for finding the sum of an arithmetics sequence,
#include <iostream.h>
void main () {
int i, n, result=0;
while (true) {
cout<<"enter n"<<endl;
cin>>n;
if ((n==999)  (n==0)) {
break;
}
Put
result = 0;
here or replace the for loop with
result = n % 2 ? (n+1)/2 * n : n/2 * (n+1);
for (i=1; i<=n; i++) {
result+=i;
}
cout<<"result is"<<result<<endl;
}
}
As you can see the variable "result" needs to be cleared at the end of
each while loop or the result from the previous loop will be carried on.
hence, the summation of 1 through 4 will be greater then the summation
of 1 through 5 if I worked with 1 through 5 first, 1 through 4 second.

Markus Schoder  
P: n/a

On May 26, 6:23 am, Markus Schoder <a3vr6dsguse...@yahoo.dewrote:
On Fri, 25 May 2007 19:01:46 0700, davy.zou wrote:
This is my own C++ code for finding the sum of an arithmetics sequence,
#include <iostream.h>
void main () {
int i, n, result=0;
while (true) {
cout<<"enter n"<<endl;
cin>>n;
if ((n==999)  (n==0)) {
break;
}
Put
result = 0;
here or replace the for loop with
result = n % 2 ? (n+1)/2 * n : n/2 * (n+1);
for (i=1; i<=n; i++) {
result+=i;
}
cout<<"result is"<<result<<endl;
}
}
As you can see the variable "result" needs to be cleared at the end of
each while loop or the result from the previous loop will be carried on.
hence, the summation of 1 through 4 will be greater then the summation
of 1 through 5 if I worked with 1 through 5 first, 1 through 4 second.

Markus Schoder
a better solution:
inline unsigned long sum(unsigned short n){
return (unsigned long)n*n>>1;
};
now call sum wherever you need.
Cheers,
FM  
P: n/a

On May 26, 8:14 pm, terminator <farid.mehr...@gmail.comwrote:
On May 26, 6:23 am, Markus Schoder <a3vr6dsguse...@yahoo.dewrote:
On Fri, 25 May 2007 19:01:46 0700, davy.zou wrote:
This is my own C++ code for finding the sum of an arithmetics sequence,
#include <iostream.h>
void main () {
int i, n, result=0;
while (true) {
cout<<"enter n"<<endl;
cin>>n;
if ((n==999)  (n==0)) {
break;
}
Put
result = 0;
here or replace the for loop with
result = n % 2 ? (n+1)/2 * n : n/2 * (n+1);
for (i=1; i<=n; i++) {
result+=i;
}
cout<<"result is"<<result<<endl;
}
}
As you can see the variable "result" needs to be cleared at the end of
each while loop or the result from the previous loop will be carried on.
hence, the summation of 1 through 4 will be greater then the summation
of 1 through 5 if I worked with 1 through 5 first, 1 through 4 second.
a better solution:
inline unsigned long sum(unsigned short n){
return (unsigned long)n*n>>1;
The above line has undefined behavior, and will give different
results with different compilers, or different levels of
optimization with the same compiler.
If you want a function, then use the expression Markus proposed:
int
sum( int n )
{
return n % 2 ? (n+1)/2 * n : n/2 * (n+1) ;
}
You can simplify it if you know that n will be very small. Such
assumptions are generally counterproductive in a library
function, however. (You should probably add an assert
concerning n, to avoid possible overflow. But the
testnecessary though it bewas missing in the original code
as well.)

James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.Cyrl'École, France, +33 (0)1 30 23 00 34  
P: n/a

On Sat, 26 May 2007 11:14:41 0700, terminator wrote:
On May 26, 6:23 am, Markus Schoder <a3vr6dsguse...@yahoo.dewrote:
>On Fri, 25 May 2007 19:01:46 0700, davy.zou wrote:
This is my own C++ code for finding the sum of an arithmetics
sequence,
#include <iostream.h>
void main () {
int i, n, result=0;
while (true) {
cout<<"enter n"<<endl;
cin>>n;
if ((n==999)  (n==0)) {
break;
}
Put result = 0;
here or replace the for loop with
result = n % 2 ? (n+1)/2 * n : n/2 * (n+1);
for (i=1; i<=n; i++) {
result+=i;
}
cout<<"result is"<<result<<endl;
}
}
As you can see the variable "result" needs to be cleared at the end
of each while loop or the result from the previous loop will be
carried on. hence, the summation of 1 through 4 will be greater then
the summation of 1 through 5 if I worked with 1 through 5 first, 1
through 4 second.
a better solution:
inline unsigned long sum(unsigned short n){
return (unsigned long)n*n>>1;
};
now call sum wherever you need.
This has undefined behaviour. If n*(n1)>>1 was intended it is also
mathematically wrong.
Also why use "unsigned short"? That is 16 bit on many platforms and long
may be 64 bit. Using >to divide by a constant power of 2 is also
unnecessary since compilers will easily optimize that themselves.
The reason I used the ?: construct is to avoid overflow for large n by
the way.

Markus Schoder  
P: n/a

On May 27, 4:41 am, Markus Schoder <a3vr6dsguse...@yahoo.dewrote:
On Sat, 26 May 2007 11:14:41 0700, terminator wrote:
On May 26, 6:23 am, Markus Schoder <a3vr6dsguse...@yahoo.dewrote:
a better solution:
inline unsigned long sum(unsigned short n){
return (unsigned long)n*n>>1;
};
now call sum wherever you need.
This has undefined behaviour.
Are u certain?
If n*(n1)>>1 was intended it is also
mathematically wrong.
Yes this one is so silly a mistake.I should have written this:
return (n*(long)(n+1))>>1;
may be 64 bit. Using >to divide by a constant power of 2 is also
unnecessary since compilers will easily optimize that themselves.
I would like to force compiler to do it the faster way.Maybe someone
uses an old compiler.
Also why use "unsigned short"? That is 16 bit on many platforms and long
The reason I used the ?: construct is to avoid overflow for large n by
the way.
the result of integer multiplication is as many bits long as the sum
of nessasary bits for operands;So in general we can assume that result
of integer multiplication is twice its operands in size.On many
machines (all of them ,as well as I know)'long' is more than twice
'short' in size;therefore no bounds check for an unsigned will be
nessesary.In your solution overflow is not fully prevented.consider:
unsigned n= ~(1<<(sizeof(int)1));
unsigned result=(n+1)/2*n;/*overflow:result is 2*sizeof(int)1 bits
long where we only have sizeof(int) bits. */
result=(n/2*(n+1);//overflow again
__int64 result 64=(n+1)/2*n;//ok:enough bits available now.
regards,
FM  
P: n/a

On Sun, 27 May 2007 02:37:10 0700, terminator wrote:
On May 27, 4:41 am, Markus Schoder <a3vr6dsguse...@yahoo.dewrote:
>On Sat, 26 May 2007 11:14:41 0700, terminator wrote:
On May 26, 6:23 am, Markus Schoder <a3vr6dsguse...@yahoo.dewrote:
a better solution:
inline unsigned long sum(unsigned short n){
return (unsigned long)n*n>>1;
};
now call sum wherever you need.
This has undefined behaviour.
Are u certain?
Yes. I think this is in the FAQ.
>Also why use "unsigned short"? That is 16 bit on many platforms and long The reason I used the ?: construct is to avoid overflow for large n by the way.
the result of integer multiplication is as many bits long as the sum of
nessasary bits for operands;So in general we can assume that result of
integer multiplication is twice its operands in size.On many machines
(all of them ,as well as I know)'long' is more than twice 'short' in
size;therefore no bounds check for an unsigned will be nessesary.In your
solution overflow is not fully prevented.consider:
unsigned n= ~(1<<(sizeof(int)1));
unsigned result=(n+1)/2*n;/*overflow:result is 2*sizeof(int)1 bits long
where we only have sizeof(int) bits. */ result=(n/2*(n+1);//overflow
again
__int64 result 64=(n+1)/2*n;//ok:enough bits available now.
sizeof gives the number of _bytes_ not bits but I get the idea.
My point was to have code that is equivalent to the OP's who used int.
The ?: will not prevent overflow but guarantees that it will not overflow
for smaller n than the OP's code.
Using short as input with a long result may prevent overflow from
happening but may also limit the input range (this is all platform
specific since there is no guarantee that sizeof(short) < sizeof(long))
and __int64 or long long or int64_t is nonstandard.

Markus Schoder  
P: n/a

On May 27, 11:37 am, terminator <farid.mehr...@gmail.comwrote:
On May 27, 4:41 am, Markus Schoder <a3vr6dsguse...@yahoo.dewrote:
On Sat, 26 May 2007 11:14:41 0700, terminator wrote:
On May 26, 6:23 am, Markus Schoder <a3vr6dsguse...@yahoo.dewrote:
a better solution:
inline unsigned long sum(unsigned short n){
return (unsigned long)n*n>>1;
};
now call sum wherever you need.
This has undefined behaviour.
Are u certain?
The standard says it is. In practice, different compilers give
very different results.
If n*(n1)>>1 was intended it is also
mathematically wrong.
Yes this one is so silly a mistake.I should have written this:
return (n*(long)(n+1))>>1;
What does the cast change?
And of course, this may overflow although the results will fit.
may be 64 bit. Using >to divide by a constant power of 2 is also
unnecessary since compilers will easily optimize that themselves.
I would like to force compiler to do it the faster way.Maybe someone
uses an old compiler.
How do you know it is the faster way? (I've actually worked on
a machine, the NSC 32000, where it wasn't.) This sort of coding
is really rather amateurish, and wouldn't be allowed in any
reasonable company.
Also why use "unsigned short"? That is 16 bit on many platforms and long
The reason I used the ?: construct is to avoid overflow for large n by
the way.
the result of integer multiplication is as many bits long as the sum
of nessasary bits for operands;So in general we can assume that result
of integer multiplication is twice its operands in size.On many
machines (all of them ,as well as I know)'long' is more than twice
'short' in size;therefore no bounds check for an unsigned will be
nessesary.
You obviously don't know very many machines, then. There are
machines on which short is the same size as long.
In your solution overflow is not fully prevented.
If the results won't fit, there's not much you can do. His
solution won't overflow if the results are representable. Yours
will overflow for intermediate values even when the correct
results are representable.

James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.Cyrl'École, France, +33 (0)1 30 23 00 34  
P: n/a

<da******@brentwood.bc.cawrote in message ...
This is my own C++ code for finding the sum of an arithmetics
sequence,
#include <iostream.h>
void main () {
That's not C++. This is C++ <G:
#include <iostream>
int main(){
using std::cout;
using std::cin;
using std::endl;
while( true ){
int n(0), result(0); // now 'result' IS cleared every loop
cout<<"enter n"<<endl;
cin>>n;
if( (n==999)  (n==0) ){
break;
} // if(n)
//  put 'result' here 
// int result(0); // now 'result' IS cleared every loop
for( int i(1); i <= n; ++i){
result+=i;
} // for(i)
cout<<"result is"<<result<<endl;
} // while()~
return 0; // self documentation :}
} // main()

Bob R
POVrookie  
P: n/a

On May 27, 6:59 pm, Markus Schoder <a3vr6dsguse...@yahoo.dewrote:
On Sun, 27 May 2007 02:37:10 0700, terminator wrote:
On May 27, 4:41 am, Markus Schoder <a3vr6dsguse...@yahoo.dewrote:
On Sat, 26 May 2007 11:14:41 0700, terminator wrote:
On May 26, 6:23 am, Markus Schoder <a3vr6dsguse...@yahoo.dewrote:
a better solution:
inline unsigned long sum(unsigned short n){
return (unsigned long)n*n>>1;
};
now call sum wherever you need.
This has undefined behaviour.
Are u certain?
Yes. I think this is in the FAQ.
Also why use "unsigned short"? That is 16 bit on many platforms and
long The reason I used the ?: construct is to avoid overflow for large
n by the way.
the result of integer multiplication is as many bits long as the sum of
nessasary bits for operands;So in general we can assume that result of
integer multiplication is twice its operands in size.On many machines
(all of them ,as well as I know)'long' is more than twice 'short' in
size;therefore no bounds check for an unsigned will be nessesary.In your
solution overflow is not fully prevented.consider:
unsigned n= ~(1<<(sizeof(int)1));
unsigned result=(n+1)/2*n;/*overflow:result is 2*sizeof(int)1 bits long
where we only have sizeof(int) bits. */ result=(n/2*(n+1);//overflow
again
__int64 result 64=(n+1)/2*n;//ok:enough bits available now.
sizeof gives the number of _bytes_ not bits but I get the idea.
You got me again . I am really sorry for the mistake.
>
My point was to have code that is equivalent to the OP's who used int.
The ?: will not prevent overflow but guarantees that it will not overflow
for smaller n than the OP's code.
Using short as input with a long result may prevent overflow from
happening but may also limit the input range (this is all platform
specific since there is no guarantee that sizeof(short) < sizeof(long))
no guarantee but intriduce a platform who does not fit into the rule
that sizeof(long)>= 2*sizeof(short).
in case this is not satisfactory, use masking for overflow prevention:
unsigned sum(unsigned n){
enum{mask=(~0)<<sizeof(unsigned)*4};
if(n & mask) retun 0;//one can throw instead of return
return (n*(n+1))>>1;
};
and __int64 or long long or int64_t is nonstandard.
Sure.it was just for clarification.

Markus Schoder Hide quoted text 
 Show quoted text 
regards,
FM.  
P: n/a

On May 28, 12:18 am, James Kanze <james.ka...@gmail.comwrote:
On May 27, 11:37 am, terminator <farid.mehr...@gmail.comwrote:
>
How do you know it is the faster way? (I've actually worked on
a machine, the NSC 32000, where it wasn't.) This sort of coding
is really rather amateurish, and wouldn't be allowed in any
reasonable company.
You obviously don't know very many machines, then. There are
machines on which short is the same size as long.
we agree on this.
In your solution overflow is not fully prevented.
If the results won't fit, there's not much you can do. His
solution won't overflow if the results are representable. Yours
will overflow for intermediate values even when the correct
results are representable.
what does the term intermediat mean please?
return (n*(long)(n+1))>>1;
What does the cast change?
And of course, this may overflow although the results will fit.
it should have been like this:
return (n*( (long)n + 1 ))>>1;
now n+1 wont be zero only if long is bigger than short.this is what I
wanted. but mistakes always happen.
regards,
FM   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 1533
 replies: 22
 date asked: May 25 '07
