C Board  

Go Back   C Board > General Programming Boards > FAQ Board

 
 
LinkBack Thread Tools Display Modes
Old 11-11-2001, 05:42 PM   #1
Registered User
 
Join Date: Nov 2001
Posts: 14
Unhappy HELP with a C/C++ program adding numbers...

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.
benw25 is offline  
Old 11-11-2001, 05:59 PM   #2
Registered User
 
matheo917's Avatar
 
Join Date: Sep 2001
Posts: 279
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
matheo917 is offline  
Old 11-11-2001, 06:27 PM   #3
Registered User
 
Join Date: Nov 2001
Posts: 14
Question

How do you declare numbers as double integers and how do you use long integers?
benw25 is offline  
Old 11-11-2001, 06:32 PM   #4
Registered User
 
matheo917's Avatar
 
Join Date: Sep 2001
Posts: 279
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
matheo917 is offline  
Old 11-12-2001, 12:09 AM   #5
Registered User
 
Join Date: Nov 2001
Posts: 30
>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
ender is offline  
Old 11-12-2001, 07:39 AM   #6
Registered User
 
Join Date: Nov 2001
Posts: 14
Unhappy

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...
benw25 is offline  
Old 11-12-2001, 08:22 AM   #7
It's full of stars
 
adrianxw's Avatar
 
Join Date: Aug 2001
Posts: 4,833
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.
__________________
Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.
adrianxw is offline  
Old 11-12-2001, 11:34 AM   #8
Registered User
 
Join Date: Aug 2001
Posts: 154
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.
Brown Drake is offline  
Old 11-12-2001, 03:40 PM   #9
Registered User
 
matheo917's Avatar
 
Join Date: Sep 2001
Posts: 279
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
matheo917 is offline  
Old 11-12-2001, 04:30 PM   #10
Registered User
 
ivandn's Avatar
 
Join Date: Oct 2001
Posts: 49
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
__________________
Ivan
ivandn is offline  
Old 11-12-2001, 05:20 PM   #11
Registered User
 
Join Date: Nov 2001
Posts: 14
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!
benw25 is offline  
Old 11-12-2001, 05:25 PM   #12
Registered User
 
matheo917's Avatar
 
Join Date: Sep 2001
Posts: 279
oki doki...

good luck....

keep up the good work....

Regards,
matheo917
matheo917 is offline  
 

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Client-server system with input from separate program robot-ic Networking/Device Communication 3 01-16-2009 03:30 PM
Wiki FAQ dwks General Discussions 192 04-29-2008 01:17 PM
Program that accepts an array of integers and prints the minimum of the numbers kolucoms6 C++ Programming 12 02-28-2008 06:19 PM
FAQ : running program inside program (spawn) nipun C Programming 3 06-13-2004 02:30 PM
fopen(); GanglyLamb C Programming 8 11-03-2002 12:39 PM


All times are GMT -6. The time now is 03:09 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22