Thread: getting wrong output

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    25

    getting wrong output

    im guessing its when it comes to culculating the initial vector, as u can see from the out put.....

    New vector:
    y[1]=[89806308033758335519957510699863969348510599027283 42001933875355046869783389222623707753599574476432 94137672174519651927335372003439184892435557960885 10866209917577152863227861185710903110831001937129 87506259980859399539001668422094856393681307990309 76439107536483051236436899063108141629011245169671 88791296.000000 -11225788504219791939994688837482996168563824878410 42750241734419380858722923652827963469199946809554 11767209021814956490916921500429898111554444745110 63858276239697144107903482648213862888853875242141 23438282497607424942375208552761857049210163498788 72054888442060381404554612382888517703626405646208 985989120.000000 44903154016879167759978755349931984674255299513641 71000966937677523434891694611311853876799787238216 47068836087259825963667686001719592446217778980442 55433104958788576431613930592855451555415500968564 93753129990429699769500834211047428196840653995154 88219553768241525618218449531554070814505622584835 94395648.000000 -22451577008439583879989377674965992337127649756820 85500483468838761717445847305655926938399893619108 23534418043629912981833843000859796223108889490221 27716552479394288215806965296427725777707750484282 46876564995214849884750417105523714098420326997577 44109776884120762809109224765777035407252811292417 97197824.000000 ]
    The length of this vector is: Inf
    Normalized new vector:
    x[1]=[0.000000 -0.000000 0.000000 -0.000000 ]


    New vector:
    y[2]=[0.000000 0.000000 0.000000 0.000000 ]
    The length of this vector is: 0.000000
    Normalized new vector:
    x[2]=[NaN NaN NaN NaN ]

    then it continues to y[10] and all i get is NaN from y2 to y10
    i cant figure out what is wrong with it....here is the part of the code i feel might need fixing.

    insert
    Code:
    for(n=0;n<=3;n++) printf("%f ",x[n]);printf("]\n");
     for(k=1;k<=10;k++) 
      {
       for(n=0;n<=3;n++) x[n]=z[n];
         matrix_mult(K,x,y,4); 
         printf("\nNew vector:\ny[%d]=[",k);
       for(n=0;n<=3;n++) printf("%f ",y[n]);
         printf("] \nThe length of this vector is: %f\n",unit_norm(y,4));
       for(n=0;n<=3;n++) z[n]=y[n]/unit_norm(y,4);
         printf("Normalized new vector:\nx[%d]=[",k);
       for(n=0;n<=3;n++) printf("%f ",z[n]); 
         printf("] \n\n");
      }
    }
    
    
    void matrix_mult(double a[4][4],double b[4],double c[4],int rows)
    {
       int k;
       double dot_p;
     for(rows=0;rows<4;rows++)
      {
       for(k=0,dot_p=0.;k<4;k++) dot_p +=a[rows][k]*b[k];
         c[rows]=dot_p;
      }
       return;
    }
    
    double unit_norm(double vec[4],int cols)
    {
       double sum;
       sum=sqrt(pow(vec[0],2)+pow(vec[1],2)+pow(vec[2],2)+pow(vec[3],2));
       return sum;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    for(n=0;n<=3;n++) z[n]=y[n]/unit_norm(y,4);
    printf("Normalized new vector:\nx[%d]=[",k);
    for(n=0;n<=3;n++) printf("%f ",z[n]);

    Well, since the unit_norm returned zero as per your previous results, then you're dividing by zero!
    That produces NaN's (Not A Number)
    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

Similar Threads

  1. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  2. Wrong Output!
    By kolliash in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2008, 07:55 AM
  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

Tags for this Thread