For assignments 1 and 2 your Big-O notation and justification will be written in
ID: 667607 • Letter: F
Question
For assignments 1 and 2 your Big-O notation and justification will be written in the html section of JSFiddle.
Assignment 1 - Using your JSFiddle account and JavaScript you will create a program that will instantiate an integer array of size 1000. Fill each array element with a random integer between 1 and 100. You will need to research the random function to do this.
You will write a function with 3 arguments. The name of the function will be InsertIntoArray. Argument 1 is the array, argument 2 is the index of where you are going to insert a new number, argument 3 is the number to insert. The program should insert the new number at the index given and shift all the previous entries index up by 1. Count the number of operations performed on the array and output this to the screen.
Experiment with different sized arrays and inserting into a different index in the array. State using Big O Notation your time complexity and be prepared to justify your answer.
Assignment 2 - Using your JSFiddle account and Javascript you will create a program that will instantiate an integer array of size 1000. Fill each array element with a random integer between 1 and 100. You will need to research the random function to do this.
You will write a function with 2 arguments. The name of the function will be SearchArray. Argument 1 is the array, argument 2 is the value you are searching for within the array. Each time the program compares 2 numbers you must count it as an operation. Output the total number of operations. State using Big O Notation your time complexity and be prepared to justify your answer.
Explanation / Answer
Assignment1:
Html coding:
<h1 id="title">Inserting Elements in Array </h1>
<input type="submit" id="byBtn" value="InsertIntoArray"/> <br>
Total Number of Operations performed:
<input type="text" id="myTextField2"/>
Java Script coding:
function randomIntInc (low, high) {
return Math.floor(Math.random() * (high - low + 1) + low);
}
function StoreArray(){
var numbers = new Array(1000);
var count=0;
for (var i = 0; i < numbers.length; i++) {
var Element=randomIntInc(1, 100);
var g=InsertIntoArray(numbers, i, Element);
count= count+g;
}
var t= document.getElementById('myTextField2');
myTextField2.value=count;
}
function InsertIntoArray(numbers,index,Element)
{
var count=0;
for (var i = numbers.length-1; i > 0; i--) {
numbers[i]=numbers[i-1];
count++;
}
numbers[0]=Element;
count++;
return count;
}
Result:
Inserting Elements in Array
Total Number of Operations performed:
Big O Notation:
------------------------------------------------------------------------------------------------------------------------------------------
Assignment2:
Html coding:
<h1 id="title">Array operations with random functions & Big o notation Analysis</h1>
Enter an element to search(1-100):
<input type="text" id="myTextField1"/>
<input type="submit" id="byBtn" value="SearchArray"/> <br>
Number of comparisions:<input type="text" id="myTextField2"/>
Java Script coding:
function randomIntInc (low, high) {
return Math.floor(Math.random() * (high - low + 1) + low);
}
function StoreArray(){
var numbers = new Array(100);
for (var i = 0; i < numbers.length; i++) {
numbers[i] = randomIntInc(1, 100)
}
var element = document.getElementById('myTextField1').value;
var c=SearchArray(numbers,element);
var t= document.getElementById('myTextField2');
myTextField2.value=c;
}
function SearchArray(numbers, Element)
{
var count=0;
for (var i = 0; i < numbers.length; i++) {
count++;
if(numbers[i]==Element)
{
alert('Element found!!.');
return count;
}
}
alert('Element not found!!.');
return count;
}
Result:
Searching an Element in Array
Enter an element to search(1-100):
Number of comparisions:
Big O Notation:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.