Here is my code. I was wanting to sum integers from the command line and give the suma s output. The code doe compile but gives me a segmentation fault if I put in more than two arguements for argc. The first arguement is the name of the program. I am not sure if I did my sum procedure correctly to make sure I did not include the first arguement(the program name) in the actual sum-value.
Any suggestions is very helpful. Thanks
#include <iostream.h>
#include <stdlib.h>
int sum(int ac, char *av[]){
int x = 0;
if (ac==0){
return 0;
}
else if (ac==1){
cout << "DONE!" << endl;
}
else {
x = (atoi(av[ac]) + sum(ac-1, av));
}
return x;
}
int main(int argc, char *argv[]){
int y=0;
y = sum(argc, argv);
cout << "Sum is" << y << endl;
return 0;
}