Thread: saurabh

  1. #1
    Registered User
    Join Date
    Dec 2016
    Location
    india
    Posts
    2

    saurabh

    i have an array of negative and positive numbers.
    i have sorted dat in pos-neg format...
    and i m printing it like pos,neg,pos,neg.
    but its working only when pos-neg elements are same(even)

    can anyone help me out in odd condition
    ??

    its printing garbage value and i dont want that.


    Code:
    #include<stdio.h>
    int main()
    {
    int a[8]={1,8,2,-3,4,-5,-6,-7};
    int i,j,n;
    int k=0,l=0,p=0,q=0;
    int pos[8],neg[8];
    clrscr();
    
    for(j=0;j<8;j++)
    {
    if(a[j]>=0)
    {
    pos[k]=a[j];
    k++;
    }
    else
    {
    neg[l]=a[j];
    l++;
    }
    }
    
    
    for(i=0;i<8;i++)
    {
    if(i%2==0)
    {
    if(pos[p]=='\n')
    continue;
    else
    {
    printf(" %d ",pos[p]);
    p++;
    }
    }
    else
    {
    if(neg[q]=='\n')
    continue;
    else
    {
    printf(" %d ",neg[q]);
    q++;
    }
    }
    }
    Last edited by Saurabh Khamkar; 12-30-2016 at 02:38 AM.

  2. #2
    Registered User
    Join Date
    Dec 2016
    Location
    india
    Posts
    2
    can any body help me
    ??

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    First off, you need to learn how to indent code.
    Code:
    #include<stdio.h>
    int main()
    {
      int a[8] = { 1, 8, 2, -3, 4, -5, -6, -7 };
      int i, j, n;
      int k = 0, l = 0, p = 0, q = 0;
      int pos[8], neg[8];
      clrscr();
    
      for (j = 0; j < 8; j++) {
        if (a[j] >= 0) {
          pos[k] = a[j];
          k++;
        } else {
          neg[l] = a[j];
          l++;
        }
      }
    
    
      for (i = 0; i < 8; i++) {
        if (i % 2 == 0) {
          if (pos[p] == '\n')
            continue;
          else {
            printf(" %d ", pos[p]);
            p++;
          }
        } else {
          if (neg[q] == '\n')
            continue;
          else {
            printf(" %d ", neg[q]);
            q++;
          }
        }
      }
    }
    Second, your name isn't a proper thread title.
    How To Ask Questions The Smart Way

    > if (pos[p] == '\n')
    What do you expect this to do?
    '\n' has the decimal value 10, but that appears nowhere in your data.

    Perhaps more meaningful variable names would help, for example
    Code:
      for (j = 0; j < 8; j++) {
        if (a[j] >= 0) {
          positiveElements[numPositives] = a[j];
          numPositives++;
        } else {
          negativeElements[numNegatives] = a[j];
          numNegatives++;
        }
      }
    Now use numPositives and numNegatives in your next loop to determine if you should print something from the corresponding array.
    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

Tags for this Thread