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 🙂
#Help! Recursive Function
284 messages · Page 1 of 1 (latest)
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.
what are you confused about?
because we aren't going to just do your homework problem for you
haha
well i started but its stuck and i thought i was right
how do i post what i did
How to Format Code on Discord
Markup
```c
int main() {}
```
Result
int main() {}
Note: Back-tick (`) not quotes (')
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);
}
}
}
You actually wrote code, that's better than most people
Or was it chatgpt
Idk doesnt matter
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
;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;
}
Program Output
output: 0
j_oe.y | 73ms | c | x86-64 gcc 13.2 | godbolt.org
it does return 0
did you change the code?
nope
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
send your bubble sort code
the example before sorted: 2 4 5 3 6 8 7 9 10 0
alright so you good?
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);
in the second for ?
i printed the array after sorting it: -858993460 0 2 3 4 5 6 7 8 9
one of them is junk value
this? 2 4 5 3 6 8 7 9 10 0
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
just do both
what do you mean do both
print before and after
send your full code
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
ur bubblesorting numbers not newArray
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
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
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.
when does this happen?
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
so the fillMaxPrefixes isn't doing what you want it to?
wait let me arrange things xD
what do you want it to do if you input "1 3 4 2 7 8 4 3 9 9"
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
haha
is that with this code?
what do you mean?. i have work with 5 questions
can u send the full code thats giving you that error
its fine juist send it
thats what discord sayin
u can send it as a file
wont let me send
ok
no issue with mine
no issue when i use valgrind either
what compiler are you using?
okay so what is the goal of your assignment
i haven't even read it
im just trying to help you fix errors
its not in english
hebrew?
yea
nah it was for my bar mitzvah
okay let me read ur code give me a sec
fun
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 🙂
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
but now the problem is that the countSmaller is shity
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
so u want to print the n amount of numbers less than or equal to num between start and end in the array?
i dont know if less and equal or just less
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
i check from a sorted array
and i need to check it in binary search
and it need to be recursive
here yea 5
ok ill read ur code
thank you man for your time
u dont need to do recursion
❤️
for this function
okay ill try
ok take ur time bro
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 ?
here 7 is good
okay so whats the issue?
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
yea 1 sec lemme check with test txt
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
sorted: 0 2 3 4 5 6 7 8 9 10
they never mentioned it in the question that wierd of them
why are you checking the middle ?
this is baniry search
with my function i got 3
and if the middle is already the number great
but i didnt use recursion lol
what do you mean with ur function
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;
}
because this is how binary search working
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
you take the first and last index and devide by 2
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;
}
ok bro
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
first line u mean?
Ye
what's the last test case
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
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?
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
you have this ```C
if (start >= end)
return 0;
is start == end
yes
so it will return 0
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?
this is a unique situation
so you do need to make a seperate if statement
BEFORE , if start >= end return 0;
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
if((start == end) && start < num) return 1;
2 base case in recursive?
yep
dam they went hard on us
yh