Thread: Last row in array problem

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    5

    Last row in array problem

    The last row in this array of values is not coming out right.
    Do you mind taking a look and letting me know what I'm missing?
    Here is a screen cap of my output:

    Last row in array problem-output_capture-jpg

    All of the other rows look fine.
    This has been bugging me for a few hours, so any help will be awesome!


    Code:
    /* Header Files */
    #include <stdio.h>
    #include <math.h>
    
    
    /*defined constants*/
    
    
    #define max_column  5
    #define max_row     5
    #define Y            .05
    #define pi          3.14
    #define d_zeta       .05
    #define d_k     50000
    /*Start of main function*/
    int main(void)
    {
    
    
       /* declarations: */
        char c;
        double zeta = .4, k, alpha, alpha_n, r, X, amp[max_column][max_row];
        int column = 0, row, j, i;
       /*Statements that perform the desired task*/
        while(zeta <= .6)
        {
            row = 0;
            k = 300000;
            while(k <= 500000)
            {
                alpha = (2*pi*1.08);
                alpha_n = sqrt(k / 1200.0);
                r = alpha / alpha_n;
                amp[column][row] = Y * sqrt((1 + ((2 * zeta * r) * (2 * zeta * r))) / (((1 - (r * r)) * (1 - (r * r)) + (( 2 * zeta * r) * (2 * zeta * r)))));
                
                row ++;
                k = k + d_k;
            }
            zeta = zeta + d_zeta;
            column ++;
        }
    
    
        k = 300000;
       /*Printing the results*/
        printf("
     Amplitude of Vibrations at 20 kph
    
    ");
    
    
        printf("K\Damping Ratio|    ");
        zeta = .4;
        while(zeta < .61)
        {
            printf("%0.2f    ", zeta);
            zeta += d_zeta;
        }
        printf("
    ________________________________________________________
    ");
        
            for(row = 0; row < max_row; row++)
            {
                /*Print row label*/
                
                printf("   %6.0f      |    ", k);
                k += d_k;
                /*Print the data*/
                for( column = 0; column < max_column; column++)
                {
                    printf("%0.4f    ", amp[row][column]); 
                }
                printf("
    ");
                
            }
    
    
       /*This keeps the window open*/
       printf("
    
    Press enter to q to quit.
    ");
       do{ 
           c = getchar();
       }while( c != 'q' );
    
    
       /*end the program*/
       return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Your while(zeta <= .6) loop is executing one too few times due to the floating point comparison of .6 and zeta. You can simply adjust the comparison value - to .61 for instance.

    Or, instead of using zeta in your calculations use .4 + column * d_zeta and have the outer while loop be while(column < max_column). Get rid of zeta from the code.




    Code:
    char c;
    
    ...
    
    c = getchar();
    The getchar function returns an int, not a char. In this case it is just a nit but it can become important in the future to remember this fact and start doing things correctly sooner rather than later when the incorrect usage becomes ingrained.
    Last edited by hk_mp5kpdw; 04-05-2012 at 05:40 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    35
    Here you are inputting into 4*5 array
    Code:
    amp[column][row] = Y * sqrt((1 + ((2 * zeta * r) * (2 * zeta * r))) / (((1 - (r * r)) * (1 - (r * r)) + (( 2 * zeta * r) * (2 * zeta * r)))));
    Here you are displaying 5*5 array
    Code:
     for(row = 0; row < max_row; row++)
            {
                /*Print row label*/
    
                printf("   %6.0f      |    ", k);
                k += d_k;
                /*Print the data*/
                for( column = 0; column < max_column; column++)
                {
                    printf("%0.4f    ", amp[row][column]); 
                }
                printf("
    ");
                 
    


    Change max_row to 4... Then its fine..

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    Quote Originally Posted by hk_mp5kpdw View Post
    Get rid of zeta from the code.
    Thanks a lot, that did the trick. Makes total since in retrospect.


    Quote Originally Posted by hk_mp5kpdw View Post
    The getchar function returns an int, not a char
    [/QUOTE]

    My instructor gave us a template to use on our programs, and it includes that line.
    I will recognize that it is not good programming practice in the future.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem initializing a double array for large array
    By gkkmath in forum C Programming
    Replies: 4
    Last Post: 08-25-2010, 08:26 PM
  2. Problem converting from char array to int array.
    By TheUmer in forum C Programming
    Replies: 11
    Last Post: 03-26-2010, 11:48 AM
  3. array problem
    By nynicue in forum C Programming
    Replies: 3
    Last Post: 02-25-2009, 10:34 PM
  4. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  5. array problem
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 05-28-2002, 11:59 AM

Tags for this Thread