Thread: Can anybody let me know how to solve this.

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    5

    Smile Can anybody let me know how to solve this.

    Question Follows:

    Twenty-five numbers are entered from the keyboard into anarray. The number to be searched is entered through the keyboard by the user. Write a program to find if the number to be searched is present in the array and if it is present, displaythe number of times it appears in the array.

    My Coding is this:
    Code:
    #include <stdio.h>
    
    main()
    {
        int nos[2];
        int a,i,f;
        for(a=1;a<=3;a++)
        {
        printf("Enter Numbers:");
        scanf("%d",&nos[a]);
        }
        printf("\nEnter a Number to be Found:");
        scanf("%d", &f);
               /*for(a=1;a<=3;a++)
                   printf("%d",nos[a]);*/
                      if(nos[a]==f)
            printf("\nNumber found");
            else
                printf("\nNumber Not Found");
        
            }
    what is wrong with above programme.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    array indexes start at 0 and end at the size of the array minus 1. your loops should be
    Code:
    for (a = 0; a < 2; a++)

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Use an extra variable to keep track of the finds:
    Code:
    #include <stdio.h>
    
    int main()    /* the proper way */
    {
        int nos[4];
        int a,f, total=0;      /* our new variable */
        for(a=0;a<=3;a++)
        {
        printf("Enter Numbers:");
        scanf("%d",&nos[a]);
        }
        printf("\nEnter a Number to be Found:");
        scanf("%d", &f);
        
        for(a=0;a<=3;a++) if(nos[a]==f) total++;
        if (total>0) printf("Number found %d times.\n",total);
        else printf("\nNumber Not Found");
        
        return 0;
    }
    You also had an overwrite in your last code, because you assign 2 elements to nos, then you count thru them 1,2,3! You actually overflow by 2, because the 2 elements of nos are numbered 0 and 1 (2 and 3 do not exist). Always remember the first element of an array is 0, not 1.

    So here the numbers match up: nos has 4 elements and we count thru them 0, 1, 2, and 3.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    Thank you verymuch guys.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    ok the programme works fine but want to know how this total++ does the work of getting the input of the variables in it.Can you please explain so that i will know the programme more detail.Thanks in advance

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Farhan View Post
    ok the programme works fine but want to know how this total++ does the work of getting the input of the variables in it.Can you please explain so that i will know the programme more detail.Thanks in advance
    It means that if an occurance of the number is found 1 is added to total. Itīs the same a writing total = total + 1;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can anyone solve this for me? (look inside)
    By johnsonswww in forum C Programming
    Replies: 10
    Last Post: 03-02-2009, 11:24 AM
  2. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  3. How to handle in software to solve this block diagram?
    By vnrabbit in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 02:45 PM
  4. Help to solve this problem
    By Romashka in forum C++ Programming
    Replies: 3
    Last Post: 04-16-2002, 09:32 AM