View Full Version : FAQ HELP with a C/C++ program adding numbers... (C++)
benw25
11-11-2001, 05:42 PM
Hey everybody...this is a new programmer....I started programming less than a week ago...I need help creating a c program that adds numbers from 1 to a million. I have tried it using many tutorials, but I don't know how to make it add from 1 to a million because 1 million is too big and there is an error...some tutorial said that the max was 32767. I am using Turbo C++ 4.5 Compiler and here is my program so far...
#include <stdio.h>
int main()
{
int a, b;
a = 0;
b = 0;
while (a<1000001)
{
a++;
b = a + b;
printf("%d\n, a)
}
return 0;
}
I need help making the program so that it adds the numbers from 1 to a million and prints each number, and at the end it prints the sum.
matheo917
11-11-2001, 05:59 PM
hi....
the problem is in you defining your variable as integer
the definition
int number; creates a short signed int which goes from
(negative) - 32,768 to (positive) 32,768.... what the problem is that you are so called "wrapping around" an integer as soon as you get to 32,768 the next number will be (negative) - 32,768....
same as flying around the world....
now it depends what machine you are using... on most computers integers is 4 bytes but it seams like yours is 2 so the compiler uses the values that i have written above....
just to be sure .... instead of declaring these variables as integers (int) declare them as double.... which is very large and i am sure you will not "wrap around"... double number;
you can also use long intger ..... long int number;
i hope that will help....
if not then let us know.... i will try a different way
Regards,
matheo917
benw25
11-11-2001, 06:27 PM
How do you declare numbers as double integers and how do you use long integers?
matheo917
11-11-2001, 06:32 PM
on your third of fouth line ( i believe) you wrote:
int a, b;
by doing so you have declared two integers, a and b.....
right where you have written the phrase (int) (in front of a and b)
you must replace it with either "double" (i suggest) or "long int"
disregard the quotation marks.....
this is the only place where you make that change, everything else stays the same......
Regards,
matheo917
ender
11-12-2001, 12:09 AM
>now it depends what machine you are using... on most computers integers is >4 bytes but it seams like yours is 2 so the compiler uses the values that i have >written above....
I'm just nitpicking, but the # of bits that is used for an int, or any data type for that matter is determined by the compiler I believe. Also, an int use to be 4 hex digits = 16 bits, I guess new compilers make ints 32 bits = 8 hex digits.
Oh yeah, and I would use a long int :) Making and integer long doubles the number of bits. So instead of a range -2^16 to 2^16 = 32678, it becomes -2^32 to 2^32....bye
benw25
11-12-2001, 07:39 AM
Here is my program as of now...
#include <stdio.h>
long int main()
{
long int a, b;
a = 0;
b = 0;
while (a<1000001);
{
a++;
b = a + b;
printf("%d\n", a)
}
return 0;
}
I still have a few problems in this....
There are problems compiling this...
in line 9; Constant is long in function main()
in line 8; Code has no effect in function main()
in line 13; statement missing; in function main()
in line 15; compound statement missing } in function main()
I am using Borland's Turbo C++ Compiler on a pretty slow computer...thanks for the info but I still have some problems...
adrianxw
11-12-2001, 08:22 AM
while (a<1000001); <--- don't want that semi colon
printf("%d\n", a) <--- do want one here
Otherwise, that compiles and runs here, MS VC++ 6.0 NT4.
Brown Drake
11-12-2001, 11:34 AM
You don't need to make main return a long int. Main should return just an int (some people use void).
I'm not familiar with printf, but you probably need a semicolon at the end of that statement.
matheo917
11-12-2001, 03:40 PM
benw25, my dear friend.... i don't mean to be rude but you have to do more than that if you want us to help you.... if you have someone give you hints, please try do do some debugging yourself..... it doesn't take that much.... those mistakes are trivial and i am sure you are capable of fixing them yourself...besides you have a fairly good compiler and the explanation of the mistakes delivered by the compiler is very adequte....
ps. the last 2 posts should fix your problems....just remember sometimes if you make one mistake your compiler will flag that you have more than one, but it's not necassarily true... sometimes your programs just exits and flags a unique error message about fatal error......
keep working....
good luck
Regards,
matheo917
ivandn
11-12-2001, 04:30 PM
Here is a trick... you can add 1 to x by just doing (x)(x+1)/2
Therefore 1 to 1,000,000 is (1,000,000)(1,000,001)/2 = 500000500000
you should be able to use type double for this calculation as well allowing you to calculate numbers higher than needed for this problem
benw25
11-12-2001, 05:20 PM
Ok...the 2 posts above seem to work...I tried to debug some of these myself, but since I have been doing programming for about a week now, my attempts failed...thanks everybody for helping me out!
matheo917
11-12-2001, 05:25 PM
oki doki...
good luck....
keep up the good work....
Regards,
matheo917
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.