Page 1 of 1

The last picture is about JavScript code!!

Posted: Tue Jul 12, 2022 8:15 am
by answerhappygod
The last picture is about JavScript code!!
The Last Picture Is About Javscript Code 1
The Last Picture Is About Javscript Code 1 (59.44 KiB) Viewed 35 times
The Last Picture Is About Javscript Code 2
The Last Picture Is About Javscript Code 2 (59.44 KiB) Viewed 35 times
The Last Picture Is About Javscript Code 3
The Last Picture Is About Javscript Code 3 (41.81 KiB) Viewed 35 times
The Last Picture Is About Javscript Code 4
The Last Picture Is About Javscript Code 4 (24.45 KiB) Viewed 35 times
Problem 4 In this problem, develop an algorithm to convert a binary representation to its decimal value. That is, convert base 2 representation to base 10 representation. Given an input array of 0 and 1s, treated as binary representation of a number, calculate the decimal value of the number. For example, if the array is filled with 1 1 0 1, the algorithm should output 13. If the array is filled with 0.01 1, then the output should be 3. To understand the conversion, you can look at the provided document "Number system 1012 22s.pdf", or search online for relevant information. In general, if a binary number has n digits as did2d3...dn-1dn, it can be converted to decimal by formula d, *2 + d₂ 2 2 + d3*23 +... da-1 2¹ + d₂*20. As another example, if the array is filled with 00100111 then the program should output 39 because 0*27 +0*2° +1*25 + 0*2 + 0*2³ + 1*22 + 1*2¹ + 1*2° -39, . Develop flowchart for this algorithm o Give pre-condition and post-condition for your algorithm. O The algorithm takes as input an array, filled with Os and 1s, outputs the decimal value represented by the Os and 1s. O 000 You will need a loop to traverse the array. The array can be traversed from left to right or, from right to left. Here you should traverse from right to left. You will need to calculate the power of the base, i.c., 2° 2¹ 2². Define a sub-algorithm named o Implement sub-algorithm power using loop. Start your flowchart as shown in the following figure. The algorithm takes as input an array arr O Save the flowchart as img_binary.jpg O power (base, n) which calculates the base to the power of n, i.e., base". Call this sub-algorithm in the main algorithm. In the main algorithm, if need to calculate 2 to power of n, don't write 2", instead, call function power to calculate. start arr[]
Implement the algorithm using JavaScript (Your implementation should match your flowchart) o Complete the function problem40), and attach the onclick event to the corresponding button in html. This function first generates an array of random size, filled with randomly generated 0s and 1s. Define a sub-algorithm power(base, n) that calculates base". Call this function in main when you need to calculate the power of base. Sample output (each run will generate a different input array) O 1,0,1,1,1,1,0,1,0 378 pr prodaja ay pre-way st - HA 1,1,0,1,1,1 55 pr - to deng proben - pory to A prod mach 0,0,0,0,0 pk-yrt H 1,0,1,1,1 wak A SLAND Bereic
141 142 function problem4() 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 var p = document.getElementById("mydata"); } var myArray []; ■ var arrayMax = Math.floor(Math.random()*6); var limit= 2; for (var i = 0; i < arrayMax+4; i++) myArray.push(Math.floor(Math.random()*limit)); p.innerHTML= myArray; /* you implementation here define sub-algorithm power (base, n) and call it in this function when needed C function power (base, n){