Unusual Output from C Array Code -



Unusual Output from C Array Code -

as simple effort @ coding exercise in book i've been reading, wrote next code:

#include <stdio.h> int main() { double data[12][5]; int i; double t = 2.0; for(i = 0; < 12; i++) { data[i][0] = t; t += 0.1; } for(i = 0; < 12; i++) { data[i][1] = 1.0 / data[i][0]; data[i][2] = data[i][0] * data[i][0]; data[i][3] = data[i][2] * data[i][0]; data[i][4] = data[i][3] * data[i][0]; } printf("x 1/x x^2 x^3 x^4\n"); int row; int column; for(row = 0; row < 12; row++) { printf("%10lf %10lf %10lf %10lf %10lf\n", data[i][0], data[i][1], data[i][2], data[i][3], data[i][4]); } homecoming 0; }

however, when run output appears @ ideone.com: http://ideone.com/klwtdk. according how think code should run, far left column should range of values inclusive 2.0 3.0 0.1 step size. not case, however.

also, while chatting on irc, told not utilize tabs when printing tables of info instead utilize printf widths. want able have header on each column in text, - there way that?

your problem declare iteration variable row, utilize i index in print statement. since value of i independent of value of row, every iteration give same values, , won't right ones.

you can resolve accessing array elements data[row][column_index] e.g. data[row][0]

regarding formatting, mentioned @jonathanleffler:

the way labels aligned

printf("%10s %10s %10s %10s %10s\n", "x", "1/x", "x^2", "x^3", "x^4");

use same width in string formats in numeric formats. right justifies labels; left justify, utilize %-10s instead.

c arrays

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -