FAQ HELP with a C/C++ program adding numbers... (C++)

This is a discussion on FAQ HELP with a C/C++ program adding numbers... (C++) within the FAQ Board forums, part of the General Programming Boards category; Hey everybody...this is a new programmer....I started programming less than a week ago...I need help creating a c program that ...

  1. #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.

  2. #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

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

  4. #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

  5. #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

  6. #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...

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    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.

  8. #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.

  9. #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

  10. #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

  11. #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!

  12. #12
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    oki doki...

    good luck....

    keep up the good work....

    Regards,
    matheo917

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. Wiki FAQ
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 192
    Last Post: 04-29-2008, 01:17 PM
  3. Replies: 12
    Last Post: 02-28-2008, 05:19 PM
  4. FAQ : running program inside program (spawn)
    By nipun in forum C Programming
    Replies: 3
    Last Post: 06-13-2004, 02:30 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

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