Thread: Wrong output

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    3

    Wrong output

    I wrote a small program in C. This program adds the squares of two numbers and checks if the square root of the sum is an integer or not.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define N 50
    
    int main(int argc, char *argv[])
    {
      int i,j;
      int ar[N][N];
      float sq,res;
      
      for(i=1;i<=N;i++)
      for(j=1;j<=N;j++)
      {
          ar[i][j]=i*i+j*j;
          sq=sqrt(ar[i][j]);
          res=sq-(int)sq;
          if((res==0) && (i<=j)) {printf("i=%d j=%d and root is %f",i,j,sq);
          printf("\n");}
      }   
      printf("\n");
      system("PAUSE");	
      return 0;
    }
    The program works ok except the last line. It prints:

    Code:
    i=3 j=4 and root is 5.000000
    i=5 j=12 and root is 13.000000
    i=6 j=8 and root is 10.000000
    i=7 j=24 and root is 25.000000
    i=8 j=15 and root is 17.000000
    i=9 j=12 and root is 15.000000
    i=9 j=40 and root is 41.000000
    i=10 j=24 and root is 26.000000
    i=12 j=16 and root is 20.000000
    i=12 j=35 and root is 37.000000
    i=14 j=48 and root is 50.000000
    i=15 j=20 and root is 25.000000
    i=15 j=36 and root is 39.000000
    i=16 j=30 and root is 34.000000
    i=18 j=24 and root is 30.000000
    i=20 j=21 and root is 29.000000
    i=20 j=48 and root is 52.000000
    i=21 j=28 and root is 35.000000
    i=24 j=32 and root is 40.000000
    i=24 j=45 and root is 51.000000
    i=27 j=36 and root is 45.000000
    i=28 j=45 and root is 53.000000
    i=30 j=40 and root is 50.000000
    i=33 j=44 and root is 55.000000
    i=36 j=48 and root is 60.000000
    i=40 j=42 and root is 58.000000
    i=50 j=2504 and root is 0.000000
    I cannot understand why j=2504!??

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    j and i are used to index your array. Assuming that either of them reaches 50, you are past the end of your array, and are now into BadThings(TM). Array indexes begin and go to size - 1.

    Thus, if you have #define N 50, where N is used for your array sizes; you cannot access array[ 50 ][ ... ]. Neither can you access array[ ... ][ 50 ].


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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ar[i][j]=i*i+j*j;
    > sq=sqrt(ar[i][j]);
    Since you don't use the array anywhere else, you may as well get rid of it altogether and go with
    double temp = i*i+j*j;
    sq=sqrt(temp);

    Then you can go to much larger values without having to cope with very large arrays.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    3
    Thank you!

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The problem in your original code was these two lines:
    Code:
      for(i=1;i<=N;i++)
      for(j=1;j<=N;j++)
    ->
    Code:
      for(i=1;i<N;i++)
      for(j=1;j<N;j++)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wrong Output!
    By kolliash in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2008, 07:55 AM
  2. Something Wrong with my function in Linux!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 04-30-2008, 10:00 PM
  3. Getting wrong output from a class
    By orikon in forum C++ Programming
    Replies: 11
    Last Post: 11-18-2005, 07:58 PM
  4. Why is the output of this wrong?
    By Tokimasa in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2004, 01:58 PM
  5. Leap year program prints wrong output
    By Guti14 in forum C Programming
    Replies: 8
    Last Post: 08-24-2004, 11:56 AM