C++ average small coding problem

Discussion in 'Mac Programming' started by ekoptur, Feb 16, 2011.

  1. ekoptur macrumors newbie

    Joined:
    Feb 16, 2011
    #1
    I am having problem by putting more integers on this. I can type ./a.out 3 15 and it find 9 which is correct but I can only put 2 numbers. How can i change this to put more than 2 number and find the average? for ex. ./a.out 3 15 17 19 24.

    Code:
    ///////    newAverage.C    ///////
    #include  <iostream>
    
    int main (int argc,  char* argv[])
    {    if ( argc != 3 )
         {   std::cerr << "Usage: " << argv[0] << " integer "
                       << std::endl;
             exit(1);
         }
    
         int a = atoi(argv[1]), b = atoi(argv[2]);
    
         float z = (a + b)/2.0;
         std::cout << "a is " << a
               << " and b is " << b << std::endl;
         std::cout << "average is " << z << std::endl;
         return 0;
    }
    
    Thank you,
     
  2. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #2
    Create a loop around argc, loop and get your values from argv, where i is the index.
     
  3. ekoptur thread starter macrumors newbie

    Joined:
    Feb 16, 2011
    #3


    I am very sorry to bother you, but I am new on C++. Can you show me how to loop it? I understand the part about the values.

    Thank you,
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    Right now you have an explicit check for 2 arguments. So that has to go. The only problem would be 0 arguments, so you can compare argc to 1 and print an error in that case and quit.

    From there, you need to loop from 1 to argc - 1:
    Code:
    long int sum = 0;
    std::cout << "The average of: ";
    for(int pos = 1; pos < argc; pos ++) {
      //Some error checking ought to go on here.
      if(pos != 1) std::cout << ", ";
      std::cout << argv[pos];
      sum += strtol(argv[pos],NULL,10);
    }
    double average = sum / (argc - 1.);
    std::cout << " is: " << average << std::endl;
    
    -Lee
     
  5. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #5
    Here's the most simple example building on what you had, if you want to present all input then you need a loop when printing as well.


    Code:
    ///////    newAverage.C    ///////
    #include  <iostream>
    
    int main (int argc,  char* argv[])
    {
            if ( argc < 3 )
            {
                    std::cerr << "Usage: " << argv[0] << " integer " << std::endl;
                    exit(1);
            }
    
            int i, sum = 0;
            float avg;
    
            for( i = 1; i < argc; i++ ) {
                    sum += (int)strtol(argv[i], NULL, 0);
            }
    
            avg = ((float)sum/(argc -1));
    
            std::cout << "avg: " << avg << std::endl;
    
            return 0;
    }
    I used strtol here, but you can just replace that with atoi.
     
  6. ekoptur thread starter macrumors newbie

    Joined:
    Feb 16, 2011
    #6
    hmm si I put it like this ,
    Code:
    /////    newAverage.C    ///////   
    #include  <iostream>
          
    int main (int argc,  char* argv[])  
    {    if ( argc != 3 )
         {   std::cerr << "Usage: " << argv[0] << " integer "
                       << std::endl;
             exit(1);
         }
    
         long int sum = 0;
         std::cout << "The average of: ";
         for(int pos = 1; pos < argc; pos ++) {
        
         if(pos != 1) std::cout << ", ";
         std::cout << argv[pos];
         sum += strtol(argv[pos],NULL,10);
         }
         double average = sum / (argc - 1.);
         std::cout << " is: " << average << std::endl;
         }
    But I am still getting the same problem?
     
  7. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #7
    What does this line do? Do you see it in Lee or subsonix's code?

    B
     
  8. ekoptur thread starter macrumors newbie

    Joined:
    Feb 16, 2011
    #8
    Oo I see now.
    Ok I think I got it.

    Thank you to all of you showing me the looping and for your help.
     
  9. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    Oh, hogwash.

    Code:
    #include  <iostream>
    #include  <errno.h>
    
    int main (int argc,  char* argv[])
    {
      if(argc <= 1) {
        std::cout << "Please enter arguments to average." << std::endl;
        return -1;
      }
    
      long int sum = 0;
      std::cout << "The average of: ";
      for(int pos = 1; pos < argc; pos ++) {
        long int tmp = strtol(argv[pos],NULL,10);
        if(tmp == 0 && EINVAL == errno) {
          std::cerr << std::endl << "Invalid value " << argv[pos] << " entered." << std::endl;
          continue;
        }
        if(pos != 1) std::cout << ", ";
        std::cout << argv[pos];
        sum += tmp;
      }
      double average = sum / (argc - 1.);
      std::cout << " is: " << average << std::endl;
      return 0;
    }
    
    -Lee
     
  10. ekoptur thread starter macrumors newbie

    Joined:
    Feb 16, 2011
    #10

    This one is abit complicated code for me I will need to try learn more about it. Thank you.
     
  11. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #11
    Excuse me? What is hogwash exactly, that you need a loop to present the args or that it's the most simple example (perhaps not in an absolute sense, but that's clearly not what I meant).

    I saw the reply, went ahead and made an example. It has nothing to do with your post.
     
  12. ekoptur thread starter macrumors newbie

    Joined:
    Feb 16, 2011
    #12
    I am sorry to bother you again.

    I have one more question. I changed the strtol with atoi and it didnt work. Do you know the reason why? with strtol, it works perfect but when I type atoi instead of strtol, it doesnt work. Could you tell me what strtol does?

    Thank you,
     
  13. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #13
    strtol does what atoi does. The reason I did not use it is this section of the atoi man page:

     
  14. ekoptur thread starter macrumors newbie

    Joined:
    Feb 16, 2011
    #14
    ok :) thank you for helping me. I still have alot to learn. good luck to me:D
     
  15. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #15
    Maybe a smiley would have been appropriate? I just meant you don't need a separate loop to display the args since you're already looping to sum. No harm intended, it was meant in good humor.

    -Lee
     
  16. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #16
    Ok, it's all good. I just made the most simple example in a few minutes showing how to loop around argc, as requested. I excluded any detailed output, and explained that a loop is required to present argv in it's entirety. No, it doesn't need to be a separate loop.
     

Share This Page