Thread: Please, Need Help: Counting/Array.

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    9

    Please, Need Help: Counting/Array.

    Hi all,

    Im kinda stuck at determine how many of these 250 numbers are less than 356; here's my coding

    Code:
    #include<stdio.h>#include<math.h>
    #define N 250
    
    
    int main()
    {
    float a[N]={811.594,155.670,80.859,94.000,0.262,25.623,457.023,984.578,134.533,41.242,65.121,8.639, 9.048,50.725,367.316,665.240,0.310,6.573,44.645,0.895,2.097,542.671,0.801,9.622,699.939,
    860.123,7.442,572.632,347.332,4.520,2.346,899.460,9.257,280.994,33.103,6.917,63.084,2.147, 602.037,34.400,207.035,610.862,510.798,384.195,66.511,73.813,28.353,0.455,642.567,592.365,
    0.429,390.547,29.399,9.592,998.127,4.489,72.900,445.181,74.800,11.912,7.055,1.754,806.574, 612.518,116.010,58.165,86.229,462.407,767.028,9.494,54.029,4.808,8.092,3.840,8.309,5.891,
    766.304,3.690,23.049,3.797,48.046,73.041,816.971,28.815,6.538,0.139,12.927,1.468,325.504, 252.919,8.828,825.905,8.414,89.715,195.159,713.316,8.033,60.206,1.269,22.268,72.693,3.552,
    796.728,2.614,49.046,51.873,92.447,14.496,46.786,9.247,1.053,28.028,91.845,5.086,59.356, 39.928,7.405,16.381,4.317,75.484,141.754,43.461,561.134,339.839,185.844,95.934,714.524,
    652.879,9.155,928.245,610.435,0.585,5.848,3.441,89.534,4.685,90.123,4.778,542.252,811.509, 88.518,67.992,3.908,81.427,82.676,3.812,443.006,9.816,26.903,62.345,7.255,31.488,8.656,
    3.055,2.282,7.377,1.698,359.694,36.820,5.927,106.018,430.541,1.513,729.546,9.143,9.760, 256.516,210.061,2.039,4.382,47.178,8.500,27.128,2.241,336.265,1.007,78.052,4.196,5.518,
    4.011,97.252,41.931,6.709,939.500,4.995,15.107,1.875,31.338,1.540,1.917,2.449,340.436, 3.458,85.963,6.296,13.044,7.033,14.899,620.605,59.634,8.744,44.663,3.440,5.132,366.882,
    44.704,516.307,8.589,893.968,101.669,90.267,1.507,74.073,11.016,912.024,4.578,36.568, 4.440,497.334,269.221,608.552,92.279,74.272,0.310,218.616,8.141,360.163,643.359,80.840,
    289.527,6.508,90.192,8.661,14.624,92.793,89.858,8.922,47.441,8.370,9.899,954.655,1.534, 8.504,28.940,359.621,2.442,1.475,22.160,50.042,50.506};
    
    
     int c=0; int i; int count[250]; for (i=0;i<N;i++) {
    c=a[i]; if (c<356) { count[c]=0;
    count[c]++;
    }
    }
    
    
    printf("There are: %d numbers less than 356\n",count[c]);
    return 0;
    
    }

    Last edited by CJofficial; 02-07-2013 at 09:18 PM.

  2. #2
    Registered User
    Join Date
    Jan 2013
    Location
    UK
    Posts
    19
    Code:
    #include<stdio.h>
    #include<math.h>
    
    #define N 250
    
    
    int main()
    {
    float a[N]={811.594,155.670,80.859,94.000,0.262,25.623,457.023,984.578,134.533,41.242,65.121,8.639,.048,50.725,367.316,665.240,0.310,6.573,44.645,0.895,2.097,542.671,0.801,9.622,699.939,860.123,7.442,572.632,347.332,4.520,2.346,899.460,9.257,280.994,33.103,6.917,63.084,2.147,602.037,34.400,207.035,610.862,510.798,384.195,66.511,73.813,28.353,0.455,642.567,592.365,0.429,390.547,29.399,9.592,998.127,4.489,72.900,445.181,74.800,11.912,7.055,1.754,806.574,612.518,116.010,58.165,86.229,462.407,767.028,9.494,54.029,4.808,8.092,3.840,8.309,5.891,766.304,3.690,23.049,3.797,48.046,73.041,816.971,28.815,6.538,0.139,12.927,1.468,325.504,252.919,8.828,825.905,8.414,89.715,195.159,713.316,8.033,60.206,1.269,22.268,72.693,3.552,796.728,2.614,49.046,51.873,92.447,14.496,46.786,9.247,1.053,28.028,91.845,5.086,59.356,9.928,7.405,16.381,4.317,75.484,141.754,43.461,561.134,339.839,185.844,95.934,714.524,652.879,9.155,928.245,610.435,0.585,5.848,3.441,89.534,4.685,90.123,4.778,542.252,811.509,8.518,67.992,3.908,81.427,82.676,3.812,443.006,9.816,26.903,62.345,7.255,31.488,8.656,3.055,2.282,7.377,1.698,359.694,36.820,5.927,106.018,430.541,1.513,729.546,9.143,9.760,6.516,210.061,2.039,4.382,47.178,8.500,27.128,2.241,336.265,1.007,78.052,4.196,5.518,4.011,97.252,41.931,6.709,939.500,4.995,15.107,1.875,31.338,1.540,1.917,2.449,340.436, 3.458,85.963,6.296,13.044,7.033,14.899,620.605,59.634,8.744,44.663,3.440,5.132,366.882,44.704,516.307,8.589,893.968,101.669,90.267,1.507,74.073,11.016,912.024,4.578,36.568,4.440,497.334,269.221,608.552,92.279,74.272,0.310,218.616,8.141,360.163,643.359,80.840,289.527,6.508,90.192,8.661,14.624,92.793,89.858,8.922,47.441,8.370,9.899,954.655,1.534,.504,28.940,359.621,2.442,1.475,22.160,50.042,50.506};
    
    int i;
    int count = 0;
    
        for(i=0; i<N; i++)
            if(a[i] < 356)
                count++;
    
        printf("There are: %d numbers less than 356\n", count);
    
    return 0;
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You forgot to initialise count to 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    WOW!!! your indenting, newlines, EVERYTHING is a mess!!!

    thats 1, 2 your define is not needed just change N to 250, 3 your second array is not needed, as well as useless.

    basically fix the formating, then just use a counter to count +1 for each one less then 356


    //edit//

    ok i missed the count being zeroed, but it is ok, because it seems another new user has not read the policies, and decided to do it for you
    Last edited by Crossfire; 02-07-2013 at 09:35 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sigh. 0x47617279, please do not do other people's homework for them. I saw your code dump and assumed that you were the original poster updating his/her code attempt, so my post pointed out a problem with your code that you have since corrected.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    Thanks all for your help!!
    I don't understand what "count++" does ?

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    I don't understand what "count++" does ?

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Do a search for: "increment operator"

  9. #9
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    Thanks Matticus

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by CJofficial View Post
    Thanks all for your help!!
    I don't understand what "count++" does ?
    This site is your friend

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-04-2012, 07:58 PM
  2. Replies: 5
    Last Post: 03-29-2012, 02:57 AM
  3. counting rows in a 2D array
    By Kyoukoku in forum C Programming
    Replies: 12
    Last Post: 09-22-2010, 10:26 AM
  4. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 11:25 AM
  5. Array counting...
    By hanek in forum C Programming
    Replies: 3
    Last Post: 03-27-2004, 05:06 PM