Creating tables from a program?

9 ビュー (過去 30 日間)
Kyle
Kyle 2011 年 3 月 3 日
コメント済み: Isaias Santiago 2014 年 12 月 5 日
Ok the question was, Write the MATLAB statements required to calculate and print out the squares of all even integers from from 0 to 50. Create a table consisting of each integer and its square, with the appropriate labels over each column(right alligned.
In other words:
Integer Square
======= ======
0 0
2 4
. .
. .
50 2500
Here's my program:
cnt = 0;
for z = 0:2:50
cnt = cnt + 1;
x(cnt)=z^2;
fprintf('the interger %g has a square value of %g\n', z,x(cnt))
end
The fprintf was just so see if the program worked.
Thanks!
  2 件のコメント
Jan
Jan 2011 年 3 月 3 日
Do you have a question?
Isaias Santiago
Isaias Santiago 2014 年 12 月 5 日
x = -2;
y = 0;
maxy = 2304;
limity = 2305;
fprintf ('integer square \n')
fprintf ('------- ------ \n')
while (maxy < limity)
x = x + 2;
y = x^2;
ynew = y;
maxy = ynew;
fprintf ('%3i %3i\n', x, ynew)
end

サインインしてコメントする。

採用された回答

Matt Fig
Matt Fig 2011 年 3 月 3 日
Only using FPRINTF:
z = (0:2:50);
fprintf('\n\n%s %s\n%s %s\n','Integer','Square','=======','======')
fprintf( '\t%3.f\t\t%4.f\n', [z;z.^2])
  2 件のコメント
Jan
Jan 2011 年 3 月 3 日
Let me mention the "squares of all *even* integers from from 0 to 50" again.
Matt Fig
Matt Fig 2011 年 3 月 8 日
Thank you Jan!

サインインしてコメントする。

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2011 年 3 月 3 日
You could also do:
z = (0:50).';
fprintf(' Integer, Integer^2\n-------------------\n');
disp([z,z.^2]);
  4 件のコメント
Walter Roberson
Walter Roberson 2011 年 3 月 3 日
Sean,
fprintf('Header\n%s', sprintf('%f %f\n', [z,z.^2].'))
Oleg Komarov
Oleg Komarov 2011 年 3 月 8 日
That's a trick...

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by