-6

I have here my code where i want it to add all the elements that are even and positive but i doesnt seem to be working

int a[10], n;
int add (int n, int a[10])
{
    if(a[n]%2==0 && a[n]>0) a[n]+add(n-1,a);
    else return("return 0");

}
akz92
  • 1
  • 1
  • 4
    What is `return("return 0")` supposed to do? And what do you return if the condition is true? Perhaps you need to refresh some of the basics of C in your text-books? – Some programmer dude Jun 03 '21 at 09:51
  • 1
    Also you seem to be declaring *two* sets of variables named `a` and `n`. One pair in the global scope and one pair as arguments to the function. These two pairs are *separate*, *distinct* and *different* variables without any relationship. – Some programmer dude Jun 03 '21 at 09:53
  • i didnt post the entire code, but n is needed – akz92 Jun 03 '21 at 09:54
  • By the way there's no bounds-checking. Unless `a[0] <= 0` is guaranteed then you *will* go out of bounds with a negative index. – Some programmer dude Jun 03 '21 at 09:57
  • 1
    And please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mcve], and [edit] your question to show it together with a description of the problem you have. – Some programmer dude Jun 03 '21 at 09:59
  • so basically i cant help you go research? – akz92 Jun 03 '21 at 10:01
  • The advice to go a read a book is excellent advice - it's very difficult to learn C by guessing and then debugging as the language is more subtle and difficult than it appears, and it's very easy to mislearn, especially as completely incorrect programs can often appear to work. Reading a good introductory book will get your foundations right. You'll also experience friction on stack-overflow until you learn to ask questions in the way encouraged by the help pages that were given. Welcome to stackoverflow :) – Paul Hankin Jun 03 '21 at 10:12

1 Answers1

1

This is a corrected version of your code.

int add(int n, int a[10]) {
    if (n < 0) return 0;
    if (a[n] % 2 == 0 && a[n] > 0) return a[n] + add(n-1, a);
    else return add(n-1, a);
}

Note that the correct way to call the function for an array of length 10 is add(9, a); (ie: one less than the length).

Here's how I would write the code:

int add(const int *a, size_t a_len) {
    int sum = 0;
    for (size_t i = 0; i < a_len; i++)
        if (a[i] % 2 == 0 && a[i] > 0) sum += a[i];
    return sum;
 }

It's a lot easier to understand, it follows the standard C convention of passing in the length (rather than the length minus 1), uses size_t rather than int for the length of the array, and correctly labels the input array as const. I'd guess that it runs faster (although the compiler may figure out how to turn your recursive version into the faster iterative one, especially at higher optimization levels).

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Paul Hankin
  • 54,811
  • 11
  • 92
  • 118
  • 2
    You should totally go and read a book though. :) There's a good list here: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Paul Hankin Jun 03 '21 at 10:06
  • @akz92 Learning programming languages is like learning *any* language, written or spoken. It takes effort and one can't really doing it by just guessing and hoping for the best. I have been programming in C and similar languages for over 30 years, and still don't consider myself finished learning. – Some programmer dude Jun 03 '21 at 11:04