Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ekoptur

macrumors newbie
Original poster
Feb 16, 2011
7
0
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,
 
Create a loop around argc, loop and get your values from argv, where i is the index.
 
Create a loop around argc, loop and get your values from argv, where i is the index.


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,
 
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
 
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.
 
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

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?
 
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.

Oo I see now.
Ok I think I got it.

Thank you to all of you showing me the looping and for your help.
 
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.

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
 
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


This one is abit complicated code for me I will need to try learn more about it. Thank you.
 
Oh, hogwash.

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.
 
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.

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,
 
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,

strtol does what atoi does. The reason I did not use it is this section of the atoi man page:

IMPLEMENTATION NOTES
The atoi() function is not thread-safe and also not async-cancel safe.

The atoi() function has been deprecated by strtol() and should not be used in new code.
 
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.

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
 
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

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.