findAvecode assistance

This is a discussion on findAvecode assistance within the C++ Programming forums, part of the General Programming Boards category; Hello, I am a C++ student and am practicing for C++ test. I have been stuck on a program. If ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    21

    findAvecode assistance

    Hello,

    I am a C++ student and am practicing for C++ test. I have been stuck on a program. If anyone could guide me a bit I would really appreciate it as this is my first programming class. We are studying pointers, and functions. Here info about this code and here is what I have done. We have to use this code with a function though. Thanks for any help anybody can give.

    I am trying to solve a practice program in order to prepare for a test and have been stuck on it for hours. the program has to calculate the sum over a range of integers from LOWER to UPPER and return the average value. for example if the lower is 3 and the upper is 6, then the average would be (3+4+5+6)/4 or 4.5. I have to enter the upper and lower limits using in cin, also if you could also show me how to do such a program using command line arguments I would really appreciate it. Thank You. Here is my program so far.
    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    double findAve(int lower, int upper, int array[])
    {
       double sum = 0;
       int numpoints = array[0];
            for (int i = lower; i <=upper; i++)
              {
               sum = sum + array[i]; 
               return sum/lower+upper;
              }
    }
    int main(int argc, char *argv[])
    {
        int x, y, arr[50];
        cout << " enter a lower value: ";
        cin >> x; 
        cout << " enter an upper value: ";
        cin >> y;
        cout << findAve(x, y, arr) << endl;
      
      system("PAUSE");	
      return 0;
    }[CODE]findAveCode

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > return sum/lower+upper;
    A return inside the for loop - that can't be good

    Without any error checking whatsoever, try replacing your cin with
    x = atoi( argv[1] );
    y = atoi( argv[2] );


    Then you would run the program as say
    myprog 5 9
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    21
    thanks salem, but the programs requires the use of cin statements to enter the starting lower value and ending upper value. thanks for any help you could give.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    You have 2 integers, x and y.
    The average of the sum of all integers from x and y inclusive is (x+y)/2
    The number of integers from x and y inclusive is (y-x+1), assuming y is larger than x.
    Therefore, instead of running a loop to calculate the average and sum, all you need to do is to obtain the 2 numbers, then apply the formulae.

    e.g.

    Code:
    double findAve(int lower, int upper)
    {
    	return double((lower + upper) / 2.0);
    }
    
    int findSum(int lower, int upper)
    {
    	return ((lower + upper) * (upper - lower + 1)) / 2;
    }

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    21
    how does the code look now. the teacher has not properly taught us how to return in main. so i have to return within the function. but it still won't run.

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    double findAve(int lower, int upper, int array[])
    {
    double sum = 0;
    int numpoints = array[0];
    for (int i = lower; i <=upper; i++)
        {
        sum = sum + array[i]; 
        }
        return (lower+upper)/2;
    }
    int main(int argc, char *argv[])
    {
    int x, y, arr[50];
    cout << " enter a number: " << endl;
    cin >> x;
    cout >> y;
    cout << findAve(x, y, arr) << endl;
      
      system("PAUSE");	
      return 0;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    What is the array of integers for?

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    21
    I was going to store the number between 3 and say 6 in there for some reason, which probably now doesn't make sense even to me. this my first C++ class and i am learning everything in this format, so i have to use the loops for this problem. thanks.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Originally posted by Madshan
    I was going to store the number between 3 and say 6 in there for some reason, which probably now doesn't make sense even to me. this my first C++ class and i am learning everything in this format, so i have to use the loops for this problem. thanks.
    Well yes, the most obvious idea is to use a loop.
    But when I was reading your question I noticed:
    "average would be (3+4+5+6)/4 or 4.5"
    hmmm... isnt 4.5 half of 3 added to 6?
    A little investigation and I'm convinced that this is a shortcut.

    Instead of looping through all the integers between the 2 numbers, all you need to do is add them, and divide by 2.
    As such, no array is needed.

  9. #9
    Registered User
    Join Date
    Nov 2003
    Posts
    21
    oh sorry if i cause confusion, you are right, but the for this program i have to enter ANY lower limit and upper limit. 3 and 6 was just an example. the lower limit could be 9 and upper limit a 100, so then i would would have to sum the numbers form 9 to 100 using a loop and find the average, or the lower limit could be 1 and upper limit could 10. so it has to work for any case. thanks.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Actually, it does work for any case.

    To be precise it works across the whole (positive, negative and 0) set of integers, as far as I know.

    I'll try to write a mathematical proof if you want, though my maths isnt all that good.

  11. #11
    Registered User
    Join Date
    Nov 2003
    Posts
    21
    Thank You you made a code that I thought to be trick very simple and easy. i appreciate you helping out and this board is great resource. The code worked perfectly, but say I wanted to practice it using a for loop and sum=sum + i with a function would that also be feasible? Since I want to practice programs many as many ways as possible. thanks once again.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Originally posted by Madshan
    The code worked perfectly, but say I wanted to practice it using a for loop and sum=sum + i with a function would that also be feasible? Since I want to practice programs many as many ways as possible. thanks once again.
    That's definitely a good idea, and now you can compare the results too.

    For starters, I dont think you need an extra array.
    What you do need to know is the number of integers between the lower and upper limits (inclusive), and the sum.

    You're on the right track for the sum, I think, except that you had superfluous variables, so confused yourself.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    heh, in case you're called to prove that the formula actually works:

    To prove that the average of the sum of all integers from a to b inclusive is (a+b)/2

    Find sum of all integers from a to b inclusive:
    number of elements = (b-a+1)

    sum = a + (a+1) + (a+2) + ... + [a+(b-a)]
    sum = a(b-a+1) + [0+1+2+ ... + (b-a)]
    sum = a(b-a+1) + sum from 0 to (b-a)
    sum = a(b-a+1) + sum from 1 to (b-a)

    Now, a formula for sum from 1 to n is (1/2)n(n+1)
    so
    sum = a(b-a+1) + (1/2)(b-a)(b-a+1)
    sum = (1/2)(b-a+1)(2a+b-a)
    sum = (1/2)(b-a+1)(a+b)

    But the average is sum divided by number of elements.
    Therefore,
    average = [(1/2)(b-a+1)(a+b)] / (b-a+1)
    average = (1/2)(a+b) = (a+b)/2
    (Q.E.D.)

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > thanks salem, but the programs requires the use of cin statements
    Oh, so what did you really mean by "show me how to do such a program using command line arguments" then?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Nov 2003
    Posts
    21
    oh sorry salem my fault man, i apologize, i think i actually mean meant both, but got mixed. sorry for confusion and thanks guys.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hello,i need assistance in completing the code
    By toader in forum C++ Programming
    Replies: 1
    Last Post: 06-22-2009, 03:32 AM
  2. Variable Timer assistance
    By rich2852 in forum C Programming
    Replies: 2
    Last Post: 01-21-2009, 04:43 AM
  3. Any assistance would be greatly appreciated
    By iiwhitexb0iii in forum C Programming
    Replies: 18
    Last Post: 02-26-2006, 11:06 AM
  4. Need some more assistance
    By Thantos in forum Windows Programming
    Replies: 6
    Last Post: 08-14-2003, 12:13 PM
  5. Need assistance for major text base Arena RPG project
    By Ruflano in forum C++ Programming
    Replies: 0
    Last Post: 04-04-2002, 10:11 AM

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