C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 13th, 2003, 06:19 PM
mulderGURL mulderGURL is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 9 mulderGURL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Comparing number... HELP @_@

I have a set of numbers & character, and each number/char appears more than once. Suppose: A, B, 6, 9, B, 8, 3, B……. (a total of 10 different kinds) How should I program, in order to find out the most occurring number/char? All the number/char has already been count, which means that I already know 6 appears six times, 9 appears 3 times, etc. I was trying to use a loop that save the first number as “temp” then start to compare the other, and if the temp is great, I would keep comparing until either the count of the other number/char is the same, then I would do something…but I start to get into infinite loop and very very long codes… any idea how to make it easier?
**Although this is a program in assembly language, I just want to get the concept (even in C is fine...)…

THANKS!

Reply With Quote
  #2  
Old June 13th, 2003, 06:48 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
this is probably a very ineficiant way, but it's a way.

make an array whose index values cover all possible ascii character values that you may encounter and therefore need to count. fill all the elements with 0. then step through your text and use the character's ascii numeric value to increment the corresponding element in the array. you end up with an array with all the characters (including commas and spaces) counted.

Code:
#include <stdio.h>
#define SIZE 128

main()
{
	int i;
	char chars[] = "A, B, 6, 9, B, 8, 3, B";
	int counts[SIZE]	 = {0};
	char *p = chars;
	
	while(*p)
		counts[*p++]++;
		
	for(i = 0; i < SIZE; i++)
		if(counts[i])
			printf("%c %d\n", i, counts[i]);
}

outputs:
Code:
  7
, 7
3 1
6 1
8 1
9 1
A 1
B 3

Reply With Quote
  #3  
Old June 13th, 2003, 07:11 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
I think he said he already had the counting done.

If this is the case, you simply have to store a temporary max count, as you stated. Set this max count to the count of the FIRST counted character. Then loop through the remaining counts, and whenever the count exceeds the temporary max count, update it.

Of course, if you wish to know which character this count is for, then you'll also have to keep another temporary character variable to store this. Initialize this variable to the first character counted, and update it wheneven a new maximum count is found.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Comparing number... HELP @_@


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway