error while using #define. plz help!!

This is a discussion on error while using #define. plz help!! within the C Programming forums, part of the General Programming Boards category; n numbers are entered from the keyboard into an array. The number to be searched is also entered by the ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    82

    error while using #define. plz help!!

    n numbers are entered from the keyboard into an array. The number to be searched is

    also entered by the user. Write a program to find if the number to be searched is present in the array and if
    it is present, display the number of times it appears in the array.

    Sample Input #1: (e.g. if n is set as 10 i.e. #define n 10)
    Enter 10 numbers : 10 20 23 -6 52 34 22 66 -22 -1
    Enter number to search: 40
    Sample Output #1: The number 40 is not found.



    Code:
    #include <stdio.h>
    #define i 10
    
    int main()
    
    {
        int nos[10]={0};
        int i=0,j=1,chck,count=0;
        
        for (i =0; i<10; i++){
            printf("Enter no %d: ",j);
            scanf("%d",&nos[i]);
            j++;
            }
            
            printf("Enter no to search for: ");
            scanf("%d",&chck);
            
        for (i=0; i<10; i++){
            if(nos[i]==chck){
                             count=count+1;
                             }
                             }
                             
                             
        if(count>0){
                    printf("Number %d is found %d times.",chck,count);
                    }
                    
                    printf("Number not found");
                             
        getch();
        return 0;
    }
    please help me removing this error and plz guide me a bit how to use #define especially with arrays.... I will be thankful to u...

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,443
    You #define i to be 10, then declare an int variable i. But the preprocessor first goes through and replaces all occurances of 'i' with 10 in your code, so it looks something like:
    Code:
    int main()
    
    {
        int nos[10]={0};
        int 10=0,j=1,chck,count=0;
    
        for (10 =0; 10<10; 10 ++){
            printf("Enter no %d: ",j);
            scanf("%d",&nos[10]);
            j++;
        }
    
        printf("Enter no to search for: ");
        scanf("%d",&chck);
    
        for (10=0; 10<10; 10 ++){
            if(nos[10]==chck){
                count=count+1;
            }
        }
    
    
        if(count>0){
            printf("Number %d is found %d times.",chck,count);
        }
    
        printf("Number not found");
    
        getch();
        return 0;
    }
    All that stuff in red is where i got replaced by 10. It's common convention to make anything you #define be all caps so you know what is a preprocessor symbol and what isn't. Also, picking a more sensible name than 'i' would be good, like MAX_NUMS. So try #define MAX_NUMS 10. Then your code should be like:
    Code:
    #define MAX_NUMS   10
    ...
    int nos[MAX_NUMS] = {0};
    ...
    for (i = 0; i < MAX_NUMS; i++)

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    82
    thank u 100X .. anduril462

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. difference between #define and #define ()
    By bored_guy in forum C Programming
    Replies: 8
    Last Post: 07-20-2009, 06:58 PM
  2. #define
    By aamirsherkhan in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2008, 06:42 AM
  3. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 06:31 AM
  4. Define Please
    By bumfluff in forum C++ Programming
    Replies: 4
    Last Post: 04-14-2006, 11:28 PM
  5. simple "#define" error
    By terracota in forum Windows Programming
    Replies: 5
    Last Post: 11-16-2004, 08:34 PM

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