Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Midterm 2: Merging Arrays of Numbers in JavaScript - Prof. Paul Stotts, Exams of Computer Science

The specifications for a javascript programming assignment in comp 110, fall 2008. Students are required to create a web page with an embedded javascript program that merges two sorted arrays of real numbers into a single, sorted array. Instructions on input validation, error checking, and html page structure.

Typology: Exams

Pre 2010

Uploaded on 03/11/2009

koofers-user-wb7
koofers-user-wb7 🇺🇸

10 documents

1 / 3

Toggle sidebar

Related documents


Partial preview of the text

Download Midterm 2: Merging Arrays of Numbers in JavaScript - Prof. Paul Stotts and more Exams Computer Science in PDF only on Docsity! COMP 110 Fall 2008 Midterm 2: Merging Arrays of Numbers Due: Fri. Dec. 5 at 11:59 pm Read this document carefully and completely before you start programming. This is to be a Javascript program. This work is pledged. You must complete the program on your own, without consulting or working with anyone else. You may ask questions of the TAs and the instructor, but you might not get the answer you hope for… we will answer questions about clarifying the specs but will not write code for you. For this assignment you will create a web page with an embedded Javascript program. You will store this program as an HTML file in your UNC web space, and you will give it an 8-character or more filename that only you know. Then we can retrieve, run, and grade it by pointing our browser www.unc.edu/~ yourOnyen/yourSecretName.html but no one else can see it. Use a text editor to create this program. Do not use Web generation tools. You may use the Lint tool, or other syntax checkers, to help you debug your Javascript. Program Specifications Your program will take as input two arrays of real numbers, both of the same length, and each already in sorted order. It will produce as output an array of numbers that is twice as long and is the merge of the two input arrays. The merge will leave the output array also in sorted order. Input: Your program will first prompt the user for a number (a positive integer), which will be the size of each of two input arrays. The program will then prompt the user for real numbers (floating point numbers) to put into those arrays. These numbers may be any real numbers (positive or negative) but the user will be required to give them in ascending sorted order for each array. Fill one input array completely, then prompt the user for numbers to fill the other one. The program will then produce a 3rd array (twice as long as each input array) and merge the numbers in two input arrays into this 3rd array. When properly done, if the two input arrays are in sorted ascending order, then the output array will be sorted as well. Output: Print the two input arrays, and print the merged array. Find and print the largest number in the merged array. Find and print the smallest number in the merged array. Compute and print the average of the numbers in each input array, and in the merged array. Example of merging: Let’s say our two input arrays are arr1 and arr2, and the user gives these numbers: arr1: -17, 2, 31.4, 107.2, 1163, arr2: -3.3, 12, 17.3, 35, 974.5, The output array (let’s call it arr3) looks like this after merging: arr3: -17, -3.3, 2, 12, 17.3, 31.4, 35, 107.2, 974.5, 1163 Notice that arr3 is twice as long as arr1 (and also twice as long as arr2). Merging is not too tough… You start at slot 0 in both input arrays. You look at the current element in arr1, and look at the current element in arr2, and whichever element is smaller, you copy that element into the output array. You will need two separate slot counters: one for arr1, and another for arr2. You will go into a FOR loop that runs from 0 to the total number of elements in both of the input arrays. Each time through the loop, you will put one element into the merge array… that element will come either from arr1 or arr2, depending on which is smaller. You start looking at slot 0 in each array, and every time you decide to copy an element into the merge array you move up one slot in the input array you copied from. HTML page structure You must write a supporting function to compute the average of the numbers in an array, and use it to compute the 3 averages. Your script should be written as a main function and the helper function (called “average”) in the head section of an HTML page, and a button to run the main function in the body section of the HTML. You may write more than one helper function if you wish, but you must have at least the one. As in previous assignments, that HTML page will serve as documentation for the program… it will explain what the program will do, what inputs are expected from the user (what are legal input values) and what information the user can expect to be computed, or what activities he/she can perform. Also explain how to make the program terminate. Somewhere on your HTML page will be a button that says something like “Click here to run the program”. When clicked, that button will execute your function/script.
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved