Thread: Help Starting out a GetSeries program.

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    3

    Help Starting out a GetSeries program.

    I'm having a bit of trouble writing the code for a program. The program has two functions.

    Main: Gets the target sum and then calls GetSeries.

    Getseries: Determines the first and last integers in EVERY series of positive, consecutive INTEGERS whose sum is the target sum. Output sent to the screen . object.

    Now I can get the target sum, but I am having trouble calling getseries and then determining the integers for the sum.

    This is my program so far. Any help would be much appreciated. Also I have K.N. King's C programming book.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int tsum, firstnum, secondnum;
    
        printf("Enter the target sum: ");
        scanf("&#37;d", &tsum);
    
        
        return 0
    }
    Last edited by Salem; 12-10-2008 at 11:37 PM. Reason: Added [code][/code] tags, ;earn to use them yourself

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by xsojournerx View Post
    I'm having a bit of trouble writing the code for a program. The program has two functions.

    Main: Gets the target sum and then calls GetSeries.

    Getseries: Determines the first and last integers in EVERY series of positive, consecutive INTEGERS whose sum is the target sum. Output sent to the screen . object.

    Now I can get the target sum, but I am having trouble calling getseries and then determining the integers for the sum.

    This is my program so far. Any help would be much appreciated. Also I have K.N. King's C programming book.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    //definition of getSeries function
    int getSeries(int);
    
    int main()
    {
        int tsum, firstnum, secondnum;
    
        printf("Enter the target sum: ");
        scanf("&#37;d", &tsum);
    
       getSeries(tsum);  //call the function    
        return 0
    }
    //return from this series will tell if any series was found
    int getSeires(int target)   {  //stack has copied tsum and named it target, here
       int i, j, k;
     
       /* now we need to know where the data is, that needs to be checked */
    
       return 0;
    }
    Hope that helps.

    When you post code, be sure to wrap it in code tags so it displays right on the forum.
    Just highlight the code, then click on the hash sign, at the top of the post frame '#'.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    3
    Quote Originally Posted by Adak View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    //definition of getSeries function
    int getSeries(int);
    
    int main()
    {
        int tsum, firstnum, secondnum;
    
        printf("Enter the target sum: ");
        scanf("%d", &tsum);
    
       getSeries(tsum);  //call the function    
        return 0
    }
    //return from this series will tell if any series was found
    int getSeires(int target)   {  //stack has copied tsum and named it target, here
       int i, j, k;
     
       /* now we need to know where the data is, that needs to be checked */
    
       return 0;
    }
    Hope that helps.

    When you post code, be sure to wrap it in code tags so it displays right on the forum.
    Just highlight the code, then click on the hash sign, at the top of the post frame '#'.

    So now that I have the getSeries, which loop would be best to find the consecutive integers?

    I was thinking something like a while or a for loop, but this part just draws a blank for me. It seems as though I wouldn't even use a loop. Maybe it's just I don't know how to word it to find the two integers.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    I think a for loop would work. Find out how long the integers are, or how many of the integers you have and loop it that many times with a for loop.

    Code:
    //something like this for example
    for(int i = 0; i <= size/number of integers; i++)
    
    //also make sure you spell things the same
    
    int main()
    {
        int tsum, firstnum, secondnum;
    
        printf("Enter the target sum: ");
        scanf("&#37;d", &tsum);
    
       getSeries(tsum);  //call the function    
        return 0
    }
    //return from this series will tell if any series was found
    int getSeires(int target)   {  //stack has copied tsum and named it target, here
       int i, j, k;
     
       /* now we need to know where the data is, that needs to be checked */
    
       return 0;
    }

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    3
    Problem is that the length of the integer is based on the input of the user. Say the user entered 30. The program would have to printed as many series as possible thats sum is 30. And frankly I've never used getSeries before so I'm clueless.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by xsojournerx View Post
    And frankly I've never used getSeries before so I'm clueless.
    What, and we have?

    Anyway, you need to decide how to tell whether the series starting with (say) 1 can reach your target. And then whether the series starting with (say) 2 can reach your target. And then whether the series starting with (say) 3 can reach your target. And then....

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The getSeries function is just a name I thought up - we have to write up our own code for it, see?

    It's like a backyard garden - you grow your own.

    So we're just doing this for ONE sum? Just *ONE* sum?? EZ smeazy.

    I like a while loop for this, but a for loop can work just as well. Your choice. The logic will be very similar.
    Last edited by Adak; 12-10-2008 at 06:54 PM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Just some idea's:

    Code:
    #define MAX 100
    
    int answer[MAX] = { 0 };
    
    int main(void)  {
    
       get your sum number, here
       getSeries(sum);
    
       printf("\n\n\t\t Program Complete - Press Enter to Continue");
       gar = getchar(); gar++;  //holds the console window open
       return 0;
    }
    
    int lo, hi, i, j, tally, count;  //sum came into the function on the parameter set
    
    say sum is 10
    hi = sum / 2 + 2;
    //10/2 = 4, so add one, but one can be part of a series, so add one more
    lo = 1;
    
    while(lo < hi)   {
       set tally to zero, here
    
       /* idea here is to start at the low end, and add up the numbers, into tally,
          until tally is > target.  
       */
       
       for(i = lo, j = 0; tally < sum; i++)  {
          tally += i;
          answer[j++] = i;
    
          if( tally == sum)  {
             print the whole set of numbers in answer[], that have made up tally
             count++;  //count the sets that meet the criteria.
          }
    
    
       }
       lo++;
       reset the answer[] here, to all zero's.
    }
    This won't solve your problem. Just a step in the right direction.

    Edit: This thought worked out well. Interestingly, the only numbers w/o at least one series of consecutive integers
    equaling their sum, have an odd coincidence with computers:
    1, 2, 4, 8, 16, 32, 64, 128, 256, 512 where Sum = 1 - 1,000.
    Last edited by Adak; 12-11-2008 at 09:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Starting C++;First Program
    By Caliban in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2009, 01:41 PM
  2. Starting a program
    By mcgeady in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2006, 12:52 PM
  3. starting program
    By Ideswa in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2006, 02:36 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM