I'm sorry. These question probably sound very stupid to you people. But to me they are extremely difficult.

Imagine being completely ignorant with computers, and signing up for an intermediate level class (I did not take the beginner class)................ and NEEDING an A to make up for the 2 B's I got last semester!!

Lots of pressure (and I haven't even mentioned the worst part).

In any case........ I need help. I appreciate those who stop to help. Thank you.

This one I cannot figure out, despite adequate math skills. If it were JUST math I could probably work something out......... but it's not.

NOTE---- I am honest. I DO NOT want to copy anyone's code.... I just want help making ny own.

The objective is to use a "getseries" function. But you have to actually MAKE this function, right? I looked in my book and it is nowhere to be found.

How on Earth do I make the function??

I got it to where there are no errors... defined it, or whatever, to where it CAN work. But I don't know how to get it TO work.

What is has to do is this:

The user enters a number.

My function is called. It runs through to look for consecutive groups of numbers that add up to the input number.

Like if the user inputs 18....

The program finds that 3+4+5+6 = 18

Since 3 and 6 are the first and last #s in the sequence, it then prints 3 and 6

(plus it also does this for any other consectutive groupings that add to 18)

I thought of a while function

A = 1
B = (A + 1)
C = (A + 2)
D = (A + 3)

(while the total is less than input) run through a loop...

if A + B + C + D = 18....

Then increase A each time.

Problems with this---

1) What if it's a sequence of 3 numbers (not 4)? How do I tell the computer to stop at 3, and it's still good?

Maybe just use A?
A = 1
(IF A + (A+1) = 18....)
Else if A+(A+1)..... and on...

A++ each time through the loop?

How would I tell the computer to print A and the hard part---- whatever value (A + 1)etc is at that time?

Would this work?

Any other ideas??

Recommended Answers

All 5 Replies

Programming is thinking. All the rest is just learning syntax. Welcome to programming.

How would you do this on paper? Serious question. Once you understand how YOU would do this, on paper, in a methodical way, you'll understand how to program it.

Maybe something like this:

Presumably you would start with 1+2. Then Try 1+2+3. Then 1+2+3+4. Then 1+2+3+4+5. See the pattern? You would stop this pattern when the sum equalled or exceeded the total you're looking for.

Then 2+3. Then 2+3+4. Then 2+3+4+5. See the pattern? You would stop this pattern when the sum equalled or exceeded the total you're looking for.

Then 3+4. Then 3+4+5. Then 3+4+5+6. See the pattern? You would stop this pattern when the sum equalled or exceeded the total you're looking for.

Then 4+5. Then 4+5+6. Then 4+5+6+7. See the pattern? You would stop this pattern when the sum equalled or exceeded the total you're looking for.



Eventually, you'll get to the point where the first number in the suquence to add IS the number you're looking for. You know that you're finished.

No, I get that.

The problem isn't the math.... it's how to get the COMPUTER to do it.

Using the tools of the computer..... it is all very new to me. I don't know how to use the functions and stuff.

I'm still working on this one.

Maybe you need a refreshment on what a for-loop is? Click Here
Other tutorials are easily found on the web.

What exactly are the restraints on this task? I assume you can't use negative numbers, otherwise you could write any integer n as -(n - 1) + -(n - 2) + ... + 0 + ... + (n - 1) + n. You also mentioned the sequences can vary in length. I assume you're looking for lengths greater than 1? (Otherwise you could simply output a sequence of length 1 for every n) Is your program expected to output all possible sequences? (e.g. 30 = 6 + 7 + 8 + 9 but also 30 = 9 + 10 + 11)

The problem itself seems pretty interesting. I'd probably try to discover some sort of pattern. Looking at what numbers that can be generated using a sequence of length 1 or more you'd end up with a set of equations like:

3  + 2n
6  + 3n
10 + 4n
15 + 5n

These formula's could be generalized as sum(m) + mn. In this case we start atm = 2because we're not interested in sequences of length 1. If a given number x can be solved using of these formula's, you have the result. e.g: 30 = 10 + (4 * 5); you've found that n = 5 and m = 4. so the sequence you're looking for starts at (n + 1) and has length m. so: 5 + 6 + 7 + 8.

Looping over the possible equations and attempting to solve them should be easy (substract and modulo) aswell as checking when it's no use trying equations anymore because you'd never find a result.

Quick and dirty proof of concept:

#include <stdio.h>

int sum(const unsigned int n)
    if (n == 0)
        return 0;
        return n + sum(n - 1);

void print_sequence(const int n)
    int m       = 2,
        m_sum   = sum(m),
        k       = 0,
        r       = 0;

    while (m_sum <= n)
        k = 0;
        r = n - m_sum;

        while (r >= k * m)
            if (r == k * m)
                while (m --> 0)
                    printf("%d ", (k++) + 1);

        m_sum = sum(m);

int main()
    int i = 0;

    for (i = 1; i <= 100; i++)
        printf("%d = ", i);
    return 0;

Based on this you'd develop a strong suspicion that any number except powers of 2 can be written this way. Might be fun to prove mathematically.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.