Looping Sums

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sakinah
    New Member
    • Apr 2007
    • 6

    #1

    Looping Sums

    Hi I am very very new to C++ i am trying to create a loop that reads 2 integers and addsall the intergers between. I am very very lost!
    #include <iostream>
    using namespace std;

    int main()
    {
    int num1=10;
    int num2=0;
    int total;

    while (num2<10)
    { total= num2+ 1;
    total++;
    }
    system ("pause");
    return 0;
    }
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by Sakinah
    Hi I am very very new to C++ i am trying to create a loop that reads 2 integers and addsall the intergers between. I am very very lost!
    #include <iostream>
    using namespace std;

    int main()
    {
    int num1=10;
    int num2=0;
    int total;

    while (num2<10)
    { total= num2+ 1;
    total++;
    }
    system ("pause");
    return 0;
    }
    Hi,this will start looping "when hell freezes over",because you are not increasing num2.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      So you want to sum m + (m+1) + (m+2) + ... + (n-2) + (n-1) + n?
      Say for m=2 and n=6 you want to find 2+3+4+5+6?

      There's a centuries old nice closed form formula available:

      0+1+2 + ... + n= n*(n+1)/2

      You don't want to count starting from 0, you want to count starting at m,
      so the values 0, 1, 2, .. m-1 should be substracted again: that last sum,
      using that same old formula again equals:

      0+1+2+ ... + m-1 = (m-1)*m/2

      So your sum for any m <= n (both nonnegative) equals:

      n*(n+1)/2-(m-1)*m/2

      We don't need no steenkin' loops ;-)

      kind regards,

      Jos

      Comment

      • Sakinah
        New Member
        • Apr 2007
        • 6

        #4
        OK so I suck! I have only been doing this for 3 weeks.

        Comment

        • Sakinah
          New Member
          • Apr 2007
          • 6

          #5
          ps thanks jos

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Sakinah
            ps thanks jos
            You're welcome of course. As you saw: there's no need for loops here. If your
            assignment text tells you to use loops, simply add the numbers
            m, (m+1), (m+2) ... (n-2), (n-1), n together; use a loop counter from m to n and
            initialize a sum variable to zero before you start the loop.

            kind regards,

            Jos

            Comment

            • Sakinah
              New Member
              • Apr 2007
              • 6

              #7
              ok this is exactly what my text says,...Write a C++ program with a loop structure that reads in 2 intergers, calculates the sum of all the intergers between thee 2 intergers (not including these 2 intergers) if any, and displays the final result. For example, if the 2 intergers ou read are -2 and 5, then your program should output 9 (=-1+0+1+2+3+4).


              Sakinah :-)

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                OK, you can use a for...loop here if you'd like. The initial condition will set your index equal to num1 + 1, the end condition will be while the index is less than num2, and you will increment the index in each update. Inside the loop, add the index to the total.

                Comment

                • Savage
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1759

                  #9
                  Originally posted by Sakinah
                  ok this is exactly what my text says,...Write a C++ program with a loop structure that reads in 2 intergers, calculates the sum of all the intergers between thee 2 intergers (not including these 2 intergers) if any, and displays the final result. For example, if the 2 intergers ou read are -2 and 5, then your program should output 9 (=-1+0+1+2+3+4).


                  Sakinah :-)
                  First and first did you have to input numbers or are the numbers declared?

                  Find the diferance between those numbers:

                  5-(-2)=7,and lower it by two becasue you are not including limits of interval.

                  This will be 5,
                  so this is it:

                  <Code has been removed. Please review our FAQ Posting Guidelines>

                  Regards,Savage! !!

                  Comment

                  • Sakinah
                    New Member
                    • Apr 2007
                    • 6

                    #10
                    thanks a bunch :-)

                    Comment

                    Working...