Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
85 views
in Technique[技术] by (71.8m points)

c - Recursion With Array

Recently i was introduced into the concept of "Recursion" and it seemed fun untill i came upon an array in which i was asked to get an array from a user with it's size and Get the negative numbers and the odd numbers Count in the array , i have thought of some ways to do this but none worked , i tried making it with different conditions or loops but everytime i found myself resettings something either negative counter or odd counter or just an infinite loop , i can't seem to understand how passing an array through this recursive procedure works for some reason it keeps giving me wrong outputs so i resetted it and started all over with the base case , Is there like a general way to follow when making recursive functions , i know its about making your problem into smaller sub problems and creating the base case but i wasn't able to figure it out here , if anyone could guide me through it would be appreciate .

Down below is the code i made for getting the array and passing it to the recursive function with the conditions that worked with the iterative function , and tried putting the base case as if (Count < 0) because Count is the size of the array so i thought about starting with it and decreasing it by one everytime i want to call the function , but that didn't seem to work either , any ideas ?

Thanks in advance !

 #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int SortArray(int* Array, int Size, int* Negative, int* Odd)
{
    if (Size < 0)   
    {
        return;
    }
            if (Array[Size] % 2 == 1 || Array[Size] % 2 == -1)
            {
                return SortArray(*Array, Size - 1, *Negative, *Odd);
            }
            if (Array[Size] < 0)
            {

            }
}
int main()
{
    int Size;
    int Negative = 0;
    int Odd = 0;
    printf("Enter The Size Of The Array: 
");
    scanf("%d", &Size);
    int* Array = malloc(Size * sizeof(int));
    if (Array ==  NULL)
    {
        printf("Malloc Failure ! 
");
        return 0;
    }
    printf("Enter The Elements Of Your Array: 
");
    for (int i = 0; i < Size; i++)
    {
        scanf("%d", &Array[i]);
    }
    SortArray(Array, Size,&Negative,&Odd);
    printf("Odd Count:%d 
", Odd);
    printf("Negative Count:%d 
", Negative);
    free(Array);
    return 0;
}
question from:https://stackoverflow.com/questions/65641315/recursion-with-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

tried putting the base case as if (Count < 0) because Count is the size of the array so i thought about starting with it and decreasing it by one everytime i want to call the function

this is a good start and the function parameters are the right ones for what you want to accomplish. the name SortArray is a bit misleading, you are not sorting anything, you are counting, I think Count would be more appropriate. also Size is not really the size, but rather our "index":

void Count(int *Array, int Index, int *Negative, int *Odd)

the base case is right, we are scanning the array backwards, so when Index is less than 0 there is nothing more left.

now, you want to update both your counters before calling the Count function again. the update part is missing from your code, that is why you get wrong results.

I would do it this way:

int n = Array[Index];       /* current number */
if (n % 2 != 0)             /* is it odd? */
        *Odd += 1;          /* if so, update odd counter by one */
if (n < 0)                  /* is it negative? */
        *Negative += 1;     /* if so, update negative counter by one */

at the end we call the Count function again decreasing Index by one, as you already guessed:

Count(Array, Index - 1, Negative, Odd);

putting all together:

void Count(int *Array, int Index, int *Negative, int *Odd)
{
        if (Index < 0)
                return;

        int n = Array[Index];
        if (n % 2 != 0)
                *Odd += 1;
        if (n < 0)
                *Negative += 1;

        Count(Array, Index - 1, Negative, Odd);
}

the function should be called the first time with the last index and counters should be set to 0 (as you already did). from main() the first call would be:

int Negative = 0;
int Odd = 0;
Count(Array, Size - 1, &Negative, &Odd);

you could make a wrapper function to "hide" this (important) particulars.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...