I wrote this code to find the mode of an array of values.

double mode (double * array, int numItems)
{
	int j, i, maxRepeats = 0;
	double mode;
	
	for(i = 0; i < numItems; i++) 
	{
		j = 1;
		
		while(array[i] == array[i+j])
		{ 
			++j;
		}
			  if(j > maxRepeats)
			  {
			  mode = array[i];
				  maxRepeats = j;
			  }
	}
	
	return mode;
}

Is there any way to do this without using nested loops?

Thanks.

Recommended Answers

All 7 Replies

Have you studied structures and linked lists yet? If you know structures, then create a structure that contains two items: the double value and an int count of the number of times the value occurs in the original list. Then make an array or linked list of that structure. You would only run through the original double array once, but you will have to run through the array of structures multiple times.

we wont be talking about linked lists. I dont know why. As far as structures go, there should be a way to do it with one single loop(according to my teacher...), im just looking to find the way to do it the way he wants... unfortunately.

the array is sorted in decreasing order by the way.

Thanks for this thread - I love it! ;)

Intuitively, you sense that there's a better way (and right you are), but can you find it (and using just the array and indeces)?

There is a relationship between i and j that is missing - but what could it be??
(i is playing dumb)

And then the question, do you need j at all?

Nope. ;)

This is too much fun to post up an answer right away - have fun with it. If no luck, I'll post back.

Note that your chaotic indentation doesn't hurt the clarity in this instance, but it's an excellent work habit to make it consistent. Subordinate lines of code should be indented, and indented a consistent amount of spaces.

Heres what i came up with.

int i = 0, repeat_count = 0, maxRepeats = 0;
	double mode;
	
	for(i = 0; i < numItems; i++)
	{
		if(array[i] == array[i+1])
		{
			repeat_count++;
		}
		
		if (array[i] != array[i+1] && repeat_count > maxRepeats) 
		{
			maxRepeats = repeat_count;
			mode = array[i];
			repeat_count = 0;
		}
		
		if (array[i] != array[i+1])
		{
			repeat_count = 0;
		}
	}
	return mode;
commented: Yes! ;) +3

Can line 15 be removed?

You get an A+ -- well done! ;)

Get that mode, three ways:

#include <stdio.h>
#define MAX 13

int main() {
  int i, j, mode1=0, count=0; 
  int digits[MAX]={20,20,20,14,13,8,8,8,1,1,1,1,1};
  int aux[100]={0};
  for(i=0;i<MAX;i++) {
     j=1;
     while(digits[i]==digits[i+j]) {
       ++j;
     }
     if(j>count) {
       mode1=digits[i];
       count=j;
     }
     i+=j-1;
  }
  printf("\n\nMode is: %d, found %d times", mode1, count);
 
  for(i=0, count=0;i<MAX;i++) {
    aux[digits[i]]++;
    if(aux[digits[i]] > count) {
      count = aux[digits[i]];
      mode1 = digits[i];
    }
  }
  printf("\nMode is: %d, found %d times\n", mode1, count);

  count=0;
  for(i=0, j=1;i<MAX;i++) {
    if(digits[i] == digits[i+1])
      ++j;

    if (digits[i] != digits[i+1] && j > count) {
      count = j;
      mode1 = digits[i];
      j = 1;
    }

    if (digits[i] != digits[i+1])
      j = 1;
  }
  printf("Mode is: %d, found %d times\n", mode1, count);	

  printf("\n\n\t\t\t     press enter when ready");

  (void) getchar(); 
  return 0;
}
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.