Two problems regarding for loops

6 views (last 30 days)
Paul
Paul on 17 Oct 2013
Commented: Jian David Knight on 28 Sep 2021
I'm working on a set of practice problems, which should be enforcing the use of for loops, but I can't seem to figure out how to do these two. I'm completely lost.
2) Create an x vector that has integers 1 through 10, and set a y vector equal to x. Plot this straight line. Now, add noise to the data points by creating a new y2 vector that stores the values of y plus or minus 0.25. Plot the straight line and also these noisy points.
3) Write a script beautyofmath that produces the following output. The script should iterate from 1 to 9 to produce the expressions on the left, perform the specified operation to get the results shown on the right, and print exactly in the format shown here.
>> beautyofmath
1 x 8 + 1 = 9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321
  2 Comments
Matt Kindig
Matt Kindig on 17 Oct 2013
Edited: Matt Kindig on 17 Oct 2013
Here's what can get you started on 2)
x = 1:10; y=x;
plot(x,y);
y2 = y+0.25;
%now how would you extend this to the minus 0.25 case?
By the way, this first problem doesn't actually use loops.
For 3)
for k=1:9 %outer loop
value = 0;
for m=1:k %inner loop
power10 = 10^(??); %what goes here?
value = value + ?? % how to calculate? Think about how digits work!
end
answer = value*?? %how to calculate the answer?
fprintf('%d x %d + %d = %d\n', value, 8, k, answer);
end
Jan
Jan on 17 Oct 2013
Edited: Jan on 17 Oct 2013
"I'm completely lost" is less useful than showing, what you have tried so far, even if it is not working. It is much easier to suggest an improvement than to write a program from scratch. So please post more details instead of simply repeating the text of the homework question.

Sign in to comment.

Answers (1)

sixwwwwww
sixwwwwww on 17 Oct 2013
Dear Paul,
Here is the solution for your task (2):
x = 1:10;
y = x;
y2(1:2:9) = y(1:2:9) + 0.25;
y2(2:2:10) = y(2:2:10) - 0.25;
plot(x, y, x, y2), legend('y', 'y2')
And following is solution for your task (3):
for i = 1:9
if i == 1
j(i, 1) = i;
else
j(i, 1) = j(i - 1, 1) * 10 + i;
end
j(i, 2) = i;
j(i, 3) = j(i, 1) * 8 + j(i, 2);
fprintf('%d x 8 + %d = %d\n', j(i, 1), j(i, 2), j(i, 3))
end
I hope it helps. Good luck!
  2 Comments
Samuel Sharon
Samuel Sharon on 11 Feb 2019
For those of you out there trying to solve Beauty of Math, this solution is terrible! I'm not going to post a solution here, but if you're using a nested for loop or a matrix, you're overthinking it! This problem should have no more than 5 lines of code, including one line for the for statement and one line for the word "end."
Jian David Knight
Jian David Knight on 28 Sep 2021
Wasn't able to get it to five lines of code, however I did get it down to 8.
for i = 1:9
fprintf('%d', 1:i)
fprintf(' x 8 + %d = ', i)
for j = flip(i:-1:1)
fprintf('%d', 10-j)
end
fprintf('\n')
end
The ouput should be this:
>> beautyofmath2
1 x 8 + 1 = 9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!