How to search an array for certain numbers and count them.

This is a discussion on How to search an array for certain numbers and count them. within the C Programming forums, part of the General Programming Boards category; Hey guys, just like the title says, I'm having a little trouble getting my program to search an array for ...

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

    How to search an array for certain numbers and count them.

    Hey guys, just like the title says, I'm having a little trouble getting my program to search an array for certain numbers and count them up.
    (Note, yes, our assignment has that many integers for the array. XD )
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    
    {
    	int data[1000] = {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 n, count=0;
    
    
    	do{
    		printf("Enter an integer in the range of 0 to 99, inclusive: ");
    		scanf("%d", &n);
    		
    		
    
    		}
    	while (n < 100);
    
    	if (n > 100)
    		printf("The number is greater than 99, please enter a new one. \n");
    
    
    }
    Can someone help me? I tried different if statements, but I dont think I'm using the right conditional.

  2. #2
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    first, make another integer variable called NumberOfInts. next, make a FOR loop.
    with count containing zero and increasing until reaching 999. instead that loop,
    make an if condition which checks whether data[count] is equal to N or not. if it
    is, increase the NumberOfInts by one. then, after the loop, make a Printf
    statement that says

    "the number you typed has %d matches."

    for the %d you need to put in parameter NumberOfInts.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    27
    Thanks for the help Ultraking, but I dont think I did it correctly, have a look:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    
    {
    	int data[1000] = {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 n;
    	int integercount;
    
    
    	do{
    		printf("Enter an integer in the range of 0 to 99, inclusive: ");
    		scanf("%d", &n);
    		
    		for (integercount = 0; integercount < 999; integercount++)
    		if( data[1000] == n)
    			integercount++;
    			printf("The array has %d integers with the value of %d.", integercount, n);
    		
    
    		}
    	while (n < 100);
    
    	if (n > 100)
    		printf("The number is greater than 99, please enter a new one. \n");
    
    
    }
    The error says that the variable 'data' is being used without being initialized.

  4. #4
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    first off, make a new variable (int) named count. instead of increasing intcount,
    raise count (not intcount) instead.

    second, declare your variables outside int main().

    third, replace intcount with count.

    if you dont get what i said, here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    	int data[1000] = {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 n;
    	int integercount;
                    int count = 0;
    
    int main(void)
    
    {
    
    
    	do{
    		printf("Enter an integer in the range of 0 to 99, inclusive: ");
    		scanf("%d", &n);
    		
    		for (integercount = 0; integercount < 999; integercount++)
    		if( data[1000] == n)
    			count++;
    			printf("The array has %d integers with the value of %d.", count, n);
    		
    
    		}
    	while (n < 100);
    
    	if (n > 100)
    		printf("The number is greater than 99, please enter a new one. \n");
    
    
    }

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    27
    Ok, I've tried that, but for some reason, the count is giving me 1000. Its detecting how many variables are in the array but arent specifically counting the ones that are inputted as &n. I changed the "integercount" to simply "count" like you added, but I'm still getting the wrong count.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Sure it is :
    Code:
    int data[1000]
    .
    .
    if( data[1000] == n)

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    if( data[1000] == n)
    What do you think this line of code is doing?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    Quote Originally Posted by vart View Post
    Code:
    if( data[1000] == n)
    What do you think this line of code is doing?
    i will tell you. it is used to see if element 1000 of array data is
    equal to N (input) or not. however, there is a mistake in here.
    1000 is static, so if you type, lets say, twelve. you will get no
    results since that the end of the array is not 12 but some other
    number while before it are twelves. anyway, data[1000] should
    be data[count]...

  9. #9
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    btw, jaja, listen carefully. IntCount and Count should be seperate, you
    should NOT rename these instead of making new ones. try the code i
    gave you two posts above and you will get what i meant. if it gives you
    always 1000, its because you are giving the %d parameter to count.

    here is the algorithm (not code):

    Code:
    Load Libraries
    Setup Variables And Arrays
    
    Start Main Function
      Start DO Loop
       Get Input
        Start FOR Loop with count set to zero and loop to 999
         Is Data[count] equal to N? if so then increase IntCount 
        Continue FOR Loop
       Display IntCount
      Continue While n is less than 100
    End Main Function
    is it clear now?
    Last edited by UltraKing227; 03-12-2010 at 11:34 PM.

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by UltraKing227 View Post
    i will tell you. it is used to see if element 1000 of array data is...
    actually it checks the element 1001 and thus the array has only 1000 elements indexed rom 0 to 999 - it actually is out of bounds access
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM
  2. Trying to count numbers in file
    By ammochck21 in forum C++ Programming
    Replies: 25
    Last Post: 11-17-2006, 11:32 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. How to complete program. Any probs now?
    By stehigs321 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 03:03 PM
  5. arrays
    By john_murphy69 in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 01:33 PM

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