Looping Program to Generate Table of Values: A+2 from 3 to 15 using M-File

  • Thread starter flamex1992
  • Start date
In summary: Hello, world!\n');2. Use the printf function with a format string that includes a percent sign...printf('%s\n', 'Hello, world!\n');3. Use the strftime function...strftime('%Y-%m-%d %H:%M:%S', 'Hello, world!\n');4. Use the sprintf function...sprintf('%s\n', 'Hello, world!\n', 'Hello, world!\n');In any of these cases, the percent sign (%) is optional. If you omit the percent sign, the format string will
  • #1
flamex1992
6
0
Question
Write a program that utilizes looping to produce the following table of values:

A --A+2
3 --5
6 --8
9 --11
12 --14
15 --17

and my input is with using the m-file

for A=3:3:15
display ('A+2')
A=A+2
end


is it correct?
 
Physics news on Phys.org
  • #2
Your program gives only the values of A+2. Do you want table of values?
And the line
display('A+2')
is not required. It simply displays 'A+2' and won't calculate anything.
It will be better if you write like this

for i=1:5 % you need five pairs in table
x(i)=3*i;
y(i)=x(i)+2;
end
display(x)
display(y)
 
  • #3
tis is the question my lacturer giv it to me.. I'm still not really understand wat the question wan.. can u tell the detail pls?
i wuld aprreciate any helps

Write a program that utilizes looping to produce the following table of values:
A A+2 A+4 A+6
3 5 7 9
6 8 10 12
9 11 13 15
12 14 16 18
15 17 19 21
 
  • #4
Have you been asked to do it in Matlab?
You can use 'for loop' procedure in Matlab to accomplish this.
Start with small examples. Understand how to write coding for simple problems which will help you to realize the table you want.
 
  • #5
yup do it in matlab..

'for loop' procedure is like how?
the one tat i doing is called for loop?
 
  • #6
Yes, the lines I mentioned at #2 gives output for A A+2
Within the same loop itself you can add codes for getting A+4 and A+6
 
  • #7
thx.. u hav help me a lot now..
then again, how to add eq A+4 and A+6 in the same M-file rather than makin 3 m-files for 3 equations... isit possible btw?
 
  • #8
flamex1992 said:
thx.. u hav help me a lot now..
then again, how to add eq A+4 and A+6 in the same M-file rather than makin 3 m-files for 3 equations... isit possible btw?

Yes it is possible to do in single for loop.
Let me explain what is happening

for i=3:3:15

bla..
bla..

end

In the above for loop, the lines between 'for' and 'end' are executed 5 times for the values i=3,6,8,12,15.

if I include

disp(i)
disp(i+2)
disp(i+4)
disp(i+6)


between 'for' and 'end' it will display 3 5 7 9 in the first run
and 6 8 10 12 in the second run and so on.

Note in the line

for i=k:m:n

variable 'i' assumes value 'k' in first run value 'k+m' in second run and it continues till it equals value of n.
 
  • #9
I would save the values as rows in a matrix and use fprintf to display a table.
 
  • #10
can help my friend give a clue on her question ?
we both don't hav a clue on these question :confused:

a)
Write a program to convert centimeters to feet and inches. The input is number (floating, 2 decimal), in centimeters, the output should be the equivalent number of feet (integer) and inches (floating, 1 decimal), with the inches given to an accuracy of one decimal place.
Assume:
1 inch = 2.54 cm
12 inches = 1 foot

If the input value is 333.3, the output format should be:
333.3 centimeters is 10 feet 11.2 inches.
 
  • #11
You should be able to figure out the conversion logic, but for the display you can use something like:
Code:
fprintf('%1.1f centimeters is %i feet %1.1f inches.\n', ans);
(ans is a vector listing cm, ft, in)
You'll want to read about the fprintf function and string formatting.
 
  • #12
is the command right for the convert question? it seems i din understand about the end part like you stated 'fprintf' part..

clear all
close all
clc
C = input('cm: ');
X = (1/2.54)*C;
inch=X
foot=X*1/12
 
  • #13
I think it wants you to give the answer such that, if the answer is 130 in, it is displayed as 10 ft 10 in.

You have the conversion factor, 1 in = 2.54 cm. You can use this to get the value in inches. You can use integer division (idivide in MATLAB) to get how many feet there are, and then find the remainder/modulus too get the number of inches. (Either rem or mod in MATLAB.)

What you currently have won't accomplish that.

I suggest you work on the basic logic first before worrying about formatting and displaying the answer.
 
  • #14
Hmm...
i guess i get the picture of it...
but is there any other way to display the answer other than the command that u gave above?
 
  • #15
I suppose you could use a series of disp commands (I don't remember whether disp has an automatic newline \n).

Edit: see the documentation for http://www.mathworks.com/help/techdoc/ref/disp.html":
MATLAB Documentation said:
Example 3 — Display multiple items on the same line

Use any of the following techniques to display multiple items on the same line:

1. Concatenate all substrings together using the [] operator. Convert any numeric values to characters using the num2str function:
Code:
    name = 'Alice';   age = 12;
    str = [name, ' will be ', num2str(age), ' this year.']
Alice will be 12 this year.

fprintf and sprintf are pretty useful, though.
 
Last edited by a moderator:
  • #16
wow i hardly to understand that.. currently stuck like slayer did.. I'm unable to get the answer X cm equal X foot X inch

-----------------------

1 more thing how to the product of the odd interger for 1 to 15..
currently doing
for a=1:2:15
display('odd integers');
display(a) ;
end

but it don't show the product off odd interger.. try put sum but still didnt giv an answer =(
any idea?
 
  • #17
sum() won't give you a product!

Try prod(a)
 

1. How does the looping program generate a table of values?

The looping program uses a specific code, written in an M-File, to iterate through a range of numbers and perform a calculation (in this case, adding 2) on each number. It then outputs the results in a table format.

2. Can I change the starting and ending values for the table?

Yes, the code can be modified to change the starting and ending values for the table. This can be done by adjusting the parameters in the for loop within the M-File.

3. How do I view the table of values generated by the program?

The table of values will be displayed in the command window of the software used to run the M-File. Alternatively, the results can be saved to a file or plotted on a graph for easier viewing.

4. Can I perform a different calculation instead of adding 2?

Yes, the code can be modified to perform any desired calculation on the range of numbers. This can be done by changing the equation within the for loop in the M-File.

5. Is this looping program only applicable to a specific range of numbers?

No, the program can be modified to work with any range of numbers. The starting and ending values can be adjusted, as well as the step size (the amount by which the numbers are incremented). This makes the program versatile and applicable to a variety of situations.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
345
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Nuclear Engineering
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Nuclear Engineering
Replies
7
Views
2K
Replies
6
Views
1K
Back
Top