Thread: Help Please

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Help Please


    I was wondering if there was a way to insert text into some sort of array like function. Below is a sample of my code. I need to print out "Yes" in case sum is divisble by x and "No" in the opposite case in the order it occurs after all the text is inserted.

    Code:
    for(i=0;i<n;i++)
        {
                printf("\n");
                scanf("%d", &x);
                sum=0;
                for(sum=0;x>0;x--)
                {
                                  scanf("%d", &c);
                                  sum=sum+c;
                }
                if(sum%x==0)
                            A[i]="YES";
                else
                            A[i]="NO";
        }
        for(i=0;i<n;i++)
                        printf("%c", A[i])
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Well if you have something like
    char *a[100];

    And change your final printf to use %s rather than %c, then it should work.
    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
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    2

    Floating point exception


    Originally Posted by lalaman
    I was wondering if there was a way to insert text into some sort of array like function. Below is a sample of my code. I need to print out "Yes" in case sum is divisble by x and "No" in the opposite case in the order it occurs after all the text is inserted.

    Code:
    for(i=0;i<n;i++)
        {
                printf("\n");
                scanf("%d", &x);
                sum=0;
                for(sum=0;x>0;x--)
                {
                                  scanf("%d", &c);
                                  sum=sum+c;
                }
    
    --------------- X is 0 --------------
                if(sum%x==0)
                            A[i]="YES";
                else
                            A[i]="NO";
        }
        for(i=0;i<n;i++)
                        printf("%c", A[i])
    X will be zero due to decrement operation in the for loop, So it will end up throwing Floating point exception..

IMN logo majestic logo threadwatch logo seochat tools logo