Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

PHP Examples - Arrays

PHP Examples 

 Arrays

Program:
<html >
<head>

<title>PHP Example 5 : ARRAYS</title>
</head>

<body>
<?php
 
 echo "PHP Example 5";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"ARRAYS";
 echo"</h1>";
 echo"<br/>";

 $array1 = array(5,3,7,6,1,2,9);  //Creating array 
 $array1[] = 8;
 $array1[] = 4;
 
 $n=count($array1); // Counting no of elements
 
 echo"<br/><b> Before sorting: </b> ";
 for($i=0;$i<$n;$i++)  //Printing array elements 
  echo $array1[$i]." ";  

 for($i=0;$i<$n;$i++)  //Sorting
 for($j=$i+1;$j<$n;$j++)
 {
  if($array1[$i]>$array1[$j])
  {
   $temp=$array1[$i];
   $array1[$i]=$array1[$j];
   $array1[$j]=$temp;
   }
 }
 
 echo"<br/> <b>After sorting: </b>";
 for($i=0;$i<$n;$i++)
  echo $array1[$i]." ";
 echo"<br/>";

 $arraysum = array_sum($array1);  // Calculating sum of the elements in the array 
 echo"<br/><b> Sum of the elements : </b>$arraysum";
 echo"<br/>";
 
 echo "<br/><b>Checking whether element is present in the array or not...</b>";
 if(in_array(5,$array1))      // Checking whether element is present in the array or not
  echo "<br/>5 is present in the array... ";
 else 
  echo "<br/>5 is not present in the array... ";
 echo"<br/>";

 echo "<br/><b>";
 echo"WWW.2K8618.BLOGSPOT.COM";
 echo"</b>";
?>


</body>
</html>



Output:






Stack Using Array - Datastructure - JAVA

Program:
import java.io.*;
class stackarray
{   
    private int maxsize;
    private int st[];
    public int top;
    public stackarray(int size)
    {
        maxsize=size;
        st=new int[maxsize];
        top=-1;
    }

    public boolean isempty()
    {
        return(top==-1);
    }
    public boolean isfull()
    {
        return(top==(maxsize-1));
    }
    public void push(int data)
    {
        if(isfull())
            System.out.println("Error:stack overflow");
        else
            {
                st[++top]=data;
                System.out.println("\tpushing to stack.... "+data);
            }

    }
    public int pop()
    {
        if(isempty())
        {
            System.out.print("\nError:empty");
            return 0;
        }
        else
        {
            return st[top--];
        }
    }
    public void display()
    {
        System.out.println("\n--STACK ITEMS-- ");
        for(int i=0;i<=top;i++)       
            System.out.print(st[i]+" ");
    }
}
class stack_array
{
public static void main(String arg[])throws IOException
{
    System.out.print("\n\n\t\t<<<STACK USING ARRAY>>>\nEnter the size of array:");
    DataInputStream x= new DataInputStream(System.in);
    stackarray ar1=new stackarray(Integer.parseInt(x.readLine()));
    int proceed=1;
    System.out.print("Enter the number of elements to push:");
    int p=Integer.parseInt(x.readLine());   
    System.out.println("Enter the elements:");
    for(int k=0;k<p;k++)
        ar1.push(Integer.parseInt(x.readLine()));
    ar1.display();
    System.out.print("\nEnter the number of elements to pop:");
    int q=Integer.parseInt(x.readLine());   
    for(int k=0;k<q;k++)
        System.out.println("\nPoping.... "+ar1.pop());
   
    do{
        ar1.display();
        System.out.println("\n---MENU---\n1.Push an element to stack.\n2.Pop an element from stack\n3.Check Full\n4.Check Empty\n5.Display Stack Items \n");
        int s1=Integer.parseInt(x.readLine());   
        switch(s1)
        {
            case 1:
                System.out.print("---PUSH AN ELEMENT---\nEnter the element:");
                ar1.push(Integer.parseInt(x.readLine()));
                break;
            case 2:
                System.out.print("---POP AN ELEMENT---\nPoping.... "+ar1.pop());
                break;
            case 3:
                if(ar1.isfull())
                    System.out.println("Stack is full.");
                else
                    System.out.println("Stack is not full.");
                break;
            case 4:
                if(ar1.isempty())
                    System.out.println("Stack is empty.");
                else
                    System.out.println("Stack is not empty.");
            case 5:
                    ar1.display();
            default:
                break;
        }
        System.out.println("\nPress 1 to continue...");
        proceed=Integer.parseInt(x.readLine());   
    }while(proceed==1);


}   
}

Output:
nn@linuxmint ~ $ javac stack_array.java
Note: stack_array.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java stack_array


        <<<STACK USING ARRAY>>>
Enter the size of array:3
Enter the number of elements to push:3
Enter the elements:
1
    pushing to stack.... 1
2
    pushing to stack.... 2
3
    pushing to stack.... 3

--STACK ITEMS--
1 2 3
Enter the number of elements to pop:1

Poping.... 3

--STACK ITEMS--
1 2
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

1
---PUSH AN ELEMENT---
Enter the element:4
    pushing to stack.... 4

Press 1 to continue...
1

--STACK ITEMS--
1 2 4
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

3
Stack is full.

Press 1 to continue...
1

--STACK ITEMS--
1 2 4
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

2
---POP AN ELEMENT---
Poping.... 4
Press 1 to continue...
1

--STACK ITEMS--
1 2
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

4
Stack is not empty.

--STACK ITEMS--
1 2
Press 1 to continue...
1

--STACK ITEMS--
1 2
---MENU---
1.Push an element to stack.
2.Pop an element from stack
3.Check Full
4.Check Empty
5.Display Stack Items

5

--STACK ITEMS--
1 2
Press 1 to continue...
2
nn@linuxmint ~ $
Related Posts Plugin for WordPress, Blogger...