Functions

I am a bit confused in functions maybe because I am teaching myself but can any
one tell me how to find sum of all the number between two integers entered by the user, using a function regardless of which input is bigger. Thank you
Break it down into smaller parts.
1. Create a function which takes your two numbers as parameters
2. Find which is the bigger number
3. loop from the smallest to biggest
4. add the control variable of the loop to a sum variable
5. Return the sum
Like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>

// "const" and "&" are so a copy of Num1 and Num2 aren't created
int add(const int& Num1, const int& Num2)
{
    int Sum = 0;

    // Add all numbers betweeen Num1 and Num2
    if (Num1 < Num2)
    {
        for (int Temp = Num1 + 1; Temp < Num2; Temp++)
        {
            Sum += Temp;
        }
    }
    else if (Num1 > Num2)
    {
        for (int Temp = Num2 + 1; Temp < Num1; Temp++)
        {
            Sum += Temp;
        }
    }

    return Sum;
}

int main()
{
    int Num1, Num2, Sum;

    std::cout << "Enter the two numbers you would like to add:\n\n";
    std::cout << "1st number: ";
    while (! (std::cin >> Num1))
    {
        std::cout << "Enter a number: ";
        std::cin.clear();
        std::cin.ignore(1000000, '\n');
        std::cin >> Num1;
    }
    std::cout << "2nd number: ";
    while (! (std::cin >> Num2))
    {
        std::cout << "Enter a number: ";
        std::cin.clear();
        std::cin.ignore(1000000, '\n');
        std::cin >> Num2;
    }

    // Add the numbers in between
    Sum = add(Num1, Num2);

    std::cout << "\n\nAnswer: " << Sum;

    return 0;
}
Last edited on
Like this:

This does not compile.
@SakurasouBusters

It does now.
If you write for (int Temp = Num1 + 1; Temp < Num2; Temp++), why not do this?

for (int Temp = Num1 + 1; Temp < Num2 - 1; Temp++)

Actually I don't care about the +1 or -1 stuff. You can just write :
for (int Temp = Num1; Temp < Num2; Temp++)
@SakurasouBusters

The OP said he wanted to add the numbers in between the two integers.

Starting Temp with a number above Num1 and ending when Temp is not less than Num2.
Last edited on
I don't care. What if the odd is Num1 > Num2??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

// "const" and "&" are so a copy of Num1 and Num2 aren't created
int add(const int& Num1, const int& Num2)
{
    int Sum = 0;
    int numMin = (Num1 < Num2) ? Num1 : Num2;
    int numMax = (Num1 > Num2) ? Num1 : Num2;

    // Add all numbers betweeen Num1 and Num2
    for (int Temp = numMin; Temp < numMax; Temp++)
    {
        Sum += Temp;
    }

    return Sum;
}

int main()
{
    int Num1, Num2, Sum;

    std::cout << "Enter the two numbers you would like to add:\n\n";
    std::cout << "1st number: ";
    std::cin >> Num1;
    std::cout << "2nd number: ";
    std::cin >> Num2;

    // Add the numbers in between
    Sum = add(Num1, Num2);

    std::cout << "\n\nAnswer: " << Sum;

    return 0;
}
If the user enters a higher number first it does not work.

I would make these changes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int add(int num1,int num2)
{
	int sum = 0;
	int temp = 0;

	if (num2 < num1) {
		swap(num1, num2);
	}

	for (int i = num1 + 1; i < num2; i++)
	{
		sum += i;
	}
	return sum;
}
Last edited on
@SakurasouBusters
@switchy

Thanks for catching that. I'll edit my previous post accordingly.
Last edited on
Thank you everyone for the help now I understand the concept.. Thank you for being such a help.
Topic archived. No new replies allowed.