Nearly finished, but a single problem remains.

This is a discussion on Nearly finished, but a single problem remains. within the C Programming forums, part of the General Programming Boards category; Hey guys, I want to thank you for all the help you've given me before, but I have another question. ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    27

    Nearly finished, but a single problem remains.

    Hey guys, I want to thank you for all the help you've given me before, but I have another question.
    This code I'm working on isnt counting the integers within the array correctly:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    	int data[] = { [spoiler]9, 71, 39, 97, 91, 61, 95, 17, 55, 55, 17, 48, 78, 9, 28, 34, 64, 44, 14, 73, 38, 33, 90, 83, 21, 54, 42, 94, 7, 1, 0, 66, 13, 41, 28, 
    44, 99, 47, 22, 68, 70, 50, 32, 12, 53, 31, 28, 49, 11, 62, 36, 90, 67, 6, 29, 85, 80, 61, 87, 15, 64, 64, 79, 95, 84, 77, 92, 27, 35, 21, 97, 82, 7, 86, 5, 70, 63, 
    14, 75, 3, 96, 63, 46, 44, 78, 65, 69, 39, 18, 58, 71, 78, 99, 97, 26, 62, 52, 56, 58, 71, 75, 50, 70, 20, 70, 2, 51, 82, 33, 26, 3, 90, 56, 3, 11, 95, 0, 51, 2, 44,
     57, 64, 62, 35, 86, 0, 34, 43, 63, 80, 89, 79, 98, 8, 17, 62, 56, 51, 62, 48, 59, 88, 89, 93, 9, 65, 38, 38, 30, 26, 40, 59, 86, 94, 18, 82, 6, 2, 93, 0, 26, 80, 
    20, 7, 30, 97, 70, 96, 24, 7, 51, 10, 7, 35, 78, 13, 61, 46, 18, 58, 79, 72, 68, 11, 51, 31, 63, 84, 8, 21, 69, 36, 15, 64, 88, 10, 31, 42, 98, 92, 77, 45, 72, 75, 
    96, 62, 90, 71, 42, 33, 53, 64, 53, 65, 17, 40, 89, 80, 52, 12, 6, 93, 38, 27, 91, 21, 3, 83, 93, 56, 34, 79, 1, 43, 79, 97, 66, 50, 42, 31, 72, 6, 94, 75, 60, 58,
     74, 20, 5, 90, 58, 65, 76, 49, 28, 63, 5, 40, 84, 93, 21, 72, 39, 93, 60, 27, 45, 35, 45, 39, 2, 65, 2, 45, 91, 84, 12, 17, 22, 53, 51, 53, 51, 3, 70, 36, 33, 74, 
    68, 24, 25, 10, 86, 45, 60, 65, 71, 70, 78, 99, 74, 65, 60, 12, 89, 19, 5, 57, 56, 17, 57, 47, 32, 47, 56, 83, 91, 27, 82, 2, 31, 21, 35, 80, 89, 51, 92, 85, 64, 
    20, 0, 51, 69, 87, 10, 98, 45, 88, 31, 89, 0, 82, 24, 67, 62, 31, 47, 52, 43, 65, 55, 13, 99, 35, 5, 60, 28, 42, 36, 64, 27, 3, 45, 56, 46, 94, 26, 26, 87, 29, 73, 
    79, 91, 5, 63, 23, 77, 96, 18, 15, 13, 8, 70, 20, 63, 99, 36, 32, 41, 31, 48, 49, 1, 12, 44, 66, 28, 38, 34, 79, 92, 91, 68, 0, 60, 57, 78, 43, 40, 74, 75, 84, 21,
     45, 0, 62, 98, 64, 68, 77, 27, 70, 85, 54, 9, 31, 66, 2, 95, 38, 73, 5, 55, 62, 13, 81, 73, 60, 54, 83, 34, 84, 20, 80, 84, 79, 29, 94, 57, 84, 70, 65, 25, 25, 60, 
    34, 17, 83, 81, 19, 44, 80, 36, 20, 47, 21, 12, 58, 84, 9, 99, 68, 98, 34, 28, 71, 40, 78, 9, 84, 52, 47, 38, 79, 20, 53, 74, 46, 55, 13, 85, 96, 18, 13, 1, 67, 78, 
    19, 76, 27, 82, 5, 52, 34, 83, 78, 43, 12, 16, 55, 31, 97, 8, 60, 94, 48, 61, 94, 12, 4, 44, 85, 64, 92, 50, 40, 39, 1, 48, 30, 81, 71, 45, 79, 98, 81, 38, 24, 14, 
    93, 98, 39, 93, 6, 53, 66, 60, 23, 83, 64, 15, 38, 50, 62, 71, 9, 79, 2, 96, 87, 50, 88, 76, 14, 7, 14, 18, 7, 92, 81, 71, 34, 37, 59, 78, 90, 56, 52, 13, 76, 37, 
    22, 44, 81, 35, 66, 60, 86, 6, 20, 88, 63, 24, 21, 22, 67, 81, 86, 50, 81, 93, 29, 39, 12, 15, 45, 68, 44, 73, 9, 54, 63, 28, 97, 29, 19, 9, 80, 41, 41, 42, 33, 55, 
    14, 68, 75, 98, 89, 93, 53, 22, 47, 53, 49, 78, 85, 36, 17, 65, 82, 73, 56, 31, 27, 13, 14, 67, 2, 61, 24, 6, 26, 87, 7, 39, 41, 95, 36, 94, 49, 92, 49, 44, 45, 22, 
    30, 91, 70, 92, 8, 26, 14, 0, 72, 23, 29, 50, 44, 92, 30, 6, 87, 89, 45, 58, 10, 47, 49, 26, 87, 83, 1, 59, 53, 53, 8, 53, 65, 31, 49, 71, 12, 54, 4, 29, 98, 33, 71, 
    78, 30, 76, 36, 26, 60, 28, 89, 32, 54, 54, 65, 73, 57, 78, 35, 24, 70, 10, 37, 99, 82, 62, 6, 82, 11, 87, 46, 38, 76, 91, 25, 64, 37, 42, 90, 87, 99, 83, 12, 89, 
    42, 26, 24, 4, 66, 73, 82, 44, 93, 82, 80, 2, 94, 84, 63, 21, 3, 62, 49, 24, 38, 90, 22, 70, 66, 21, 57, 89, 14, 4, 71, 33, 13, 35, 55, 68, 1, 48, 3, 34, 59, 55, 66, 
    12, 87, 48, 60, 14, 91, 62, 96, 83, 27, 18, 70, 16, 99, 54, 1, 37, 26, 83, 36, 98, 53, 52, 1, 78, 12, 12, 35, 51, 9, 8, 62, 84, 15, 39, 24, 9, 93, 97, 53, 75, 17, 
    93, 21, 32, 88, 5, 61, 1, 52, 91, 83, 68, 52, 3, 62, 21, 25, 37, 33, 74, 27, 97, 25, 46, 58, 7, 79, 62, 68, 20, 69, 16, 0, 53, 35, 94, 12, 51, 85, 37, 43, 74, 9, 65, 
    77, 75, 11, 21, 76, 33, 96, 51, 80, 8, 51, 89, 14, 0, 20, 96, 93, 64, 69, 33, 7, 96, 55, 42, 88, 87, 42, 62, 15, 54, 19, 81, 75, 26, 32, 26, 89, 65, 27, 22, 73, 31, 
    35, 72, 53, 37, 81, 50, 31, 32, 27, 0, 48, 31, 3, 73, 37, 74, 19, 80, 19, 73, 59, 64, 19, 80, 41, 15, 54, 12, 78, 93, 92, 42, 79, 10, 55, 79, 62, 55, 92, 33, 11, 
    97, 27, 88, 62, 80, 48, 96, 76, 51, 9, 59, 36, 30, 34, 10, 35, 35, 92, 89, 90, 20, 65, 22, 15, 50, 69, 46, 48, 49, 81, 6, 94, 94, 27, 92, 67, 72, 87, 26, 
    12[/spoiler]};
    	int i, integer = -1;
    	int count = 0;
    	int total = 0;
    	
    
    int main(void)
    
    {
    	printf("Integer   Count    Total\n");
    	printf("------------------------\n");
    
    	do{				
    		integer++;
                    count = 0;
    		for( i = -1 ; i < 1000; i++ )
    			
                if( data[i] == integer )
                    count++;			 
    				total = count + total;
    
    	printf("%3d        %1d     %3d\n", integer, count, total);
    	
    	}
    	while (integer < 99);
    
    }
    I surprisingly got the integer and total columns to work correctly, but my count column is not working correctly.
    Its supposed to just show how many of the "integer" is in the array. Once I fix that, the total column will be correct. Any idea what I did wrong?
    Last edited by jaja009; 03-26-2010 at 12:15 PM. Reason: shortening array length.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Were you planning to reset the count to 0 at some point between numbers?

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    27
    Quote Originally Posted by tabstop View Post
    Were you planning to reset the count to 0 at some point between numbers?
    OMG!! XD Thanks man! I added a "count = 0" just below the "integer++;" and it works! Its little stuff like that that I always forget!

    (added the corrections to the code above, and everything works perfectly, thanks tabstop!)

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I don't think you want to index the array data at location -1. Nor do you want to hard-code the upper limit 1000 unless you know that's the exact size of the array.

    Something seems fishy about the way you calculate total. If you sum up the count, you'd always get 1,3,6,10,15,21...

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    27
    Quote Originally Posted by nonoob View Post
    I don't think you want to index the array data at location -1. Nor do you want to hard-code the upper limit 1000 unless you know that's the exact size of the array.

    Something seems fishy about the way you calculate total. If you sum up the count, you'd always get 1,3,6,10,15,21...
    Yeah I realized that earlier. I meant to change that. When it was -1 my total would be +1 of what it should be. And the upper limit is 1000, I used Microsoft Word to count. But everything is correct now, he's the output:

    Code:
    Integer   Count    Total
    ------------------------
       0       12       12
       1       10       22
       2       10       32
       3       10       42
       4        4       46
       5        9       55
       6       10       65
       7       10       75
       8        8       83
       9       13       96
      10        8      104
      11        6      110
      12       17      127
      13       10      137
      14       11      148
      15        9      157
      16        3      160
      17        9      169
      18        7      176
      19        8      184
      20       12      196
      21       14      210
      22        9      219
      23        3      222
      24       10      232
      25        6      238
      26       15      253
      27       14      267
      28        9      276
      29        7      283
      30        7      290
      31       15      305
      32        7      312
      33       11      323
      34       11      334
      35       14      348
      36       11      359
      37        9      368
      38       11      379
      39        9      388
      40        6      394
      41        6      400
      42       11      411
      43        6      417
      44       12      429
      45       12      441
      46        7      448
      47        8      456
      48       10      466
      49       10      476
      50       10      486
      51       14      500
      52        9      509
      53       16      525
      54       10      535
      55       12      547
      56        9      556
      57        7      563
      58        8      571
      59        7      578
      60       13      591
      61        6      597
      62       19      616
      63       10      626
      64       14      640
      65       15      655
      66        9      664
      67        6      670
      68       11      681
      69        6      687
      70       14      701
      71       12      713
      72        7      720
      73       11      731
      74        8      739
      75        9      748
      76        8      756
      77        5      761
      78       15      776
      79       14      790
      80       13      803
      81       11      814
      82       11      825
      83       12      837
      84       12      849
      85        7      856
      86        6      862
      87       12      874
      88        8      882
      89       14      896
      90        9      905
      91       10      915
      92       13      928
      93       15      943
      94       12      955
      95        5      960
      96       11      971
      97       10      981
      98       10      991
      99        9     1000
    Press any key to continue . . .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Major Problem
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 12:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21