2 Replies - 409 Views - Last Post: 20 March 2013 - 09:40 PM Rate Topic: -----

#1 BuhRock  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 256
  • Joined: 21-February 10

Finding unique grades, frequency, and mode in C

Posted 20 March 2013 - 07:00 PM

Hello, I am working on a C function that requires me to find the unique grades of an array of student grades, the frequency that grade appears, and the mode. I am having trouble with all three of these aspects to a degree. My unique_grades array repeats two separate numbers, and the frequency is not printing accurately. What can I do to fix these issues?
void findFrequency(double grades[MAX_STUDENTS])
{
	double unique_grade[24];
	int maxcount = 0;
	int i;
	int j = 0;
	
	printf("Grade:	Frequency:\n");
		
		for(i = 34; i > 0; i--) {//find unique grades
			int count = 0;
			for(j = 0; j < 34; j++){//find frequency
				if (grades[j] == grades[i])
				count++;
			}
			if (count > maxcount) {//find mode
				maxcount = grade[i];
			}
			
			if (grades[i] == grades[i-1])
			{
				continue;
			}
			else
				unique_grade[i] = grades[i];
			printf("%2.2f\t %i\n",unique_grade[i],count+1);
			
			
		}
		
		printf("%2.2f\n", "The mode is ", maxcount);

}


Is This A Good Question/Topic? 0
  • +

Replies To: Finding unique grades, frequency, and mode in C

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,301
  • Joined: 25-December 09

Re: Finding unique grades, frequency, and mode in C

Posted 20 March 2013 - 09:16 PM

First you appear to be accessing your arrays out of bounds. You declared your unique_array with a size of 24 yet in your loops you try to assign a value to element 34 in some cases.

How is MAX_STUDENTS defined?

What is the significance of the "magic numbers" 24 and 34 that you are using in your function?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3456
  • View blog
  • Posts: 10,661
  • Joined: 05-May 12

Re: Finding unique grades, frequency, and mode in C

Posted 20 March 2013 - 09:40 PM

Once you figure out what your "magic numbers" are supposed to mean, and you stop accessing elements outside the array bounds consider looking over your algorithm. Note that the grade with the highest frequency is the mode, and grades with a frequency of one is unique. How would you go about determining the answer if all you had was just had pen and paper rather than a computer? If you can explain in words the steps that you would take to solve the problem, then your at 75% to being able to describe an algorithm that you can implement on a computer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1