I'm a newbie both here in stackoverflow and both in the world of programming.
Today i was solving some exercise about recursion, and one of these asked to write a recursive function for finding minimum element of an array.
After many tries, I have finally wrote this working code, but i want to ask you if this is a "good" code. I mean, the fact it's working aside, is it written well? There's something that should be changed? And, above all, there's a way to make this functions working well without declaring that global int "min"? :)
Here's the code:
#include <stdio.h>
int recursiveMinimum(int array[], size_t size);
int min = 1000;
int main(void) {
int array[] = {55, 5, 1, 27, 95, 2};
printf("\nMinimum element of this array is: %d\n\n",
recursiveMinimum(array, 6));
}
int recursiveMinimum(int array[], size_t size) {
if (size == 1) {
return min;
} else {
if (array[size] <= min) min = array[size];
return min = recursiveMinimum(array, size - 1);
}
}
It is a bad idea when a function depends on a global variable.
But in any case your function is incorrect and invokes undefined behavior.
In the first call of the function this if statement
if (array[size] <= min) min = array[size];
trying to access memory outside the passed array because the valid range of indices is [0, size).
Also the array can contain all elements greater than the initial value of the global variable
int min = 1000;
And the function may not be called a second time because the value of the variable min is unspecified.
The function should return the index of the minimal element in the array. In general the user can pass the second argument equal to 0. In this case again the function will invoke undefined behavior if you will try to return a non-existent element of an empty array.
The function can be declared and defined the following way
size_t recursiveMinimum( const int a[], size_t n )
{
if ( n < 2 )
{
return 0;
}
else
{
size_t min1 = recursiveMinimum( a, n / 2 );
size_t min2 = recursiveMinimum( a + n / 2, n - n / 2 ) + n / 2;
return a[min2] < a[min1] ? min2 : min1;
}
}
Here is a demonstration program
#include <stdio.h>
size_t recursiveMinimum( const int a[], size_t n )
{
if (n < 2)
{
return 0;
}
else
{
size_t min1 = recursiveMinimum( a, n / 2 );
size_t min2 = recursiveMinimum( a + n / 2, n - n / 2 ) + n / 2;
return a[min2] < a[min1] ? min2 : min1;
}
}
int main( void )
{
int a[] = { 55, 5, 1, 27, 95, 2 };
const size_t N = sizeof( a ) / sizeof( *a );
size_t min = recursiveMinimum( a, N );
printf( "\nMinimum element of this array is: %d at the position %zu\n",
a[min], min );
}
The program output is
Minimum element of this array is: 1 at the position 2
Pay attention to that the first parameter has the qualifier const because the passed array is not being changed within the function. And to decrease the number of recursive calls the function calls itself for two halves of the array.
Recursion works by reducing the size at the call to the next iteration and comparing the result of the call with the current value and return the lower of the 2.
As recursion stop you can simply return the first element
int recursiveMinimum(int array[], size_t size) {
if (size == 1) return array[0];
int min_of_rest = recursiveMinimum(array, size - 1);
if (array[size - 1] <= min_of_rest) return array[size - 1];
return min_of_rest;
}
Full example: https://godbolt.org/z/sjnh8sYz3
In the past, we used to implement it with pointers, KR-C style.
Using pointers in a harsh way was a mean to deal with inefficiency of compilers at that time.
Not sure it is considered good practice now. An example is provided hereafter.
Anyway, it would be better (easier and more efficient) to implement it in a non recursive manner.
#include <stdio.h>
void recursiveMinimum(int *array, size_t size, int *min) {
if (size == 0) return;
if (*array < *min) *min = *array;
recursiveMinimum (array+1, size-1, min);
return;
}
int main(void) {
int array[] = {55, 5, 1, 27, 95, 2};
size_t size = sizeof(array)/sizeof(*array);
int min = array[0];
recursiveMinimum (array, size, &min);
printf("\nMinimum element of this array is: %d\n", min);
return 0;
}
Related
#include <stdio.h>
#include <math.h>
int prod(int arr[], int n) {
if (arr[n-1] < 0) {
return 1;
}
return ((arr[n-1]) * (prod(arr[n-2] , n)));
}
int main( int argc, char* args[] ) {
int arr[] = {2 , 3};
printf("%d" , prod(arr , 2));
}
i keep getting pointer related errors but have no idea what to change, any help? The code is supposed to use the recursive function to get the product of all integers equal or over 0 in the array.
I guess the condition for termination of recursion should be:
if (n-1 < 0) {
return 1;
}
It can be reformulated as:
if (n <= 0) {
return 1;
}
What means that product of entries of empty array is 1.
Moreover, expression that goes deeper should be:
return arr[n-1] * prod(arr , n - 1);
The final code could be:
int prod(int arr[], int n) {
if (n == 0)
return 1;
return arr[n-1] * prod(arr, n - 1);
}
There should be assignment in the prod function with something like this:-
n=n-1
You are not decreasing the value of n anywhere.
This function is incorrect.
int prod(int arr[], int n) {
if (arr[n-1] < 0) {
return 1;
}
return ((arr[n-1]) * (prod(arr[n-2] , n)));
}
For starters this if statement
if (arr[n-1] < 0) {
return 1;
}
does not make a sense.
It seems you mean
if ( n-1 < 0) {
return 1;
}
In this expression
prod(arr[n-2] , n)
the first argument is a scalar object of the type int. That is arr[n-2] is an element of an array. Also the second argument shall be decremented that is you have to use the expression n - 1.
But apart from this there are several other drawbacks.
For starters a product of elements of an array can be too big to fit in an object of the type int, You should use at least the type long long int as the return function type or maybe the float type long double.
Secondly as the array is not being changed within the function then the first parameter should have the qualifier const.
The user can pass as second argument the value 0. In this case it will look strange that the function returns 1.
Thus the function should look at least like
long long int prod( const int arr[], size_t n )
{
return n == 0 ? 0 : ( n == 1 ? arr[n-1] : arr[n-1] * prod( arr, n - 1 ) );
}
Here is a demonstrative program
#include <stdio.h>
long long int prod( const int arr[], size_t n )
{
return n == 0 ? 0 : ( n == 1 ? arr[n-1] : arr[n-1] * prod( arr, n - 1 ) );
}
int main(void)
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
printf( "%lld\n" , prod( arr , sizeof( arr ) / sizeof( *arr ) ) );
return 0;
}
The program output is
3628800
I'm trying to make a function that identifies the maximum value in an array and calculate the sum of each time it appears. That's fine but the problem is that I need to make the function args the size of the array and the array itself.
This is what I've come up this far:
int sum(int a, int size)
{
int i, max, output=0;
//checking every index for max value
for(i=0;i<=tam;i++){
if(i==1){
//setting max on the first index
max=a[i];
}else{
if(a[i]>max){
a[i]=max;
}
}
}
//making the sum
for(i=0;i<size;i++){
if(a[i]==max);
output=output+max;
}
printf("%d", output);
}
The argument "a" is the array and the size is the size of the array. I get errors saying "a" is neither array nor pointer nor vector.
Any help is apreciated.
Replace int sum(int a, int size) to int sum(int *a, int size) or int sum(int a[], int size)
This function declaration
int sum(int a, int size);
declares a function with two parameters of the type int. If you mean that the first parameter should specify a one-dimensional array then the function declaration will look like
int sum(int a[], int size);
or
int sum( int *a, int size);
because a parameter having an array type is adjusted by the compiler to pointer to the array element type.
Also your function returns nothing though its return type is not void.
Moreover the function uses undeclared variables as for example the variable tam.
And if an array has size elements then the valid range of indices is [0, size ).
Also the function should not change the passed array. And to avoid integer overflow the return type should be long long int.
Also the function should not output any message. It is the caller of the function that decides whether to output a message.
The function can look the following way as it is shown in the demonstrative program below.
#include <stdio.h>
long long int sum( const int a[], size_t n )
{
long long int total = n == 0 ? 0 : a[0];
size_t max = 0;
for ( size_t i = 1; i < n; i++ )
{
if ( a[max] < a[i] )
{
total = a[i];
max = i;
}
else if ( !( a[i] < a[max] ) )
{
total += a[max];
}
}
return total;
}
int main(void)
{
int a[] = { 2, 8, 8, 9, 7, 3, 8, 1, 9 };
const size_t N = sizeof( a ) / sizeof( *a );
printf( "The sum of elements with the maximum value is %lld\n", sum( a, N ) );
return 0;
}
The program output is
The sum of elements with the maximum value is 18
#include<stdio.h>
#include<stdlib.h>
int sum(int *a, int size);
int main()
{
int *a=(int*)malloc(10*sizeof(int));
for(int i=0;i<10;i++)
a[i]=i+1;
sum(a,10);
}
int sum(int *a, int size)
{
int i, max, output=0;
//checking every index for max value
for(i=0;i<size;i++){
if(i==1){
//setting max on the first index
max=a[i];
}else{
if(a[i]>max){
a[i]=max;
}
}
}
//making the sum
for(i=0;i<size;i++){
if(a[i]==max);
output=output+max;
}
printf("%d", output);
}
Well, you can do it in just one pass, as when you identify a new maximum, the accumulated sum of the last is no longer valid (it refers not to the biggest number, but to one smaller)
There's something in your code that is weird... you start the loop at 0, and then compare if (i == 1) which I guess is a mistake (shouldn't it be 0?), as you should want to check if you are at the first (and not at the second cell) to do the initialization of max. Anyway, there's a clever way to do is to initialize max to the minimum number you can have (and for an int you have that value in <limits.h> as the constant INT_MIN). I'll show you now one possible source code to your problem (taken from yours, but changed some variables and added others to show you that in one pass you can do a lot of work:
#include <stdio.h>
#include <limits.h>
/* pretty format of output with location of trace in source file
*/
#define F(_fmt) __FILE__":%d:%s: "_fmt,__LINE__,__func__
/* to pass an array, just declare it as below. The array size is
* unspecified because your don't know it before calling sum,
* and this is the reason to pass the array size. */
int sum(int a[], int size)
{
int i, pos0 = -1, pos1 = -1, max = INT_MIN, output=0;
/* checking every index for max value, and output, you made
* an error here and used tam instead of size. */
for(i = 0; i <= size; i++){
if (a[i] > max) { /* if greater than max */
pos0 = i; /* save position as first */
max = a[i]; /* save max value */
output = max; /* initialize output to max */
} else if (a[i] == max) { /* if equal to max */
pos1 = i; /* save position as last */
output += max; /* add to output */
} /* else nothing */
}
/* print all the values we got */
printf(F("pos0 = %d, pos1 = %d, max = %d, output = %d\n"),
pos0, pos1, max, output);
return output; /* return the requested sum */
}
int list[] = { 3, 2, 5, 6, 5, 4, 7, 3, 7, 4, 7, 2, 1, 6, 2 };
/* max is located ^ ^ ^ in these positions
* 6 8 10 */
/* the following macro gives us the size of the array list by
* dividing the size of the complete array by the size of the
* first element. */
#define n_list (sizeof list / sizeof list[0])
int main()
{
printf(F("result = %d\n"), sum(list, n_list));
}
which should output (if the program is named test from test.c):
$ test
test.c:23:sum: pos0 = 6, pos1 = 10, max = 7, output = 21
test.c:34:main: result = 21
$ _
I tried to solve a C coding problem Two Sum in Online coding platform LeetCode
I am not able to return the integer pointer size.
Question:
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int i,j,sum=0,n1=0,n2=0,sz=1;
int *re;
re = (int*)malloc(sizeof(returnSize));
for(i=0;i<numsSize;i++){
if(sum==target){
break;
}
n1 = i;
for(j=i+1;j<numsSize;j++){
sum = nums[i]+nums[j];
if(sum==target){
n2 = j;
re[0] = n1;
re[1] = n2;
break;
}
}
}
return re;
}
“I expect the output of nums = [2, 7, 11, 15], target = 9, to be [0, 1], but the actual output is ]”
The interface of the function is designed to provide a two-part result, the array and its size.
You are not supposed to return the array by overwriting returnSize.
You are supposed to return the size of the returned array by writing it to the int variable referred by the pointer returnSize (and probably check that it is not a NULL pointer).
The array (i.e. the newly malloced pointer) is supposed to be returned via return, which of course you do. But doing that by overwriting return parameter pointer is what indirectly causes a problem here. (A mre would be required to trace the observed problem to this.)
By the way, I spotted this simply by seeing that you ignore and overwrite one of the parameters, the pointer. If that were correct, then the interface of the function would be inefficient. That can be the case, but usually not for challenges.
I can not reproduce the problem for the provided array. But in any case the function is incorrect..
Instead of this statement (where the argument of the malloc call does not make sense)
re = (int*)malloc(sizeof(returnSize));
^^^^^^^^^^^
there should be
re = (int*)malloc(sizeof( sizeof( int ) * *returnSize ));
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Though the parameter returnSize is redundant because the array according to your description has a fixed size equal to 2.
Moreover there is an unused variable sz=1
The function can invoke undefined behavior because in the case when the target is equal to 0 then the dynamically allocated array is not initialized and has indeterminate values because there is exit from the loop.
for(i=0;i<numsSize;i++){
if(sum==target){
break;
}
// ...
There is no need to dynamically allocate an array. You could return a structure of two elements.
The first parameter should be declared with the qualifier const.
The function can be written simpler and more clear and readable.
#include <stdio.h>
struct PairIndices
{
size_t first;
size_t second;
};
struct PairIndices twoSum( const int *a, size_t n, int target )
{
struct PairIndices pair = { n, n };
for ( size_t i = 0; i < n && pair.first == n ; i++ )
{
size_t j = 1;
while ( j < n && a[i] + a[j] != target ) j++;
if ( j != n )
{
pair.first = i;
pair.second = j;
}
}
return pair;
}
int main(void)
{
int a[] = { 2, 7, 11, 15 };
const size_t N = sizeof( a ) / sizeof( *a );
int target = 9;
struct PairIndices pair = twoSum( a, N, target );
if ( pair.first != N )
{
printf( "a[%zu] + a[%zu] == %d\n", pair.first, pair.second, target );
}
else
{
printf( "There are no two elements in the array "
"sum of which is equal to %d\n", target );
}
return 0;
}
The program output is
a[0] + a[1] == 9
So I have a task in my training that sounds like this:
Write a subprogram that will recursively find the maximum element from an array and also write the main function to call it.
What I failed to fully understand is what recursion is. I wanted to ask you guys if my code is recursive or not. And if not what changes should I make/ what recursion really means?
#include <stdio.h>
int find_maximum(int[], int);
int main() {
int c, array[100], size, location, maximum;
printf("Input number of elements in array\n");
scanf("%d", &size);
printf("Enter %d integers\n", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]);
location = find_maximum(array, size);
maximum = array[location];
printf("Maximum element location = %d and value = %d.\n", location + 1, maximum);
return 0;
}
int find_maximum(int a[], int n) {
int c, max, index;
max = a[0];
index = 0;
for (c = 1; c < n; c++) {
if (a[c] > max) {
index = c;
max = a[c];
}
}
return index;
}
Thank you all for your time!
Problems that are well-suited to recursion can be broken down into smaller, simpler subproblems. This is one of the things that gives recursion its power. When trying to use recursion to solve a problem, it usually seems best to try to break the problem down into simpler subproblems in finding your way to a solution.
You might notice that in finding the maximum value stored in an array, it is either the value of the first element, or the maximum value of the remaining elements. This breaks the problem into two parts: if the first element is larger than any remaining elements, you are done; otherwise, you must continue and see if the next element is larger than the remaining elements. In code, this might look like:
int max_in(size_t rest_sz, int *rest)
{
int curr_val = rest[0];
if (rest_sz == 1) {
return curr_val;
}
int max_in_rest = max_in(rest_sz-1, rest+1);
return curr_val > max_in_rest ? curr_val : max_in_rest;
}
Here, there is a base case: if rest_sz is 1, there is no need to look further; the value of first element (curr_val = rest[0]) is the maximum, and that value is returned. If the base case is not satisfied, execution of the function continues. max_in_rest is the result from the recursive function call max_in(rest_sz-1, rest+1). Here rest_sz-1 indicates the number of elements remaining in the portion of the array indicated by rest+1. In the new function call, the base case is met again, and eventually this case will be true since rest_sz is decremented with each recursive call. When that happens, the value of curr_val in the current stack frame will be returned; note that this value is the value of the last element in the array. Then, when the function returns to its caller, max_in_rest in that frame will get the returned value, after which the larger of curr_val or max_in_rest is returned to the previous caller, and so on, until finally control is returned to main().
Using pencil and paper to diagram each function call, the values of its variables, and what is returned would help to understand exactly how this recursion works.
You can apply the same method to solving the problem of finding the index of the maximum value of an array. In this case, if the value of the first element is greater than the value of any remaining elements, then the index of the maximum element is the index of the first element; otherwise the index of the maximum element is the index of the maximum value of the remaining elements. In code, this might look like:
size_t find_max_r(int arr[], int *rest, size_t rest_sz, size_t curr_ndx)
{
if (rest_sz == 1) {
return curr_ndx;
}
int curr_val = arr[curr_ndx];
size_t max_in_rest_ndx = find_max_r(arr, rest+1, rest_sz-1, curr_ndx+1);
int max_in_rest = arr[max_in_rest_ndx];
return curr_val >= max_in_rest ? curr_ndx : max_in_rest_ndx;
}
There is just a little more information to keep track of this time. Here, if the base case is satisfied, and rest_sz is 1, then there is no reason to look further, the current index curr_ndx is the index of the maximum value. Otherwise, find_max_r() is recursively called, with rest incremented to point to the remaining elements of the array, and rest_sz suitably decremented. This time, curr_ndx is keeping track of the current index with respect to the original array, and this value is passed into each function call; also, a pointer to the first element of the original array, arr, is passed into each function call so the index value curr_ndx can access the values from the original array.
Again, when the base case is reached, the current position in the array will be the end of the array, so the first elements to be compared in the return statement will be towards the end of the array, moving towards the front of the array. Note that >= is used here, instead of > so that the index of the first maximum value is returned; if you instead want the index of the last maximum value, simply change this to >.
Here is a complete program. Note the use of the helper function find_max() to call the recursive function find_max_r(), which allows the caller to use a function with the same signature that the posted code uses (except for the use of size_t types, which is really the correct type for array indices):
#include <stdio.h>
int max_in(size_t sz, int *rest);
size_t find_max(size_t sz, int arr[]);
size_t find_max_r(int arr[], int *rest, size_t rest_sz, size_t curr_ndx);
int main(void)
{
int array[] = { 2, 7, 1, 8, 2, 5, 1, 8 };
size_t array_sz = sizeof array / sizeof array[0];
int max_val = max_in(array_sz, array);
printf("Maximum value is: %d\n", max_val);
size_t max_ndx = find_max(array_sz, array);
printf("Maximum value index: %zu\n", max_ndx);
return 0;
}
int max_in(size_t rest_sz, int *rest)
{
int curr_val = rest[0];
if (rest_sz == 1) {
return curr_val;
}
int max_in_rest = max_in(rest_sz-1, rest+1);
return curr_val > max_in_rest ? curr_val : max_in_rest;
}
size_t find_max(size_t sz, int arr[])
{
int *rest = arr;
return find_max_r(arr, rest, sz, 0);
}
size_t find_max_r(int arr[], int *rest, size_t rest_sz, size_t curr_ndx)
{
if (rest_sz == 1) {
return curr_ndx;
}
int curr_val = arr[curr_ndx];
size_t max_in_rest_ndx = find_max_r(arr, rest+1, rest_sz-1, curr_ndx+1);
int max_in_rest = arr[max_in_rest_ndx];
return curr_val >= max_in_rest ? curr_ndx : max_in_rest_ndx;
}
Program output:
Maximum value is: 8
Maximum value index: 3
Think of calculating the maximum number in an array as the number which will be maximum of the first element and the maximum of the remaining elements of the array. Something like: max(first_elem, max(remaining_elems)).
The actual recursive function: find_max quite simple, if there is just a single element in the array, that element is returned. Otherwise, we get the maximum of the first element and the remaining elements of the array.
#include <stdio.h>
// function to find the max of 2 numbers
int max(int x, int y)
{
return (x > y) ? x : y;
}
// the recursive function
int find_max(int *p, int n)
{
if (n == 1) return *p;
return max(*p, find_max(p + 1, n - 1));
}
int main(void)
{
int arr[] = {23, 3, 11, -98, 99, 45};
printf("max: %d\n", find_max(arr, sizeof arr / sizeof arr[0]));
}
No, your code does not use recursion. Recursion is when a function calls itself, or calls another function which leads to a call to itself again.
You can change your code like this to have a recursive, stateless function that can determine the maximum value of the array.
int find_maximum(int a[], int n) {
return find_maximum_r(a, 0, n);
}
int find_maximum_r(int a[], int index, int n) {
if (index + 1 == n) {
return a[index];
}
int maxRight = find_maximum_r(a, index + 1, n);
return a[index] > maxRight ? a[index] : maxRight;
}
No, your code is recursive only if you call the function find_maximum from itself directly or indirectly.
As your function is trying to get not only the maximum value, but also the position in the array, I have modified slightly the interface to return the reference (that is, a pointer to the value) so we can infer the position of the array element directly from the subtraction of element pointers. This way, I can pass to the function the array pointer directly and the array size, and then divide the array in two halves, and applying the same function to the two halves (it can be demonstrated that if some element is the maximum value of the array, it has to be greater than or equal to each half's maximum) For the same reason, I have modified some of the variables defined in your main() function, to allow for references to be used:
max.c
#include <stdio.h>
#include <assert.h>
int *find_maximum(int a[], int n); /* return a reference pointer to the maximum value */
int main() {
int c, array[100], size, *location, /* location must be a pointer */
maximum;
printf("Input number of elements in array\n");
scanf("%d", &size);
assert(size >= 1);
printf("Enter %d integers\n", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]);
location = find_maximum(array, size);
maximum = *location; /* access to the value is granted by pointer dereference */
printf("Maximum element location = %td and value = %d.\n",
location - array, /* pointer difference gives the array position */
maximum);
return 0;
} /* main */
/* somewhat efficient recursive way of a divide and conquer method
* to get the maximum element reference. */
int *find_maximum(int a[], int n)
{
if (n == 1) return a; /* array of 1 element */
int *left = find_maximum(a, n/2), /* left half begins at a
* and has n/2 elements */
*right = find_maximum(a + n/2, (n+1)/2); /* right half begins
* at a + n/2, and
* has (n+1)/2
* elements */
return *left > *right
? left
: right;
} /* find_maximum */
As you see, I have to divide by two, but as I have arrays of any length, I have to be careful not to leave out any element in the next step. This is the reason for using an array of (n+1)/2 elements in the right half of the recursive call to the function. I include n/2 elements in the first half (rounding down), I have to include (n+1)/2 elements (rounding up) in the right half, to be sure that I include all the array elements in the two halves.
First of all, recursion means - function calling itself.
And what you've written is not recursive function. I'll post the most simple way to find biggest or largest element in an array, using recursion.
#include<stdio.h>
#define N 5
int biggest(int num[], int n, int big)
{
if(n < 0)
return big;
else
{
if(big < num[n])
big = num[n];
return biggest(num, --n, big);
}
}
int main()
{
int a[N], i;
printf("Enter %d integer number\n", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("Biggest Element in the array: %d\n", biggest(a, N - 1, a[0]));
return 0;
}
Source: C Program To Find Biggest Element of An Array using Recursion
NO it is not recursive function
to know about recursion this link is very useful https://www.khanacademy.org/computing/computer-science/algorithms/recursive-algorithms/a/recursion/
to make a recursion function to solve your problem try this
you can try this pseudo code declare your array global and a max=0 global and size global
int find_maximum(int i)
{
if (i == size )
return max;
else if ( max < array[i])
max =array [i];
return find_maximum(i+1);
}
where i is the array index
No, your program is certainly not recursive. As the definition, recursive function must call itself with a terminating condition.
Please read TutorialsPoint about recursion in C.
Update on #JonathanLeffler's comment:
Please note that the output in the reference will overflow.
I want to retrieve the index in the array where the value is stored. I know the value of the item at that point in the array. I'm thinking it's similar to the findIndex function in c#.
For example, array[2] = {4, 7, 8}. I know the value is 7, how do I get the value of the index, 1, if I know it is at array[1]?
For example you can define the corresponding function the following way
size_t FindIndex( const int a[], size_t size, int value )
{
size_t index = 0;
while ( index < size && a[index] != value ) ++index;
return ( index == size ? -1 : index );
}
Also instead of type size_t you can use type int.
But the better way is to use standard algorithm std::find or std::find_if declared in header <algorithm> provided that you use C++
For example
#include <algorithm>
#include <iterator>
int main()
{
int a[] = { 4, 7, 8 };
auto it = std::find( std::begin( a ), std::end( a ), 7 );
if ( it != std::end( a ) )
{
std::cout << "The index of the element with value 7 is "
<< std::distance( std::begin( a ), it )
<< std::endl;
}
}
The output is
The index of the element with value 7 is 1
Otherwise you have to write the function yourself as I showed abve.:)
If the array is sorted you can use standard C function bsearch declared in header <stdlib.h>
For example
#include <stdio.h>
#include <stdlib.h>
int cmp( const void *lhs, const void *rhs )
{
if ( *( const int * )lhs < *( const int * )rhs ) return -1;
else if ( *( const int * )rhs < *( const int * )lhs ) return 1;
else return 0;
}
int main()
{
int a[] = { 4, 7, 8 };
int x = 7;
int *p = ( int * )bsearch( &x, a, 3, sizeof( int ), cmp );
if ( p != NULL ) printf( "%d\n", p - a );
return 0;
}
First its important that the argument list contain size information for the array, i.e. passing a pointer to an array only does not provide enough information to know how many elements the array has. The argument decays into a pointer type with no size information to the function.
So given that, you could do something like this:
int findIndex(int *array, size_t size, int target)
{
int i=0;
while((i<size) && (array[i] != target)) i++;
return (i<size) ? (i) : (-1);
}
For small arrays this approach will be fine. For very large arrays, some sorting and a binary search would improve performance
Here's my version without a additional variable.
// Return index of element starting
// Return -1 if element is not present
int indexOf(const int elm, const int *ar, int ar_cnt)
{
// decreasing array count till it reaches negative
// arr_cnt - 1 to 0
while (ar_cnt--)
{
// Return array index if current element equals provided element
if (ar[ar_cnt] == elm)
return ar_cnt;
}
// Element not present
return -1; // Should never reaches this point
}
Hope the comments are self explanatory!