# Thread: Last row in array problem

1. ## 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:

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. 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.

3. 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. Originally Posted by hk_mp5kpdw
Get rid of zeta from the code.
Thanks a lot, that did the trick. Makes total since in retrospect.

Originally Posted by hk_mp5kpdw
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