Hi Guys,

So I have the functiom find_index_of_max and I need to use main as a driver to print the index of the the max value in the array. However, mine isn't functioning correctly. So, I was wondering if anyone can tell me what I need to do.
Thanks!

#include "Hw2Func.h"

int find_index_of_max(float *arr, int values_in_arr, float *max_value)
{
	int index;
	
	index = 0;
	
	*max_value = arr[index];
	
	for (index = 1; index < values_in_arr; index++) 
	{
		if (arr[index] > *max_value)
		{
			*max_value = arr[index];
			break;
		}
	}
	return index;
}
#include <stdio.h>

int find_index_of_max(float [], int, float);
void strappend(char [], char []);

int main (int argc, const char * argv[]) 
{
    printf("This is a test ... only a test.\n");
	//variable definition
	int arr[10] = {1, 22, 13, 5, 7, 10, 2, 9, 6 , 12};
	int index;
	
	
	printf("The Array :\n");
	
	index = find_index_of_max(<#float [] #>, <#int #>, <#float #>);
	printf("The index of the max number is : %f\n", index);
	
    return 0;
}

Recommended Answers

All 4 Replies

First, the 'break' statement in find_index_of_max is probably not what you want. Instead, it returns the index of the first element greater than arr[0]. You'll need to examine all the elements of your array to find the largest. I would think it works with your example array, but if you swapped array[1] and array[2], it would still return 1.

You also need to call the function with actual values: find_index_of_max(arr,10,??) -- you need to declare a float to hold that number.

Thanks for the help.

Ok, so I did some more work on my code, but it's still not printing the index of the max value.

int find_index_of_max(float [], int, int, int, int);
void strappend(char [], char []);

int main (int argc, const char * argv[]) {	
	
		printf("Testing program...\n");
	
		//variable definition
		float arr[] = {1, 22, 13, 5, 7, 10, 2, 9, 6 , 12, 14, 27};
		int size = sizeof arr / sizeof arr[0];
	        int i;
		int k;
		
		
		printf("\nThe Array :\n");
	
		printf("The greatest of %d values is %d at index %d\n", size, i, k); 
		
		printf("\nEnd of program...Goodbye!");
	
		return 0;
	}
#include "Funcs.h"
int find_index_of_max(float arr[], int size, int i, int j, int k)
{
	i, j, k = 0;
	i = arr[0];
	for (j = 1; j < size; ++j)
		if (arr[j] > i) {
			i = arr[j];
			k = j;
		}
	return i, k;
	}

It just returns this:
Testing program...

The Array :
The greatest of 12 values is 0 at index 0

End of program...Goodbye!

Crossposted here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.