Showing posts with label ARRAY. Show all posts
Showing posts with label ARRAY. Show all posts

Arrays In Javascript.

Consider this small code sample in Javascript.


var arrayOfNumbers=[1,2,3,4,5,6,78];
for(var index in arrayOfNumbers){
    console.log(index+1);
};

Output of this snippet will be

01
11
21
31
41
51
61

Now this is because Arrays are treated as Objects in Javascript, so index is iterating over keys in an object. In this case the object will be represented as follows

arrayOfNumbers={ "0":1,
                 "1":2,
                 "2":3,
                 "3":4,
                 "4":5,
                 "5":6,
                 "6":78}

wap to find the second largest element from an array.

#include<stdio.h>
main()
{
int i,n,arr[100],a=0,b=0;
printf("enter the no of elements in the array\n");
scanf("%d",&n);
printf("enter the elements of the array\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=1;i<n;i++)
if(arr[a]<arr[i])
{
b=arr[a];
a=i;
}
else
if(b<arr[i])
b=arr[i];
printf("\n the second largest number is %d ",b);
}

write a c program that returns the position of the largest element in an array.


#include<stdio.h>
void main()
{
int array[100],j=0,n,i;
printf("enter the no of elemnts in the array\n");
scanf("%d",&n);
printf("enter the elements of the array\n");
for(i=0;i<n;i++)
scanf("%d",&array[i]);
for(i=1;i<n;i++)
{
if(array[j]<array[i])
j=i;
}
printf("\nthe position of largets element is= %d",j+1);
}

Implemet Stack in python

  class Stack : def __init__ ( self , data ): self . stack = [] if ( data ): self . stack . append ( da...