Thread: promming doubt

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    2

    programming doubt

    i have a doubt.its about a pgm on polynomial addition.the q is that the poly nomial should be read as nos followed by spaces and terminated by -1-1eg 1 2 1 3 -1-1means1x2+1x3.max no of terms is 15.i have done ythe following pgm to do this
    Code:
    for(i=0,j=0;j<15;j++)
    {
    scanf("%d",&a[i][j]);
    if((a[i][j]==-1)&&(a[i][j--]==-1))
    break;
    }
    for(m=0,n=0;n<15;n++)
    {
    scanf("%d",&b[m][n]);
    if((b[m][n]==-1)&&(b[m][n--]==-1))
    break;
    }
    during exe i am not able to the second scanf for 'b'.why is it so?
    Last edited by smitha; 09-16-2005 at 08:26 PM. Reason: spell check

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    29
    Clear stdin before the second call to scanf().

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    2
    may i know how?

  4. #4

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > during exe i am not able to the second scanf for 'b'.why is it so?
    Because you're not reading in pairs of numbers (despite your use of 2D arrays)

    scanf("%d",&a[i][j]);
    Consumes the first -1 and exits the first loop

    scanf("%d",&b[m][n]);
    Consumes the second -1 and exits the second loop (without apparently doing anything)
    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.

  6. #6
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    for polynomial addition,using stack is also an option.


    regards
    varun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubt in pointer.
    By shwetha_siddu in forum C Programming
    Replies: 5
    Last Post: 03-21-2009, 01:28 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Doubt abt Storage!
    By kalamram in forum C Programming
    Replies: 1
    Last Post: 04-21-2006, 05:30 AM
  4. Float vars doubt
    By sevensuns in forum C++ Programming
    Replies: 1
    Last Post: 08-01-2004, 07:57 PM
  5. Greatest C++ Doubt
    By vasanth in forum C++ Programming
    Replies: 15
    Last Post: 02-28-2002, 04:41 AM