Triangle Wave | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Triangle Wave

I want pattern series code in java output is: 1 22 333 22 1 1 22 333 22 1 1 22 333 22 1 1 22 333 22 1 Input: 4 3   where: First line represents the value of N1 (frequencies of waves). Second line represents the value of N2 (amplitude of waves).

19th Nov 2022, 3:15 PM
Hannah
4 Answers
+ 1
need a for loop for N1 [1, N1] to print 4 times the wave. Inside this loop a other for loop N2 [1, N2] to print 1,22,333 and 1 more for loop where you do the opposite 22,1. And last thing you need to print 2 times the 2, 3 times the 3 etc, you need 1 more for loop
19th Nov 2022, 3:57 PM
Giannis
Giannis - avatar
+ 1
other way to simplify it a bit, create a String array that contains the wave for example [1,22,333,4444,333,22,1] and print it N1 times
19th Nov 2022, 4:08 PM
Giannis
Giannis - avatar
+ 1
Hannah the abs() function can give you a ramp up/down like that for one cycle. i 3-abs(i) --- ------- -2 1 -1 2 0 3 1 2 2 1 There is the core of your wave. Use an inner loop to print each number, as many times as the number itself. Use an outer loop to repeat the overall pattern.
19th Nov 2022, 4:15 PM
Brian
Brian - avatar
19th Nov 2022, 5:59 PM
SoloProg
SoloProg - avatar