Forum : Array Assignment
Brief description  about Online courses   join in Online courses
View Santosh  Hegde 's Profile

Array Assignment

Dear sir,
i tried multiple things but i`m unable to get output for:
To make an array of size 10 and put random number in the array, make the sorting of array in ascending order.(Hint: use math random function to input array OR input from user)

For,
a. array element insertion from front.
b. array element insertion from back.
c. array element deletion from front.
d. array element deletion from back.

and i can do some other function
so please help me for this assignment..
looking forward to you...
Asked by Santosh Hegde | Jan 10, 2011 |  Reply now
Replies (1)
View teacher siliconindia 's Profile
Hi Santosh,
For your query you can go through this below code:

function Module1()
{
rarray = new Array(10);
for(v=0 ; v<10 ; v++)
{
rarray[v] = Math.round((Math.random()*1000));
}
/*
rarray[0] = Math.round((Math.random()*1000));
rarray[1] = Math.round((Math.random()*1000));
rarray[2] = Math.round((Math.random()*1000));
rarray[3] = Math.round((Math.random()*1000));
rarray[4] = Math.round((Math.random()*1000));
rarray[5] = Math.round((Math.random()*1000));
rarray[6] = Math.round((Math.random()*1000));
rarray[7] = Math.round((Math.random()*1000));
rarray[8] = Math.round((Math.random()*1000));
rarray[9] = Math.round((Math.random()*1000));
*/
for (i=0 ; i<10-1 ; i++)
for (j=0 ; j<10-1-i ; j++)
if (rarray[j] > rarray[j+1])
{
t = rarray[j];
rarray[j] = rarray[j+1];
rarray[j+1] = t;
}
alert(rarray.join([" : "]));
}

*****

f=0;
r=-1;
size=10;
queue = new Array(size);
//f=queue[0];
function Module2ins()
{
//queue.push(document.myform1.field1.value);
if(r == (size-1))
{
alert("Queue Is Full");
}
else
{
queue[++r]=document.myform1.field1.value;
alert("Element Inserted Is: "+queue[r]);
}
}

function Module2del()
{
if(f > r)
{
alert("Queue Is Empty");
f=0;
r=-1;
}
else
{
del = queue[f++];
alert(del);
}
}
function Module2disp()
{
if ( f > r)
{
alert("Queue Is Empty");
}
else
{
for(i=f;i<=r;i++)
{
d = queue[i];
alert("The \""+i+"\" Position Element Is: "+d);
}
}

}

*****
ssize=10;
stack = new Array(ssize);
top=-1;

function Module3ins()
{
if(top == (ssize-1))
{
alert("Stack Is Full");
}
else
{
stack[++top]=document.myform2.field1.value;
alert("Element Inserted Into Stack Is: "+stack[top]);
}
}

function Module3del()
{
if(top == -1)
{
alert("Stack Is Empty");
}
else
{
alert("Element Deleted From Stack Is: "+stack[top--]+" \(Top Of Stack\)");
}
}

function Module3disp()
{
if(top == -1)
{
alert("Stack Is Empty");
}
else
{
for(i=0;i<=top;i++)
{
ele = stack[i];
if(i == top)
{
alert("The Top Of The Stack is: "+ele);
}
else
{
alert("The \""+i+"\" Position Element Is: "+ele);
}
}
}
}

Regards,
Teacher
Jan 12, 2011