Thread: ASCII values

  1. #31
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    I am currently trying to get the program to search for more than one character. This has been successful up to a certain stage.

    For some reason the program is reading the count for the first character and the second the same. I have looked through the code a few times and still stuck. Any help would be much appreciated

    Here is the code.
    Code:
    #include<stdio.h> 
    int main(void)
    {
     
     
       int array[100], search, c, n, count = 0;
       int             search2, c1, n1, count2 = 0;
     
       printf("Enter the number of elements in array\n");
       scanf("%d",&n);
     
       printf("Enter %d numbers\n", n);
     
       for ( c = 0 ; c < n ; c++ )
          scanf("%d",&array[c]);
       for ( c1 = 0 ; c1 < n ; c1++)
       
       printf("Enter the number to search\n");
       scanf("%d",&search);
       printf("enter second number to search\n");
       scanf("%d", &search2);
     
       for ( c = 0 ; c < n ; c++ )
       for ( c1 = 0; c1< n ; c1++)
       {
          if ( array[c] == search )
          if ( array[c1] == search2)     
          {
             printf("%d is present at location %d.\n", search, c+1);
        count++;
             printf("%d is present at location22i3rwefkhc %d.\n", search2, c1+1);
        count2++;     
          }
       }
       if ( count2 == 0 )
          printf("%d is not present in array.\n", search2);
        else
          printf("%d is present %d times in array.\n", search2, count2);   
          
          
          
          
          
       if ( count == 0 )
          printf("%d is not present in array.\n", search);
       else
          printf("%d is present %d times in array.\n", search, count);
        
         
             
      
    
    
          
       
          
       return 0;
       
     }

  2. #32
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by SDH View Post
    I am currently trying to get the program to search for more than one character. This has been successful up to a certain stage.
    This might be related to your problem

    Code:
    for ( c = 0 ; c < n ; c++ )
          for ( c1 = 0; c1 < n ; c1++)
          {
             if ( array[c] == search )
                if ( array[c1] == search2)     
                {
                   printf("%d is present at location %d.\n", search, c+1);
                   count++;
                   printf("%d is present at location22i3rwefkhc %d.\n", search2, c1+1);
                   count2++;     
                }
          }
    This is a nested for loop. For example, if n == 16, then the loop will run 256 times. Is that what you want? If you want to go through each array, and each array is n==16 elements, then you probably want two loops, one after the other.

    Also, don't write if(FOO) if (BAR). If you want to say that both FOO and BAR must be true, the logical way (clearer to read and understand) is "if (FOO && BAR)"

  3. #33
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    This is a nested for loop. For example, if n == 16, then the loop will run 256 times. Is that what you want? If you want to go through each array, and each array is n==16 elements, then you probably want two loops, one after the other.
    I think I have now sorted out the problem, by running the first loop to search for the first number and how many times it occurs. Then start the second one after that is complete. I am now having problems with the second number to search for though. It for some reason thinks that I am asking it to search for an 8. Any ideas on why this is occuring would be a big help, thanks.
    Code:
     #include<stdio.h> 
    int main(void)
    {
     
     
       int array[100], search, c, n, count = 0;
     
       printf("Enter the number of elements in array.");
       scanf("%d",&n);
       printf ("There is %d numbers in the array.\n", n);
     
       printf("Enter %d numbers\n", n);
     
       for ( c = 0 ; c < n ; c++ )
          scanf("%d",&array[c]);
     
       printf("Enter the number to search\n");
       scanf("%d",&search);
     
       for ( c = 0 ; c < n ; c++ )
       {
          if ( array[c] == search )    
          {
             printf("%d is present at location %d.\n", search, c+1);
        count++;
          }
       }
      
       if ( count == 0 )
          printf("%d is not present in array.\n", search);
       else
          printf("%d is present %d times in array.\n", search, count);
       if ( count == 2)
          printf("One Pair.\n"); 
       if ( count == 3)
          printf("Three of a kind.\n");
       ///////////////////////////////////////////////////////////
       ///////////////////////////////////////////////////////////   
          
       int search2, c2, count2 = 0;
    
    
     
       for ( c2 = 0 ; c2 < n ; c2++ )
          scanf("%d",&array[c2]);
     
       printf("Enter the number to search\n");
       scanf("%d",&search2);
     
       for ( c2 = 0 ; c2 < n ; c2++ )
       {
          if ( array[c2] == search2 )    
          {
             printf("%d is present at location %d.\n", search2, c2+1);
        count2++;
          }
       }
      
       if ( count2 == 0 )
          printf("%d is not present in array.\n", search2);
       else
          printf("%d is present %d times in array.\n", search2, count2);
       if ( count2 == 2)
          printf("One Pair.\n"); 
       if ( count2 == 3)
          printf("Three of a kind.\n");   
          
          
             
          
         return 0;
       }

  4. #34
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Sorry, have just found what the problem was. I was asking it look for a input to then say the array has that many values. So I have just removed this and it has sorted the problem.

    How would I modify this program to either read a number or letter, the letters represent j,q,k,a?
    thanks
    Last edited by SDH; 01-12-2013 at 06:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ascii values
    By caliber005 in forum C Programming
    Replies: 6
    Last Post: 05-18-2011, 03:16 PM
  2. ASCII values help
    By Grant_Searle in forum C Programming
    Replies: 5
    Last Post: 10-22-2010, 09:01 AM
  3. Using ASCII Values
    By ga836044 in forum C Programming
    Replies: 7
    Last Post: 03-17-2004, 01:31 AM
  4. adding ASCII values
    By watshamacalit in forum C Programming
    Replies: 1
    Last Post: 12-26-2002, 07:16 PM
  5. Testing for ASCII values?
    By csmatheng in forum C Programming
    Replies: 1
    Last Post: 02-19-2002, 02:22 PM