0% found this document useful (0 votes)
54 views

Ds Codes

The document contains code for several Java programs that demonstrate different array and data structure concepts: 1) The first section generates a random 3x3 matrix and performs addition on two 3x3 arrays. 2) It then multiplies two 3x3 arrays using a nested for loop. 3) Subsequent sections demonstrate finding the largest number and occurrence count in an array, calculating grade based on average score, calculating standard deviation, and counting letter frequencies in an array. 4) Further sections provide examples of implementing stack and queue data structures with push, pop, peek and size functions.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Ds Codes

The document contains code for several Java programs that demonstrate different array and data structure concepts: 1) The first section generates a random 3x3 matrix and performs addition on two 3x3 arrays. 2) It then multiplies two 3x3 arrays using a nested for loop. 3) Subsequent sections demonstrate finding the largest number and occurrence count in an array, calculating grade based on average score, calculating standard deviation, and counting letter frequencies in an array. 4) Further sections provide examples of implementing stack and queue data structures with push, pop, peek and size functions.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Arrays lab work

package arrays;
public class arrays {
public static void main(String []args){
int[][] matrix1=new int[3][3];
for(int i=0;i<matrix1.length;i++)
{
for(int j=0;j<matrix1.length;j++)
{
matrix1[i][j]=(int)(Math.random()*100);
System.out.println("the index is: "+i+j+" and value is:
"+matrix1[i][j]);
}
}
//array addition
System.out.println("Array Addition");
int b[][]={{1,2,3},{4,5,6},{7,8,9}};
int c[][]={{1,1,1},{2,2,2},{3,3,3}};
int d[][]= new int[3][3];
for (int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
d[i][j]=b[i][j]+c[i][j];
System.out.print(d[i][j]+" ");
}
System.out.println(" ");
}
//array multiplication
System.out.println("Array Multiplication");
int e[][]={{1,2,3},{4,5,6},{7,8,9}};
int f[][]={{1,1,1},{2,2,2},{3,3,3}};
int g[][]= new int[3][3];
for (int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
g[i][j]=0;
for(int k=0;k<3;k++)
{
g[i][j]+=e[i][k]*f[k][j];
}
System.out.print(g[i][j]+" ");
}
System.out.println(" ");
}
}
}
===================================================================================
===============================================
Occurrence example 1
package occurrance;
import java.util.Scanner;
public class Occurrance {

public static void main(String[] args) {


int x,count=0,i=0,max = 0;
Scanner s=new Scanner(System.in);
int a[]=new int[6];

System.out.println("Enter all the Elements: ");

for(i=0;i<a.length;i++)
{
a[i]=s.nextInt();

}
max=0;
for(int j=0;j<a.length;j++)
{
if(max<a[j])
{
max=a[j];
}
}
x=max;
for(int k=0;k<a.length;k++)
{
if(a[k]==x)
{
count++;
}
}
System.out.println("Largest number is: "+max);
System.out.println("Number of occurrances of element: "+count);
}

===================================================================================
===================================================================================
===================================================================================
========

Score grading example 2

package grading.on.score;
import java.util.Scanner;
public class GradingOnScore {

public static void main(String[] args) {


int marks[]=new int[6];
float total=0,average;
Scanner scanner=new Scanner(System.in);
for(int i=0;i<6;i++)
{
System.out.println("Enter Marks of Subjects"+(i+1)+": ");
marks[i]=scanner.nextInt();
total=total+marks[i];
}
scanner.close();
System.out.println("Average");
average=total/6;
System.out.println("The Students Grade is: ");
if(average>=90)
{
System.out.println("A ");
}
else if(average>=80&&average<90)
{
System.out.println("B ");
}
else if(average>=70&&average<80)
{
System.out.println("C ");
}
else if(average>=60&&average<70)
{
System.out.println("D ");
}
else if(average>=50&&average<60)
{
System.out.println("E ");
}
else
{
System.out.println("F ");
}
}

===================================================================================
===================================================================================
===================================================================================
========

Standard deviation example 3


package standard.deviation;

public class StandardDeviation {

public static void main(String[] args) {


int[] numbers=new int[]{12,11,14,14,16,18,19};
int sum=0;
int max=0;
int min=numbers[0];
double sd=0;
for(int i=0;i<numbers.length;i++)
{
int avg=0;
sd+=((numbers[i]-avg)*(numbers[i]-avg))/(numbers.length-1);
}
double StandardDeviation=Math.sqrt(sd);
System.out.println("the standard deviation is: "+StandardDeviation);
}

===================================================================================
===================================================================================
===================================================================================
========
Counting occurrence of each letter example 4
package alphabets;
import java.util.Random;
public class Alphabets {

public static void main(String[] args) {


char[] chars=createArray();
System.out.println("The lower case letters are: ");
displayArray(chars);
int[] counts=countLetters(chars);
System.out.println();
System.out.println("The occurrences of each letters are");
displayCounts(counts);
}

private static char[] createArray() {


char[] chars=new char[100];
Random generator=new Random();
String S="abcdefghijklmnopqrstuvwxyz";
for(int i=0;i<chars.length;i++)

chars[i]=(char)
S.charAt(generator.nextInt(26));
return chars;
}

private static void displayArray(char[] chars) {


for (int i=0;i<chars.length;i++)
{
if((i+1)%20==0)
{
System.out.println(chars[i]+" ");
}
else
{
System.out.print(chars[i]+" ");
}
}
}

private static int[] countLetters(char[] chars) {


int[] counts=new int[26];
for (int i=0;i<chars.length;i++)
counts[chars[i]-'a']++;
return counts;
}

private static void displayCounts(int[] counts) {


for(int i=0;i<counts.length;i++)
{
if((i+1)%10==0)
{
System.out.println(counts[i]+" "+(char)(i+'a'));
}
else
{
System.out.println(counts[i]+" "+(char)(i+'a')+" ");
}
}

System.out.println("STANDARD DAVIATION");
int sum=0;
int max=0;
int min=counts[0];
double sd=0;
for(int i=0;i<counts.length;i++)
{
int average=0;
sd+=((counts[i]-average)*(counts[i]-average))/(counts.length-1);
double standardDeviation=Math.sqrt(sd);
System.out.println("The standard deviation is: "+standardDeviation);
}

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------------------------------
package stack;

public class Stack {

private int maxsize;


private long stackarray[];
private int top;

public Stack(int s)
{
maxsize =s;
stackarray=new long[maxsize];
top=-1;
}
public void push(long j)
{
stackarray[++top]=j;
}
public long pop()
{
return stackarray[top--];
}
public boolean isEmpty()
{
return(top==-1);
}
public static void main(String[] args)
{
Stack s= new Stack(5);
s.push(1);
s.push(2);
s.push(21);
s.push(31);
s.push(34);
System.out.println("VALUES IS");
while(!s.isEmpty())
{
long value =s.pop();
System.out.println(value);

}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------

ackage queue1;

public class Queue1 {

private int arr[];


private int front;
private int rear;
private int count;
private int capacity;

Queue1(int size) {
arr = new int[size];
capacity = size;
front = 0;
rear = -1;
count = 0;

public void dequeue() {


if (isEmpty()) {
System.out.println("UNDERFLOW \n PROGRAM TERMINATED");
System.exit(1);
}
System.out.println("REMOVING " + arr[front]);
front = (front + 1) % capacity;
count--;
}

public void enqueue(int item) {


if (isFull()) {
System.out.println("OVRFLOW \n PROGRAM TERMINATED");
System.exit(1);
}
System.out.println("INSERTING " + item);
rear = (rear + 1) % capacity;
arr[rear] = item;
count++;
}

public int peek() {


if (isEmpty()) {
System.out.println("UNDERFLOW \n PROGRAM TERMINATED");
System.exit(1);
}
return arr[front];
}

public int size() {


return count;
}

public Boolean isEmpty() {


return (size() == 0);
}

public Boolean isFull() {


return (size() == capacity);
}

public static void main(String[] args) {


Queue1 q = new Queue1(5);
q.enqueue(7);
q.enqueue(70);
q.enqueue(71);
System.out.println("FIRST ELEMENT " + q.peek());
q.dequeue();
System.out.println("FIRST ELEMENT " + q.peek());
System.out.println("SIZE IS " + q.size());
q.dequeue();
q.dequeue();
if (q.isEmpty()) {
System.out.println("QUEUE IS EMPTY");
} else {
System.out.println("QUEUE IS NOT EMPTY");
}

-----------------------------------------------------------------------------------
------------------------------------------------

package bubble.sort;

public class BUBBLESORT {

public void sort(int mainarr[])


{
int n;
n = mainarr.length;
boolean f;
f=false;
for(int a = 0;a<n-1;a++)
{
for (int b = 0;b<n-a-1;b++)
{
if (mainarr[b]>mainarr[b+1])
{
int temp;
temp = mainarr[b];
mainarr[b] = mainarr[b+1];
mainarr[b+1] = temp;
System.out.println("worst case");
f=true;
}

}
if(f== false)
{System.out.println("best case executed");
break;
}
}
// for(int i= 0;i<mainarr.length;i++)
//{
// System.out.println("array:"+mainarr[i]);
//
//}

public static void main(String[] args) {


// TODO code application logic here
int arr[]={10,1,2,50,3,4};

BUBBLESORT obj = new BUBBLESORT();

obj.sort(arr);
for(int i= 0;i<arr.length;i++)
{
System.out.print(" "+arr[i]);

}
}

-----------------------------------------------------------------------------------
------------------------------------------------------

package insertionsorting;

public class Insertionsorting {


public void sort (int mainarr [], int size)
{
boolean flag;
size= mainarr.length;
int True = 0;
int False = 0;

System.out.println("i V H C F");

for (int i=1 ; i<size;i++)


{
int value = mainarr[i];
int hole = i;
while(hole >0 && mainarr[hole-1]>value)
{
mainarr[hole]=mainarr[hole-1];
hole = hole-1;
True++;
flag=true;
if (flag==true)
{
int z = 0;

if(hole == 0)
{
flag = false;
}
System.out.println(i+" "+ value+" "+ hole + " "+ hole + ">"+
z+"&&"+(hole-1)+" "+ flag);

}
False++;
flag = false;
mainarr[hole]=value;
}
System.out.println("-------------------------------");
System.out.println("\nTrue iterations: " + True);
System.out.println("False iterations: " + False);
System.out.println("Total iterations: " + (True+False) );
System.out.println("\n-------------------------------");

public static void main(String[] args) {


Insertionsorting i = new Insertionsorting();
int arr[] = {66,63,4,7,67,539,70,21,2,42}; //Worst Case
int n=arr.length;
i.sort(arr, n);
System.out.println();
System.out.print("\nSorted array: ");

for ( int j = 0 ; j<n ; j ++){

System.out.print(arr[j]+" ");

}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------

package selectionsort;
public class Selectionsort {

void sort(int arr[]) {


int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int min_idx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
}

void printarray(int arr[]) {


int n = arr.length;
for (int i = 0; i < n; i++) {
System.out.println(arr[i]);

}
}

public static void main(String[] args) {


Selectionsort ob = new Selectionsort();
int arr[] = {30, 24, 13, 24, 80};
ob.sort(arr);
System.out.println("SORTED ARRAY");
ob.printarray(arr);
}

}
-----------------------------------------------------------------------------------
------------------------------------------------

package quick.sort;

public class QuickSort {


int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low-1);
for (int j=low; j<high; j++)
{
if (arr[j] < pivot)
{
i++;

int temp = arr[i];


arr[i] = arr[j];
arr[j] = temp;
}
}

int temp = arr[i+1];


arr[i+1] = arr[high];
arr[high] = temp;

return i+1;
}

void sort(int arr[], int low, int high)


{
if (low < high)
{

int pi = partition(arr, low, high);

sort(arr, low, pi-1);


sort(arr, pi+1, high);
}
}

static void printArray(int arr[])


{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}

public static void main(String[] args) {


int arr[] = {10, 7, 8, 9, 1, 5};
int n = arr.length;

QuickSort ob = new QuickSort();


ob.sort(arr, 0, n-1);

System.out.println("sorted array");
printArray(arr);

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------------------------------------
binary treee

class Node
{
int key;
Node left, right;

public Node(int item)


{
key = item;
left = right = null;
}
}

// A Java program to introduce Binary Tree


class BinaryTree
{
// Root of Binary Tree
Node root;

// Constructors
BinaryTree(int key)
{
root = new Node(key);
}

BinaryTree()
{
root = null;
}

public static void main(String[] args)


{
BinaryTree tree = new BinaryTree();

/*create root*/
tree.root = new Node(1);

/* following is the tree after above statement

1
/ \
null null */

tree.root.left = new Node(2);


tree.root.right = new Node(3);

/* 2 and 3 become left and right children of 1


1
/ \
2 3
/ \ / \
null null null null */

tree.root.left.left = new Node(4);


/* 4 becomes left child of 2
1
/ \
2 3
/ \ / \
4 null null null
/ \
null null
*/
}
}
===================================================================================
===================================================================================
==================================================

You might also like