Thread: Comparing each element of one array with another

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    6

    Comparing each element of one array with another

    Hi! I would like to ask you, how to compare each element of array with another,defined ? I have tried this,but didn't work.

    Code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void bill()
    {
    float bill;
    printf("Enter value of your bill: ");
    scanf("%f",&bill);
    
    while (bill<0 || bill>10000)
        {
        printf("Wrong input\n");
        break;
        }
    
    }
    
    int main()
    {
    char p[1000];
    char val[1000];
    int length = 0;
    char* itr;
    char notes[]={100,50,20,10,5,2,1,0.50,0.20,0.10,0.05,0.02,0.01};
    
    bill();
    while(getchar()!='\n');
    
    
    
    fgets(p,sizeof(p),stdin); 
    
    
    itr = strtok(p, " ");
    while(itr!=NULL)
        {
            val[length]=atof(itr);
            itr = strtok(NULL, " ");
            length ++;
            val[length]= val[length];
    
        }
        
    itr = strtok(p, " \n");
    
    for(int k=0;notes[k];k++) //comparing
    {
    for(int i=0;i<length;i++)
        {
        if(p[i]!=notes[k])
        {
        
        printf("%c is invalid\n",p[i]);
        }
        } }
    
    return 0;
    }
    Thanks for help !
    Last edited by Jakub Elia; 03-15-2015 at 04:47 AM. Reason: edited code

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Here are some things to think about.
    1. bill() doesn't return anything.
    2. bill() also has a local variable called bill. Whilst not illegal, it does lead to an awful lot of confusion.
    3. notes is an array of char, yet you're trying to store floats in it.

    Your indentation could be better.

    You need to say what you typed in by way of user input for us to make much more sense of the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    6
    You're right. But what should bill() return? I tried local variable and 0,but compiler didn't compile it. And I typed in for example : 20 30 60 100

    Here is edited code :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void bill()
    {
        float b;
        printf("Enter value of your bill: ");
        scanf("%f",&b);
    
        while (b<0 || b>10000)
        {
            printf("Wrong input\n");
            break;
        }
        
    }
    
    int main()
    {
        char p[1000];
        char val[1000];
        int length = 0;
        char* itr;
        float notes[]={100,50,20,10,5,2,1,0.50,0.20,0.10,0.05,0.02,0.01};
    
        bill();
        while(getchar()!='\n');
    
    
    
        fgets(p,sizeof(p),stdin); 
    
    
    
        itr = strtok(p, " ");
        while(itr!=NULL)
        {
            val[length]=atof(itr);
            itr = strtok(NULL, " ");
            length ++;
            val[length]= val[length];
    
        }
        
        itr = strtok(p, " \n");
        for(int k=0;notes[k];k++)
        {
            for(int i=k+1;i<length;i++)
            {
                if(p[i]!=notes[k])
                {
        
                    printf("%c is invalid\n",p[i]);
                }
            } 
        }
    
        return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > You're right. But what should bill() return?
    What did you imagine it should do when you wrote it ?

    It seems to me you should be returning the input value.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Only the last element of the array works.
    By LAURENT* in forum C++ Programming
    Replies: 17
    Last Post: 10-06-2014, 07:12 PM
  2. remove array element
    By Ash Mo Naiming in forum C Programming
    Replies: 6
    Last Post: 01-30-2013, 09:57 AM
  3. array of ptrs vs constant ptr to an first element of array
    By monkey_c_monkey in forum C Programming
    Replies: 5
    Last Post: 08-30-2012, 11:39 PM
  4. size of an array poited by array element
    By mitofik in forum C Programming
    Replies: 7
    Last Post: 12-24-2010, 12:09 AM
  5. How to add/subtract from array element...
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 05-01-2002, 10:04 PM

Tags for this Thread