Mesmerizing Array problem

This is a discussion on Mesmerizing Array problem within the C Programming forums, part of the General Programming Boards category; Hey guys, I require a bit of assistance. My goal is to get my program to read an array of ...

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

    Mesmerizing Array problem

    Hey guys, I require a bit of assistance. My goal is to get my program to read an array of 1000 integers all ranging 0-99; it then has to list the integers in a column 0-99, count and display the number of the integers, and show the current total as it makes progress.
    This is an example of what its supposed to look like printed out:
    Code:
    integer count total
    -------------------
       0 =    12,    12
       1 =    10,    22
       2 =    10,    32
       3 =    10,    42
    this is my current code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    	int data[] = {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};
    	int i, integer = 0, count = 0, total = 0;
    
    int main(void)
    
    {
    	printf("Integer   Count    Total\n");
    	printf("------------------------\n");
    
    	do{
    
    	for( i = 0; i < 1000; i++){
    		integer = integer + 1;
    					
                if( data[i] == integer )
                    count++;
    				total = total + count;
    	}	printf("%d        %d        %d\n", integer, count, total);
    	}
    	while ( integer < 100 );
    
    
    }
    I sorta understand what I'm supposed to do, but i think I've confused myself. My code prints out:
    Code:
    Integer   Count    Total
    ------------------------
    1000        0        0
    and thats it, and I'm not sure why. Can someone assist me?
    Last edited by jaja009; 03-26-2010 at 02:23 AM. Reason: made the ultra long array fit forum. XD

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    count is not initialized to anything.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    27
    thanks, count is now initialized to 0.

    Ugh, I'm still making noob mistakes. While that does address one of my problems, I still get the wrong output.
    Last edited by jaja009; 03-26-2010 at 01:15 AM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    How about limiting your line length in your code, for the forum?

    To say that you've exceeded the viewable width, is to put it mildly!

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    When you increment count, how will you tally up the individual numbers count? One way is to make another array with 100 items, and use the subscript of that array to count the tally for the numbers.

    For example:

    Code:
    int total[100];
    total[data[i]]++;
    Since data[0] = 9, total[9] will increment and therefore the total of 9 becomes 1. The rest will be up to you.

    Oh, and thank Adak for teaching this to me.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    27
    Quote Originally Posted by Adak View Post
    How about limiting your line length in your code, for the forum?

    To say that you've exceeded the viewable width, is to put it mildly!
    Sorry, I've fixed it now.

    Quote Originally Posted by blurx View Post
    When you increment count, how will you tally up the individual numbers count? One way is to make another array with 100 items, and use the subscript of that array to count the tally for the numbers.

    For example:

    Code:
    int total[100];
    total[data[i]]++;
    Since data[0] = 9, total[9] will increment and therefore the total of 9 becomes 1. The rest will be up to you.

    Oh, and thank Adak for teaching this to me.
    To make sure I understand, you set the total array to the same amount of numbers as data array, and the "i" increments upward, and the total will count how many of each number there is within the data array?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Right. That's a distribution count or "bin" count. Great little trick.

    Say your array is { 1, 2, 1, 3, 4, 1}

    Code:
    for(i = 0; i < 6; i++) {
      bin[array[i]]++;
    }
                        0  1  2  3  4
    ====================================
    Result of bin is: { 0, 3, 1, 1, 1 }
    meaning there were no zero's, there were 3 one's, there was one two, one three, and one four.

    Note that your bin array must take in the full range of your data. Even if you have just 4 numbers, if they range from 1 to 1,000, you must have 1001 as your bin array size (0 - 1000) == 1,001

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting array problem :)
    By BEST in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2009, 01:57 PM
  2. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 12:35 PM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 03:28 PM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 07:24 PM

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