Jump to content

finding sum of between numbers, almost there i think?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
15 replies to this topic

#1
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
i need to find the sum of the between numbers that a person inputs.

so if i put in 1 and 3 then it should add 1+2+3 for an output of 6.

what im struggling with, is the loop i need to use(teacher said i have to) in the second function called sumTo.



#include <iostream>

using namespace std;


int sumTo(num1, num2);


int main ()

{

    

    int num1;

	int i;

	int num2; 

	int sumTo = 0;

    


    cout << "Please enter 2 numbers: ";

    cin >> num1 >> num2;


	 cout << "The sum of the numbers between " << num1 << " and " << num2 << " = " << sumTo(num1, num2) << endl;;



return 0;

} 

    

int sumTo(num1, num2)

{



    for (int  = num1; i < num2; i++)

    {

        sumTo += i;

    }

}

   

i dont think that "for" loop is right at all, but i couldnt think of any other way

im also getting an error that num1 and num2 are undeclared, although i did declare them in main

and theres a few other minor problems im guessing.

i think i have "creating functions" topic understood, but now im running into another problem.

thanks for your help if you can work me through the issue im having here

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Your function declaration is bad. You should put the type of the variable you want passed to it.

You have:
int sumTo(num1, num2);

You need:

int sumTo(int num1, int num2);

You have:
int sumTo(num1, num2)
{


    for (int  = num1; i < num2; i++)
    {
        sumTo += i;
    }
}

You need:
int sumTo(int num1, int num2)
{


    for (int  = num1; i < num2; i++)
    {
        sumTo += i;
    }
}


Three other things pop out at me.

1) You set sumTo to return an int value but you have no return in sumTo.
2) Your for loop is missing the initial variable i
3) You calculation will never include num2 unless you use <=

Here are the fixes:

int sumTo(int num1, int num2)
{


    for (int  i= num1; i <= num2; i++)
    {
        sumTo += i;
    }

    return sumTo;
}


#3
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
dang what a noob mistake on my part, i cant believe i forgot to put "int num1"

:shakes head:

i totally forgot the return, no wonder it wouldnt do anything.!!!!

thank you so much for your help, im going to compile it all tonight when i get home from work, i appreciate it.

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
No problem. If you have any more questions feel free to ask.

#5
icepack

icepack

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
for extra credit points and a O(1) algorithm you could do this:
int sum_between(int num1, int num2) {
int sum_1 = (num1 * (num1+1))/2; //sum of all the numbers from num1 to 1
int sum_2 = (num2 * (num2+1))/2;  //sum of all the numbers from num2 to 1
return sum_1 - sum2 + num1;  //we add num1 since it's inclusive

I'm so cool it hurts.
http://www.markdimarco.com

#6
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
@icepack:
Good job, the sum between 5 and 7 is now -8 thanks to you! You also have a typo in your algorithm on top of your flawed code.

@Jordan/Hawk1:
What if the user enters the numbers 9 and 6? Your code will never execute properly. Here's a final version I quickly wrote up for you to look over, it includes a function you (I think it was you) asked about earlier (a "get minimum" and "get maximum" function):

#include <iostream>
using namespace std;

int SumBetween(int value1, int value2);
int Max(int value1, int value2);
int Min(int value1, int value2);

/*
 * The main function, used to test the SumBetween function.
 */
int main ()
{
    int value1, value2;
    
    printf("Please enter 2 numbers: ");
    cin >> value1 >> value2;
	printf("The sum of the numbers between %d and %d = %d.\n", value1, value2, SumBetween(value1, value2));

    system("PAUSE");
    return 0;
} 
    
/*
 * SumBetween will return the sum between any two integer values.
 * If the first value is higher than the second value, it will
 * implicitly reverse the order of the two values and compute.
 */
int SumBetween(int value1, int value2)
{
    int i = Min(value1, value2);
    int k = Max(value1, value2);
    int sum = 0;
    while (i <= k) sum += i++;
    return sum;
}

int Max(int value1, int value2)
{
    int max = value1 > value2 ? value1 : value2;
    return max;
}

int Min(int value1, int value2)
{
    int min = value1 < value2 ? value1 : value2;
    return min;
}

Of particular interest, you should look into why this works:
while (i <= k) sum += i++;
Hint: it has to do with operator precedence. Have fun!

#7
icepack

icepack

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
oh if THATS the game you want to play :-)

change the last line in my function to:
#include <algorithm>
//........//
int sum_between(int num1, int num2) {

       int sum_1 = (num1 * (num1 + 1)) / 2;
       int sum_2 = (num2 * (num2 + 1)) / 2;
       return abs(sum_2-sum_1) + min(num1,num2);
}

I'm so cool it hurts.
http://www.markdimarco.com

#8
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
Haha I was just fooling around. Obviously your constant running time beats my O(n), but I think mine is easier to understand for a learning programmer, even though I wrote the while in one line. xD

I've actually never seen that used before, do you have a proof that sigma(i=n to k) is equal to your equation laying around somewhere? Not induction, just showing how to form that equation. I could probably come up with it myself, but I'm too lazy :P

#9
icepack

icepack

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
I got 18 for 5 and 7. and 7+6+5 = 18. Anyhow, I wrote it all in a main method and only transfered it to it's own method with the post. I don't see the typo. But yeah, the traditional O(n) method is easier to understand.
Unless you meant if they user enters the numbers backwards. In which case the second algorithm takes care of that.
I'm so cool it hurts.
http://www.markdimarco.com

#10
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Being a homework assignment I doubt anyone will put the first digit as more than the second digit. That might be the assignment next week! Nice coding anyway and very helpful. +rep to you both.

#11
icepack

icepack

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
@Steve.L
Math Forum - Ask Dr. Math

there is some history on the equation.

and some more on arithmetic series:
Mathwords: Arithmetic Series
I'm so cool it hurts.
http://www.markdimarco.com

#12
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts

icepack said:

I got 18 for 5 and 7. and 7+6+5 = 18. Anyhow, I wrote it all in a main method and only transfered it to it's own method with the post. I don't see the typo. But yeah, the traditional O(n) method is easier to understand.
Unless you meant if they user enters the numbers backwards. In which case the second algorithm takes care of that.


return sum_1 - sum2 + num1;  //we add num1 since it's inclusive


You just typed sum2 instead of sum_2, or maybe num2. I don't know, didn't really look over it very well. But either way it's still a typo. :P

And thanks for all your +reps Jordan, although I'm not quite sure what they do!