views:

213

answers:

5

The program is supposed to calculate the number of arguments, iterate over the list of arguments, for each argument convert the argument to an integer and copy it to an array, iterate over the elements of the array, adding the value of each one to a variable (this computes the sum of elements), and print the sum. There will not be more than 15 arguments. So far I have:

int sumofA (int sizeofA, int x, int y){  
  int i = sizeofA;  
   if (i <= 15){  
      int z = x + y;  
      return z;  
   }  
}  

int main (int argc, char*argv[]){    
   int sizeofA = argc - 1;  
   int i = 1;  
   while (i <= sizeofA){  
      int x = GetInt (argc, argv, i);  
      i = i + 1;  
      int y = GetInt (argc, argv, i);     
      printf ("%d\n", sumofA (sizeofA, x, y));
   }  
   return 0;  
}

Ok, now (when given three arguments other than ./a) it prints the sum of the first argument and the second argument...then the second and the third...and then the value of the third argument. Why?

Here's the code for GetInt (I have to use this):

int GetInt (int argc, char * argv[], int i) {  
   if (i < 0 || i >= argc) return 0;  
   return atoi(argv[i]);  
}

Do I need to go through and assign each argument to an integer (ex. int z = GetInt (argc, argv, i + 2) )?

+3  A: 

Your problem is here:

 int x = GetInt (argc, argv, 1);
 int y = GetInt (argc, argv, i);

As you go through the loop the first time, what is i, and what does it print out? What about the second time you go through the loop?

Tom Ritter
+3  A: 

For starters, you are returning 0 after the first iteration of your loop.

jthompson
+6  A: 

There are a couple of this going wrong here:

  • i is not defined in sumofA, so comparing with it yield undefined behavior.
  • If i >= 15, then it's unclear what sumofA will return.
  • You return inside the loop; surely that's not what you want.
  • Your code is not actually storing anything in the array A.

Please compile your code with all warning-flags on (gcc: -Wall -Werror -pedantic), and make sure there are no warnings when your code compiles.

Note that the variable size is unneeded: use while (i < argc).

Edit: Now that you added the code of GetInt, replace

GetInt (argc, argv, i);

with

atoi(argv[i]);

There is no use for GetInt, so you can remove it completely.

Lastly: in your question you mention storing the numbers in an array and then summing them. Do you want to do that, or need to do that (due to some assignment?) Because it's not necessary: simply add the result of all calls to atoi. The array A is then superfluous.

Edit 2: I see you fixed the code in some places. Your code currently (23:31 CEST) adds the first argument to each of the arguments separately and prints them. You're not quite there yet. Good luck!

Stephan202
+1 for taking the time to enumerate all those problems. I really didn't know where to start :)
SpoonMeiser
And the list was/isn't even complete... *sigh* :)
Stephan202
I have to use GetInt, along with storing numbers in an array and summoning them for the assignment.
Kaity
Ok, then you have no choice. If I may ask, which book/institution/person gave you this assignment? Reason I'm asking is that I find the signature of GetInt somewhat strange.
Stephan202
It's from my professor, he doesn't really go by the book at all, that's why I have to get help from everyone on here alot.
Kaity
A: 

The first time through your loop x and y are both assigned the same value (since i=1), their sum is printed out, and then the function returns which ends your program. So you'll just be getting the first value added to itself.

Goog
+1  A: 

Since you want formatting help...

First off, C has more assignment operators than just =, and these are great for things like x = x + 1. Most languages, C included, provide a += operator, which looks like this: x += 1. That does the same thing as x = x + 1, and is nicer. Syntaxtic sugar, but sugar is tasty.

While we're at it, C and many other languages provide a special case for += 1 - the ++ operator from which C++ gets its name. So really, x = x + 1 can be rewritten as x++ with the exact same effect. Note, however, that there is a major difference between x++ and ++x, but that doesn't matter currently. For now, use whichever one you want, and make sure to put it on its own line all by itself until you learn the difference between x++ and ++x.

If you find you have a variable you're using once, don't use it. Instead of this:

int sizeofA = argc - 1;
...
while (i <= sizeofA){
  ...
  printf ("%d\n", sumofA (sizeofA, x, y));
}

Try this:

...
while (i <= argc - 1){
  ...
  printf ("%d\n", sumofA (argc - 1, x, y));
}

Or (probably better):

...
while (i < argc){
  ...
  printf ("%d\n", sumofA (argc - 1, x, y));
}

Third off, you should not be blindly passing it your entire arguments list, only the argument you want to convert at this current time, but that's another issue, and I suppose you can't change GetInt(). This seems like a battle for another day.

(Fourth off, why is this a Community Wiki question?)

Fifthly, I think your logic is off - if you start at 1, I don't think you want to stop 1 before the end of the list. But this is a simple off-by-one error and you can fix that later when you know for sure that that is, in fact, the error.

Lastly, I suggest you define a variable, int answer, and, instead of printf()ing all the elements of the list, simply go through the list, add the numeric value of each one to answer, an then, at the end, print out answer. But maybe I'm missing the point of the assignment. Alternatively, each time you iterate through the loop, print out answer, so you can see the value grow correctly each time. I think this is closer to what you actually want to do in the assignment, but I can't take time to check because I have a class to go to.

Chris Lutz