Mode of an array

Hello,
i have to create an array and then find the mode, fairly simple it would seem xD
this code is extremely rough, i'm just looking for some guidance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

int main()
{
	int num[100],mode=0;

	for (int i=0; i<100;i++)
	{	
		cout << "Enter values ending in -1: ";
		cin >> num[i];
		
		while (num[i]!=-1)
		{
			mode=i+num[i];
			cout << "Mode of your values is" << mode;
		}

		while (num [i] <=0)
		{
			cout << "Your values were not all positive."<< endl;
			cout << "Enter values ending in -1: ";
			cin >> num[i];
		}
	
	}
return 0;
}
Are we supposed to type something like "123456-1" and the program figures out to store "1", "2", "3", "4", "5", "6"?

I'm sorry, but the best guidance I can give is to completely remake this loop. Right now as soon as you type a number that isn't -1 you get stuck in infinity.

This program should be three parts:

1) Input values into an array using a sentinel to stop the loop (-1)
2) compute the mode (which is not what you think it is)
3) display the mode

You have these parts all wrapped up in each other, and it is making your life tough.

Last edited on
Ok well i know my loop is messed up, since this i have fixed that. i just need the part where i actually figure out how to get the mode from the array. for instance 1 2 3 3 4 5 -1, the mode would obviously be 3 and i need for the location of that number to become the mode, however if the array comes across a number that occurs more often i want the mod to be replaced by that number
The mode is the number that occurs the most, the median is the middle number.
i know what the mode is....my question is regarding how to output the mode from an array
Okay, 3 is in the middle so I was a confused.

First, there is no way to be sure that your array was in numeric order, so you have to sort it. This means that first you have to figure out where the -1 is. Once the array is sorted (or you have sorted a reference array), then you can start looking through values.

If value[i] is equal to value[i +1], then increase a counter, else set the counter to zero. if the counter is greater than the max count then mode = value[i].

Seems like that would work, or something similar.


Thanks lowestone, that was helpful, but now i have another problem popping up. i think there is something wrong with my sentinel loop. when i run the program and i enter a string of integers it doesnt do anything when i enter the -1 at the end of the string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
int main()
{
	int num[100],mode=0,count=0;
	
	
	for (int i=0; i<100;i++)
	{	
		cout << "Enter values ending in -1: ";
		cin >> num[i];
	
	for (int j=num[i]; j<100;j++)
	{
		while (num[j]!=-1)
		{
			if (num[j]=num[j]+1)
				count++;
			else
				count=0;


			mode=count;
		}
			cout << "Mode of your values is" << mode;
		
			while (num [j] <=0)
			{
			cout << "Your values were not all positive."<< endl;
			cout << "Enter values ending in -1: ";
			cin >> num[i];
			}
		}
	}
return 0;
}
[code]

When the cin extracts an integer it reads until a non-integral value. In this case, it is the "-". So if you type 123456-1, num[0] = 123456 and num[1] = -1. The loop is waiting for you to enter in 98 more integers.

Your loops are still a little loopy. You enter the first loop, type in a number, enter the second loop (and if the number you typed is less than 100), start looping through junk data in your array. Your mode is going to come out garbage, then most likely enter another loop that will never end. If it didn't , then you still loop through j until the number you originally entered reaches 100. Do that 99 more times and your program is done.

This is why I recommend breaking your code into pieces.

Piece 1, which is already trouble because the way cin works could be something like:
1
2
3
4
5
6
const int SIZE = 100;
int num[SIZE], i = 0;
for (; i << SIZE && num[i] != -1; i++){
  cout << "Enter a number  (-1 to end): ";
  cin >> num[i]; cin.ignore(80, '\n');
}


This is going to be more hitting enter than you originally planned. You're going to have to do "1" "Enter" "2" "Enter" "-1" "Enter".

Another thing would be to make a character array:
1
2
3
4
const int SIZE = 100;
char num[SIZE];
cout << "Enter your numbers.  (-1 to stop)";
getline(cin, SIZE, '-');


Which is a terrible idea if you want numbers greater than 9.

With that you could enter: 12345-1 (even just 12345-) and you will fill your array. I don't know if this will null terminate the array, so you will have to figure that out, and then the amount of things that the person entered.

Then you can move to step 2, sort and find the mode. This should be a completely separate loop. Since characters are numbers, you will still be able to use the same sorting/finding methods.

When that loop is done, then cout << "Your mode is: " << mode
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
     const int SIZE = 100 ;
     int num[SIZE]  ;
     int n_nums = 0 ;

     int input ;
     while ( n_nums < SIZE && (std::cin >> input)  && input != -1)
     {
          if ( input > 0 )
               num[n_nums++] = input ;
          else
               std::cerr << "Ignoring negative input: " << input << '\n' ;
     } 
 
     // sort the array.  valid indexes are 0 to n_nums-1

     // find the longest run of elements with the same value.
}



Topic archived. No new replies allowed.