This sounds easy but have never done it

This is a discussion on This sounds easy but have never done it within the C Programming forums, part of the General Programming Boards category; I need to combine these programs into one program that works for a positive integer "n" The first is a ...

  1. #1
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32

    Red face This sounds easy but have never done it

    I need to combine these programs into one program that works for a positive integer "n"
    The first is a sum of squares starting with 1 until "n"
    The second is a sum of cubes starting with 1 until "n"
    The third is a sum of fractions starting with 1/2^1 until 1/2^n
    I know I need to take out my exit stuff until the last one and all I need is one set of preprocessor directives.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    main ()
    {
    /* Declare variables */
      int i, n, sumsquare = 0;;
      printf("Enter n: \n");
      scanf("%d", & n);
        /* Set for loop conditions */
      for (i=1; i <= n; i++) 
      {
            sumsquare += i*i; 
      }
          printf ("The sum of the squares from 1..%d is %d\n", n, 
          sumsquare);
     system("pause");
     exit(0); 
    
    }
    {
    /* Declare and initialize variables */
      int i=0, n, sumcubes=0;
    
      printf("Enter n: \n");
      scanf("%i", &n);
    
    /* Set for loop conditions */
      while (i++<n) 
      {
      sumcubes += (i*i*i);
      }
      printf ("The sum of the cubes from 1..%d is %d\n", n, 
          sumcubes); 
     system("pause");
     exit(0); 
    
    }
    {
        unsigned int i = 1, n;
        float sum=0;
    
        printf("Enter n: \n");
        scanf("%u", &n);
    
        do 
        {
            sum += (1/(pow(2,i)));
        } 
        while (i++ < n);
    
        printf ("The sum of the fractional exponentials from 1 ... %u is %f\n",
                n, sum);   
        system ("pause");
        return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Press edit, and fix your tags. Then, write functins.
    Code:
    int dosomething( int n )
    {
        ...do stuff...
        ...return something...
    }
    ... repeat for all functions ...
    
    int main( void )
    {
        ...prompt for n...
        ...read n...
    
        ...call all of your functions, passing them n...
        ...store whatever they return, and print it...
    
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32
    This is the best I can do right now & the compiler highlights the bracket of the second program. This is actually more interesting than I thought. Thank you quzah.
    I am going to apologize ahead of time for the lengthy code
    Code:
     main ()
    {
    /* Declare variables */
      int i, n, sumsquare = 0;;
      printf("Enter n: \n");
      scanf("%d", & n);
        /* Set for loop conditions */
      for (i=1; i <= n; i++) 
      {
            sumsquare += i*i; 
      }
          printf ("The sum of the squares from 1..%d is %d\n", n, 
          sumsquare);
     system("pause");
    
    }
    int main ()
    {
    /* Declare and initialize variables */
      int i=0, n, sumcubes=0;
    
      printf("Enter n: \n");
      scanf("%i", &n);
    
    /* Set for loop conditions */
      while (i++<n) 
      {
      sumcubes += (i*i*i);
      }
      printf ("The sum of the cubes from 1..%d is %d\n", n, 
          sumcubes); 
     system("pause");
    
    }
    int main ()
    {
        unsigned int i = 1, n;
        float sum=0;
    
        printf("Enter n: \n");
        scanf("%u", &n);
    
        do 
        {
            sum += (1/(pow(2,i)));
        } 
        while (i++ < n);
    
        printf ("The sum of the fractional exponentials from 1 ... %u is %f\n",
                n, sum);   
        system ("pause");
        return 0;
    }

  4. #4
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32

    Unhappy It's got a lot of compiling beefs with me

    There are so many I am reluctant to list them.
    I have eliminated main () from the others and just put one on the top or do I need to put main () above every new program?
    Code:
    20 syntax error before '{' token
    24 syntax error before string constant
    24 conflicting types for 'printf'
    24 a parameter list with an ellipsis can't match an empty parameter name list declaration
    24 conflicting types for 'printf'
    24 a parameter list with an ellipsis can't match an empty parameter name list declaration
    24 [Warning] data definition has no type or storage class
    25 syntax error before string constant
    25 conflicting types for 'scanf'
    25 a parameter list with an ellipsis can't match an empty parameter name list declaration
    25 conflicting types for 'scanf'
    25 a parameter list with an ellipsis can't match an empty parameter name list declaration
    25 [Warning] data definition has no type or storage class
    32 syntax error before string constant
    33 [Warning] data definition has no type or storage class
    34 syntax error before string constant
    34 [Warning] data definition has no type or storage class
    41 syntax error before string constant
    41 [Warning] data definition has no type or storage class
    42 syntax error before string constant
    42 [Warning] data definition has no type or storage class
    50 syntax error before string constant
    51 [Warning] data definition has no type or storage class
    52 syntax error before string constant
    52 [Warning] data definition has no type or storage class
    Last edited by ralphwiggam; 09-29-2006 at 12:30 PM.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    The main function is only supposed to be defined once, and you've written it three times. My suggestion is to rewrite your program anew from Quzah's outline.

    And this has been bugging me; it's int main(void) .

  6. #6
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32

    Smile Thanks you all

    I straightened it out and I appreciate you all's comments.
    I want to say more but it's just stupid sentiment.
    Thanks I have learned a lot.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > main ()
    Instead of naming this main, call this something like:
    Code:
    int calc_sum_squares(int n)
    {
    .
    .
       return sumsquare;
    }
    >int main ()
    And name this next one:
    Code:
    int calc_sum_cubes(int n)
    {
    .
    .
       return sumcubes;
    }
    Then in main you would have:
    Code:
        printf("Enter n: \n");
        scanf("%u", &n);
    
          printf ("The sum of the squares from 1..%d is %d\n", n, 
          calc_sum_squares(n));
    
          printf ("The sum of the cubes from 1..%d is %d\n", n, 
          calc_sum_cubes(n));

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Why did you send this to me as a private message? You have already had the issue solved by quaza.

    If you have a problem you send as a private message, please make sure it is unsolved in the future, then people will be able to help you better

  9. #9
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32

    Unhappy Sorry

    I am new to the forum and I was hoping for a new insight.
    I did not mean to bother you.
    I will not abuse the ettiquete of the forum, but please understand I am learning.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy String position question...
    By Striph in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 08:48 PM
  2. Seg fault in easy, easy code
    By lisa1901 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2007, 04:28 AM
  3. Java vs C to make an OS
    By WOP in forum Tech Board
    Replies: 59
    Last Post: 05-27-2007, 03:56 AM
  4. EASY GUI development.. like with Qt?
    By rezonax in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2001, 01:18 PM
  5. Sounds, or no sounds?
    By face_master in forum C++ Programming
    Replies: 3
    Last Post: 09-03-2001, 05:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21