473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Counting numbers in an array

Hi, I've come across a problem where i seem to be doing the counting of numbers in a inefficient way i've got a 200x200 array with numbers ranging from 101-600 and also 0 and 1. and i need to count the number of times each number appears in the array. only thing i could think of was this but its quite long.

if (s[i][j]==101) {count_c101 ++;}
if (s[i][j]==102) {count_c102 ++;}
if (s[i][j]==103) {count_c103 ++;}
if (s[i][j]==104) {count_c104 ++;}
if (s[i][j]==105) {count_c105 ++;}
if (s[i][j]==106) {count_c106 ++;}
if (s[i][j]==107) {count_c107 ++;}
if (s[i][j]==108) {count_c108 ++;}
if (s[i][j]==109) {count_c109 ++;}
if (s[i][j]==110) {count_c110 ++;}
if (s[i][j]==111) {count_c111 ++;}
if (s[i][j]==112) {count_c112 ++;}
if (s[i][j]==113) {count_c113 ++;}
if (s[i][j]==114) {count_c114 ++;}
if (s[i][j]==115) {count_c115 ++;}
if (s[i][j]==116) {count_c116 ++;}
if (s[i][j]==117) {count_c117 ++;}
if (s[i][j]==118) {count_c118 ++;}
if (s[i][j]==119) {count_c119 ++;}
if (s[i][j]==120) {count_c120 ++;}
if (s[i][j]==121) {count_c121 ++;}
if (s[i][j]==122) {count_c122 ++;}
if (s[i][j]==123) {count_c123 ++;}
if (s[i][j]==124) {count_c124 ++;}
if (s[i][j]==125) {count_c125 ++;}
if (s[i][j]==126) {count_c126 ++;}
if (s[i][j]==127) {count_c127 ++;}
if (s[i][j]==128) {count_c128 ++;}
if (s[i][j]==129) {count_c129 ++;}
if (s[i][j]==130) {count_c130 ++;}
....
....
....
this is a small part of my entire code, where i use all the different values of count_c101,count_c102,count_c103,count_c104,count_ c105,.....

I would be grateful for any help

kind regards,
Kam
Aug 17 '07 #1
5 2005
JosAH
11,448 Expert 8TB
You do know about for-loops do you? You also know about arrays do you?
How about an array count[] that keeps track of the number of times a number
occurs in that other array, i.e. count[i]= j means number i occurs j times.

kind regards,

Jos
Aug 17 '07 #2
You do know about for-loops do you? You also know about arrays do you?
How about an array count[] that keeps track of the number of times a number
occurs in that other array, i.e. count[i]= j means number i occurs j times.

kind regards,

Jos
thanks for the reply. I've never come across array count before, what library do i need to include for that?

kind regards,
kamlesh
Aug 17 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
There's no special library required. A loop is part of the language. Get a book on C.
Aug 17 '07 #4
There's no special library required. A loop is part of the language. Get a book on C.
not to worry i figured it all out

Expand|Select|Wrap|Line Numbers
  1. for(a=0;a<=600;a++)
  2. {
  3.    count[a]=0;
  4. }
  5.  
  6. for(i=0;i<imax;i++)
  7. {
  8.    for(j=0;j<jmax;j++)
  9.    {
  10.     count[s[i][j]]++;
  11.    }
  12. }
Aug 17 '07 #5
JosAH
11,448 Expert 8TB
not to worry i figured it all out

Expand|Select|Wrap|Line Numbers
  1. for(a=0;a<=600;a++)
  2. {
  3.    count[a]=0;
  4. }
  5.  
  6. for(i=0;i<imax;i++)
  7. {
  8.    for(j=0;j<jmax;j++)
  9.    {
  10.     count[s[i][j]]++;
  11.    }
  12. }
Very well done; I hope the size of 'count' isn't 600 ...

kind regards,

Jos
Aug 18 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Mr. x | last post by:
Hello, I need a script for counting the no. of enters on my site, please. I need a check that for the same user in the same day - if the user enter several times, it is count once (or something...
9
by: tman88g | last post by:
I've been trying to learn arrays, and I was wondering how one would go about making a program that would take from the user as many single digit numbers (0 - 9) as the user wants to enter, and then...
4
by: frizzle | last post by:
Hi there, I have a function to create an array of all files in a certain folder, so i can display the structure. The actual function is below the message, as is an example of its output. As...
3
by: IZZI | last post by:
This code is created to find all duplicates in a list of number by using a binary search tree implemented as an array. I want to add a function to count how many numbers in the list are duplicated...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.