loading...

CS101 Introduction to Computing Assignment 3 Solution Spring 2014

Assignment: Marks 20
You are required to develop a web page which will create an array of strings and then it will sort the array in ascending and descending orders. The number of elements of the array will be specific by the user at run time.
Detailed Description:
1. You are required to develop a web page as shown below:

2. User will specify the number of array elements in the text field and then click on “Take Input” button. This process will create an array of specified number in the text field and also take input from user in those elements. Example is shown in the figure below:

Here 5 is number of elements of the array. On clicking on “Take Input” button, the Prompt window should display asking for input in the array elements one by one.

3. After taking inputs, if the user clicks on “Ascending” button, then the entered values should be sorted and displayed in ascending order and if user clicks on “Descending” button, then it should be sorted in descending order. Example is shown in the figures below assuming that the inputs are “Table”, “Chair”, “Door”, “Fan”, and “House”.

Hints:
You can make this assignment by making some modifications in the example program given in Lecture 26
Event handlers and functions are the core concepts for solving this assignment
NOTE:
You have to modify the font size of the output array otherwise marks will be deducted.

Solution:

<HTML>
<HEAD>
<TITLE> sorting_the_array</TITLE>
<!–Assignment Solution
After entering no of elements don’t press enter. just click on get input button –>
<SCRIPT>
function getinput(){
var val = parseInt(document.getElementById(‘elements’).value);
words = new Array( val );
for( k = 0; k < words.length; k = k + 1 ){
words[ k ] = window.prompt(“Enter word # ” + k, “”) ;
function sortasc(){
words.sort();
document.write(“<font size=’8′><b>” + “Sorted Array in Ascending Order:” + “</b></font>” + “<br><br><br>” ) ;
for( k = 0; k < words.length; k = k + 1 ){
document.write(“<font size=’6′><b>” + words[k] + “</b></font>” + “<br><br>” );
}
function sortdes(){
words.sort();
words.reverse();
document.write( “<font size=’8′><b>” + “Sorted Array in Descending Order:” + “</b></font>” + “<br><br><br>” ) ;
for( k = 0; k < words.length; k = k + 1 ){
document.write(“<font size=’6′><b>” + words[k] + “</b></font>” + “<br><br>” );
</SCRIPT>
</HEAD>
<BODY>
<h1>Implementation of Sort and Reverse functions </h1>
<br>
<br>
<h2>Enter number of elements </h2>
<br>
<FORM>
<input type = “textid=”elementssize=”30“>
<input type = “buttonname=”inputvalue=”Take InputonClick = “getinput()“>
<br>
<br>
<br>
<input type = “buttonname=”ascvalue=”AscendingonClick=”sortasc()” >
<input type = “buttonname=”desvalue=”DescendingonClick=”sortdes()“>
</FORM>
</BODY>
</HTML>
DOWNLOAD SOLUTION HERE
loading...