C array

Trouble printing out the max index of an array

Page 1 of 1

3 Replies - 659 Views - Last Post: 16 June 2010 - 06:24 PM Rate Topic: -----

#1 tam13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-June 10

C array

Posted 16 June 2010 - 06:16 PM

Hey,

So, I'm new to C and I'm trying to write a program that finds the max value and prints the index. My code should be working however when I run it the output looks like this:
Testing program...

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

End of program...Goodbye!

Can somebody tell me why it prints zero for the value and index.
Thanks!

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;
	}


Is This A Good Question/Topic? 0
  • +

Replies To: C array

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5953
  • View blog
  • Posts: 23,215
  • Joined: 23-August 08

Re: C array

Posted 16 June 2010 - 06:21 PM

Ummmm....where are you doing anything with the array???
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: C array

Posted 16 June 2010 - 06:21 PM

'i' and 'k' aren't initialized and you never call your sort function in main()
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5953
  • View blog
  • Posts: 23,215
  • Joined: 23-August 08

Re: C array

Posted 16 June 2010 - 06:24 PM

Cross-posted here. Bad form.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1