Like Tree1Likes
  • 1 Post By Bayint Naung

Funny array behaviour. Please Help!

This is a discussion on Funny array behaviour. Please Help! within the C Programming forums, part of the General Programming Boards category; Dear all, I have been having trouble with this code . This program is supposed to brute-force verbal arithmetic, but ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    14

    Funny array behaviour. Please Help!

    Dear all,

    I have been having trouble with this code . This program is supposed to brute-force verbal arithmetic, but when it is testing large numbers, the values in array x1 screw up badly. They become negative and I have given up. Could anyone please tell me what's wrong? This same error came up in another program I tried to write.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    int pows(int a,int b)
    {
        int p=1;
        for(;b>0;b--)
        {
            p*=a;
        }
        return p;
    }
    
    int check(int data[],int dataCheck[],int k)
    {
        int i;
        int flag=1;
        for(i=0;i<k;i++)
        {
            if(data[i]!=dataCheck[i])
            {
                flag=0;
                break;
            }
        }
        return flag;
    }
    int sortNum(int x1[],int x)
    {
        int i,j;
        int y=x;
        for(i=0;x!=0;i++)
        {
            x/=10;
        }
        x1[i]=10;
        x1[i+1]=10;
        j=i;
        for(i--;i>=0;i--)
        {
            x1[i]=y%10;
    
            y/=10;
        }
    
    
    
        return j;
    }
    int main()
    {
      char a[30],b[30],c[30];
      char x[30];
      int x1[30];
      int aNum,bNum,cNum;
      int data[60]={0},dataCheck[59]={0};
      int aLen,bLen,cLen;
      int cLent,cNumt;
      int xLen;
      int i,j,m,k=0,k1=0;
    
    
      fgets(a,10,stdin);
      fgets(b,10,stdin);
      fgets(c,11,stdin);
    
    
      aLen=strlen(a);
      bLen=strlen(b);
      cLen=strlen(c);
    
      a[aLen-1]='\0';
      b[bLen-1]='\0';
    
      strcpy(x,a);
      strcat(x,b);
      strcat(x,c);
    
      for(i=0;x[i]!='\n';i++)
      {
          for(j=i+1;x[j]!='\n';j++)
          {
              if(x[i]==x[j])
              {
                  data[k]=i;
                  data[k+1]=j;
                  k+=2;
                  break;
              }
          }
    
      }
    
      for(m=0;m<pows(10,aLen+bLen-2);m++)
      {
          aNum=m%(pows(10,aLen-1));
          bNum=(m-aNum)/pows(10,aLen-1);
          cNum=aNum+bNum;
          cNumt=cNum;
          for(cLent=0;cNumt!=0;cLent++)
          {
              cNumt/=10;
          }
          xLen=sortNum(x1,(aNum*pows(10,bLen+cLent-1)+bNum*pows(10,cLent)+cNum));
    
          if(xLen!=aLen+bLen+cLen-3)
          {
              continue;
          }
          for(i=0,k1=0;i<xLen;i++)
          {
              for(j=i+1;j<xLen;j++)
              {
                  if(x1[j]==x1[i])
                  {
                      dataCheck[k1]=i;
                      dataCheck[k1+1]=j;
                      k1+=2;
                      break;
                  }
              }
    
          }
    
          if(k!=k1)
          {
              continue;
          }
           
    
          if(check(data,dataCheck,k)==1)
          {
              
              printf("%d+%d=%d\n",aNum,bNum,cNum);
          }
         
    
      }
      return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sounds like you are simply overflowing your variable. See variables have a specific range they can hold. If you try to go beyond those ranges, you run into situations like what you are encountering. Use smaller numbers, or use a bigger data type.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    14
    Quote Originally Posted by quzah View Post
    Sounds like you are simply overflowing your variable. See variables have a specific range they can hold. If you try to go beyond those ranges, you run into situations like what you are encountering. Use smaller numbers, or use a bigger data type.


    Quzah.
    what do you mean by overflowing? what are the ranges? To use a bigger data type do i use long unsigned int or do I increase the array size? Thanks in advance.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You can test the ranges using the MAX values....
    For example...
    Code:
    printf("%d", INT_MAX);
    The reason they don't say "an int can hold values between -6000 and + 6000 is that it won't be the same on on compilers or on all machines... On a 16 bit system INT_MAX is 32767 ... on a 32 bit system it's 2147483647 ... and as soon as compilers catch up on 64 bit systems it wil be... 9223372036854775807 ( the current value for long long int)...

    Frankly I think this is a terrible mistake made back in the beginnings of the language... they should have standardized by bit sizes int16 int32 int 64 etc. Many libraries have these definitions but they are simply aliases for the native types...

    So what happens when you add 1 to each of those values? Yep, you end up with -1.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Standard grantee minimum value. Question 1.1
    As CT suggested, you can print MAX value. Take a look at limits.h
    C99 introduced stdint.h - Wikipedia, the free encyclopedia
    CommonTater likes this.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    14
    Thanks a lot! I'll try to correct it...

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    14
    I fixed that issue, but some of the array values that I set to 0 initially and subsequently did not modify, start to have values? What's going on???

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That probably means you've walked off the end of one array and wandered into another.

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    14
    But that happened within the range of the array. How can it be modified by some phantom?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by polarbear View Post
    I fixed that issue, but some of the array values that I set to 0 initially and subsequently did not modify, start to have values? What's going on???
    Try posting your new code, and sample output (assuming it's compiling without warnings).


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by polarbear View Post
    But that happened within the range of the array. How can it be modified by some phantom?
    Ok think about 2 arrays declared in sequence...
    Code:
    char a[16], b[16];
    These will be in memory like this...
    Code:
    AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBB
    C does not do range checking... so you can easily walk off the end of one array into the beginning of the next...

    Code:
    AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBB
    xxxxxxxxxxxxxxxxxxxxxxxx
    So now you are writing to a[i] but actually changing the contents of array b.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused by behaviour
    By rocketman50 in forum C++ Programming
    Replies: 3
    Last Post: 03-15-2010, 11:55 AM
  2. weird behaviour with abs()
    By crazyinlove in forum C Programming
    Replies: 19
    Last Post: 02-28-2010, 12:58 AM
  3. Strange behaviour of GCC
    By BlackOps in forum C Programming
    Replies: 14
    Last Post: 07-29-2009, 07:44 PM
  4. Unexpected behaviour
    By fnoyan in forum C++ Programming
    Replies: 5
    Last Post: 03-05-2005, 09:45 AM
  5. strange behaviour.......
    By surdy in forum C Programming
    Replies: 2
    Last Post: 05-01-2004, 12:50 PM

Tags for this Thread


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