#Help! Recursive Function

284 messages · Page 1 of 1 (latest)

high sleet
#

Hello i need to write recursive Function that get a sorted array of integers. and index of the start and last on the array and a number. i need to do a binary search and find the number on the array or if the number is not in the array then the most close number to the number and what i need to return is the number of numbers that is before the number we need to find. for example for the array : 0 2 3 4 5 6 7 8 9 10. if i want to find 5 the function return 4 because it have 4 numbers before him in the array. please help me i need to hand it soon haha. thanks in advance 🙂

cedar sailBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

odd lava
#

because we aren't going to just do your homework problem for you

high sleet
#

haha

#

well i started but its stuck and i thought i was right

#

how do i post what i did

cedar sailBOT
#
How to Format Code on Discord
Markup

```c
int main() {}
```

Result
int main() {}
high sleet
#
int countSmaller(int arr[], int start, int end, int num)
{
    if (start > end)
        return 0;
    else
    {
        int mid = (start + end) / 2;
        if (arr[mid] == num)
        {
            return mid;
        }
        else if (arr[mid] < num)
        {
            if (arr[mid + 1] >= num)
            {
                return mid;
            }
            return countSmaller(arr, mid + 1, end, num);

        }
        else if (arr[mid] > num)
        {
            if (arr[mid - 1] <= num)
            {
                return mid;
            }
            return countSmaller(arr, start, mid - 1, num);

        }
        
    }
}
solemn hearth
#

You actually wrote code, that's better than most people

#

Or was it chatgpt

#

Idk doesnt matter

high sleet
#

i have a test txt and for the example: arr: 0 2 3 4 5 6 7 8 9 10. start = 0. end = 9. number = -5. it should return 0 but in my program it return 1

#

i actually wrote it alone xD

odd lava
#

;compile

#include <stdio.h>
#include <stdlib.h>

int countSmaller(int arr[], int start, int end, int num)
{
    if (start > end)
        return 0;
    else
    {
        int mid = (start + end) / 2;
        if (arr[mid] == num)
        {
            return mid;
        }
        else if (arr[mid] < num)
        {
            if (arr[mid + 1] >= num)
            {
                return mid;
            }
            return countSmaller(arr, mid + 1, end, num);

        }
        else if (arr[mid] > num)
        {
            if (arr[mid - 1] <= num)
            {
                return mid;
            }
            return countSmaller(arr, start, mid - 1, num);

        }
        
    }
}
int main() {
int arr[10] = {0 ,2, 3, 4 ,5 ,6 ,7, 8, 9, 10};
printf("output: %d\n", countSmaller(arr, 0, 9, -5));
return 0;
}
sturdy ploverBOT
#
Program Output
output: 0
high sleet
#

did you change the code?

odd lava
#

nope

high sleet
#

oh i think i know what is the problem

#

the sorted array i was needed to sort it with bubble sort

#

and now i see that one of the values is still junk value

odd lava
#

send your bubble sort code

high sleet
#

the example before sorted: 2 4 5 3 6 8 7 9 10 0

odd lava
#

alright so you good?

high sleet
#
void BubbleSort(int arr[], int size)
{
    int i, j, temp;
    for (i = 0; i < size - 1; i++)
    {
        for (j = 0; j < size - i - 1; j++)
        {
            if (arr[j] > arr[j + 1])
            {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j + 1] = temp;

            }
        }
    }
}
#

in the main i called it : BubbleSort(numbers, 11);

odd lava
#

looks good to me

#

wait why are you doing size -1 for i

high sleet
#

in the second for ?

odd lava
#

actually it doesn't matter

#

they both are right

high sleet
#

i printed the array after sorting it: -858993460 0 2 3 4 5 6 7 8 9

#

one of them is junk value

odd lava
#

this? 2 4 5 3 6 8 7 9 10 0

high sleet
#

yea

#

maybe i exceed the range of the array?

odd lava
#

using your code i got

#

0 2 3 4 5 6 7 8 9 10

#

print the array before sorting it

#

make sure you are reading it in right

high sleet
#

before i sort it print ok

#

after no

odd lava
#

just do both

high sleet
#

what do you mean do both

odd lava
#

print before and after

high sleet
#

i did haha

#

before it print ok. after i do the sort bubble it print with junk value

odd lava
#

send your full code

high sleet
#

its long xD

#

its telling me the code is long and i cant write it haha

#

because its a lot of functions in this code its multiple task and im on the last questionn

odd lava
#

ur bubblesorting numbers not newArray

high sleet
#

yea i sent numbers

#

newarray is for other function

odd lava
#

you are printing newArray before bubblesorting

#

print numbers before and after sorting

high sleet
#

i deleted it already

#

i checked it bro

#

and before it was ok. and after the bubble sort one of the values was junk value

odd lava
#

theres nothing wrong with your bubblesort algorithm, the only issue is if you have a garbage value in your array prior to sorting

#
#include <stdio.h>

void BubbleSort(int arr[], int size)
{
    int i, j, temp;
    for (i = 0; i < size - 1; i++)
    {
        for (j = 0; j < size - i - 1; j++)
        {
            if (arr[j] > arr[j + 1])
            {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j + 1] = temp;
            }
        }
    }
}

int main()
{
    int arr[10];
    
    for (int i = 0; i < 10; i++)
    {
        scanf("%d", &arr[i]);
    }
    
    printf("before\n");
    for(int i = 0; i < 10; i++) {
        printf("%d ", arr[i]);
    }
    
    BubbleSort(arr, 10);
    printf("after\n");
    for(int i = 0; i < 10; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
#

see, no problems

high sleet
#

thats wierd 😦

#

i also have other problem like error code 2

#

man i suck

#

Run-Time Check Failure #2 - Stack around the variable 'newArr' was corrupted.

high sleet
#

because this function i believe

#
void fillMaxPrefixesArray(int numbers[], int n, int maxPrefixesArray[])
{
    if (n == 0)
    {
        maxPrefixesArray[0] = numbers[0];
    }
    else
    {
        fillMaxPrefixesArray(numbers, n - 1, maxPrefixesArray);
        if (numbers[n] > maxPrefixesArray[n - 1])
        {
            maxPrefixesArray[n] = numbers[n];
        }
        else
        {
            maxPrefixesArray[n] = maxPrefixesArray[n-1];
        }

    }
}
#

which is basicly working but

#

in the end i get error code

#

what it do for example this arr: 1 3 4 2 7 8 4 3 9 9 it put in new arr this: 1 3 4 4 7 8 8 8 9 9

#

btw thank you for trying to help much appricated

odd lava
#

so the fillMaxPrefixes isn't doing what you want it to?

high sleet
#

wait let me arrange things xD

odd lava
#

what do you want it to do if you input "1 3 4 2 7 8 4 3 9 9"

high sleet
#

for this input 1 4 2 3 2 9 1 3 5 6. it will put in newarray like this: 1 4 4 4 4 9 9 9 9 9

#

which i did and its working

#

it print it ok

#

but when i finish the code i get this message let me picture for you

odd lava
#

oh you speak hebrew?

#

that's cool

high sleet
#

haha

odd lava
high sleet
odd lava
#

can u send the full code thats giving you that error

high sleet
#

yea i want to send my full code for you to see

#

but its to long for here

odd lava
#

its fine juist send it

high sleet
#

thats what discord sayin

odd lava
#

u can send it as a file

high sleet
#

wont let me send

odd lava
#

copy your code to a .txt

#

then upload the .txt

high sleet
#

like this ok ?

odd lava
#

yes

#

it's fine

high sleet
#

ok

odd lava
#

no issue with mine

#

no issue when i use valgrind either

#

what compiler are you using?

high sleet
#

is the number is 7 tho ?

#

i use visual studio

odd lava
#

is that what you want it to be?

#

7?

high sleet
#

i think it should return 5 xD

#

rip

odd lava
#

okay so what is the goal of your assignment

#

i haven't even read it

#

im just trying to help you fix errors

high sleet
#

its not in english

odd lava
#

hebrew?

high sleet
#

yea

odd lava
#

i can only read hebrew with vowels lol

#

but i can't understand it

high sleet
#

xD

#

smartie

odd lava
#

nah it was for my bar mitzvah

high sleet
#

haha nicee

#

Alia La Tora

odd lava
#

okay let me read ur code give me a sec

high sleet
#

basicly 5 diffrent functions

#

recursive

odd lava
#

fun

high sleet
#

i need to write recursive Function that get a sorted array of integers. and index of the start and last on the array and a number. i need to do a binary search and find the number on the array or if the number is not in the array then the most close number to the number and what i need to return is the number of numbers that is before the number we need to find. for example for the array : 0 2 3 4 5 6 7 8 9 10. if i want to find 5 the function return 4 because it have 4 numbers before him in the array. please help me i need to hand it soon haha. thanks in advance 🙂

odd lava
#

well i see one issue

#

ur passing in "10" as the size

#

for the function

#

in C arrays start at 0

#

so when you do numbers[n]

#

you are going out of bounds

#

aswell as maxPrefixesArray[n]

#

to fix it

#

just pass 9

#

in the main function

high sleet
#

wow nice

#

ty

#

haha

odd lava
#

im still getting 7 though

#

so let me read more of your code

high sleet
#

but now the problem is that the countSmaller is shity

odd lava
#

ye

#

what do you want this function to do exactly?

high sleet
#

ok ill try to say it clear haha

#

i basicly send this recursive function a sorted array the first ind and the last ind of the array and also i send a number

#

what i want the function to do is to find the the number or the closest number to number if not in the array and return how many numbers came before in the array

#

let me include some examples:

#

arr = [1,3,4,7,8,13] , start = 0, end = 5, num = 8
for this the function should return 4 because 1 , 3 , 4 , 7 smaller then 8

#

and here in this example:
arr = [1,3,4,7,8,13] , start = 0, end = 5, num = 5
the function should return 3 because 1 , 3 , 4 are smaller then the number 5

odd lava
#

so u want to print the n amount of numbers less than or equal to num between start and end in the array?

high sleet
#

i dont know if less and equal or just less

odd lava
#

so in this array
1 4 2 3 2 9 1 3 5 6 - unsorted
1 4 4 4 4 9 9 9 9 9 - sorted (WRONG THIS IS A DIFF ARR)
start: 0, end: 9, num:5

#

do you check from the unsorted

#

or sorted

high sleet
#

i check from a sorted array

#

and i need to check it in binary search

#

and it need to be recursive

odd lava
#

so you want to check that whole array until you get less than 5

#

so it should be 5

high sleet
#

here yea 5

odd lava
#

ok ill read ur code

high sleet
#

thank you man for your time

odd lava
#

u dont need to do recursion

high sleet
#

❤️

odd lava
#

for this function

high sleet
#

but i have to this is the assaignment

#

rip

odd lava
#

okay ill try

high sleet
#

ok take ur time bro

odd lava
#

when u input 1 4 2 3 2 9 1 3 5 6

#

sorting it you get 1 1 2 2 3 3 4 5 6 9

#

so 7 is correct

#

are u sure you want to use numbers arr and not newArr arr ?

high sleet
#

lemme check again

#

its on numbers because they ask to bubble sort it

odd lava
#

so how would you get 5?

#

it's 7

high sleet
#

here 7 is good

odd lava
#

okay so whats the issue?

high sleet
#

1 1 2 2 3 3 4 5 6 9

#

because 7 numbers are coming before 5

odd lava
#
Enter a number and a digit Odd
The sum of digits is 15
The string is 24522
Enter 10 numbers
1 4 4 4 4 9 9 9 9 9 
Enter limits and a number
Num smaller is 7
#

7 is correct then

#

the array you are printing on line 5

#

is NOT numbers

#

you are printing newArr on line 5

high sleet
#

yea 1 sec lemme check with test txt

odd lava
#

its this

#
24522 2
1 4 2 3 2 9 1 3 5 6
0 9 5
high sleet
#

in this case:

#

2 4 5 3 6 8 7 9 10 0
1 7 5

#

i get num smaller is 4. and in test file it say it need to print num smaller is 3

odd lava
high sleet
#

sorted: 0 2 3 4 5 6 7 8 9 10

odd lava
#

0 2 3 4 are less than 5

#

that's 4 numbers

high sleet
#

yea but now i peeped

#

that they saying from index 1 to 7

odd lava
#

okay i see

#

give me a sec

high sleet
#

they never mentioned it in the question that wierd of them

odd lava
#

why are you checking the middle ?

high sleet
#

this is baniry search

odd lava
#

with my function i got 3

high sleet
#

and if the middle is already the number great

odd lava
#

but i didnt use recursion lol

high sleet
#

what do you mean with ur function

odd lava
#
int countSmaller(int arr[], int start, int end, int num)
{
    int ret = 0;
    for(int i = start; i < end; i++) {
        if(arr[i] < num) {
            ret++;
        }
    }

    return ret;
}
high sleet
#

oh

#

😦

odd lava
#

i see why you divided by 2

#

this is how i would do it

high sleet
#

because this is how binary search working

odd lava
#

recursively call countSmaller on the left and right halfs
combine the counts from left and right halfs
check if the current element is smaller than num

high sleet
#

you take the first and last index and devide by 2

odd lava
#

try this

int countSmallerRecursive(int arr[], int start, int end, int num) {

    if (start > end)
        return 0;
    int mid = start + (end - start) / 2;

    int leftCount = countSmallerRecursive(arr, start, mid, num);
    int rightCount = countSmallerRecursive(arr, mid + 1, end, num);

    int totalCount = leftCount + rightCount;

    if (arr[mid] < num)
        totalCount++;

    return totalCount;
}
high sleet
#

ok bro

odd lava
#

start >= end

#

actually

#

so you dont go out of bounds

#

don't forget that arrays are 0 bounded meaning the last index is size -1

high sleet
#

first line u mean?

odd lava
#

Ye

high sleet
#

i think it worked

#

last test didnt work

odd lava
#

what's the last test case

high sleet
#

input = 24502 2
2 4 5 3 6 8 7 9 10 0
3 3 5
output = Enter a number and a digit
Even
The sum of digits is 13
The string is 24502
Enter 10 numbers
2 4 5 5 6 8 8 9 10 10
Enter limits and a number
Num smaller is 1

#

i got 0 instead of 1

odd lava
#

and you want 0 ?

#

oh

high sleet
#

from the test file it should be 1

#

and i got 0

odd lava
#

0 2 3 4 5 6 7 8 9 10

#

start at index 3

#

end at index 3

#

think about it, how would you fix this issue?

high sleet
#

my brain is not working atm its 6 in the morning

#

and im a wake from yesterday trying to do this shit haha

#

well its on 4 and the number is 5 and it exist

odd lava
#

you have this ```C
if (start >= end)
return 0;

#

is start == end

#

yes

#

so it will return 0

high sleet
#

yea

#

so no good =

#

?

odd lava
#

no

#

you need it

#

or you will seg fault

#

i know the solution but im trying to help you xD

#

how could you fix this?

high sleet
#

yea i know bro

#

maybe add a && somthing

#

cant think anymore rip

odd lava
#

this is a unique situation

#

so you do need to make a seperate if statement

#

BEFORE , if start >= end return 0;

high sleet
#

thats nice u understand so good

#

wish i could be good

#

like you

#

and i really dont know haha 6 in the morning im awake 24 hours re

odd lava
#

if((start == end) && start < num) return 1;

high sleet
#

2 base case in recursive?

odd lava
#

yep

high sleet
#

dam they went hard on us

odd lava
#

there might be another way to do it

#

but thats how i would

high sleet
#

is it ok like this ?

odd lava
#

yh

high sleet
#

nice u nailed it

#

oh wait

#

now test num 1 is crazy and not right

#

2 4 5 3 6 8 7 9 10 0
0 9 5

#

instead of 4 it return 9

#

0 2 3 4 5 6 7 8 9 10

odd lava
#

@high sleet

#

did u figure this out yet

#

i fell asleep lol