80 likes | 294 Views
Sieve. public class sieve { public static void main(String args[]) { int max = 100; boolean prime[] = new boolean[max+1]; int n,m; // initialize the array (entries 0,1 unused) for (n = 2; n <= max; n++) { prime[n] = true;
E N D
Sieve public class sieve { public static void main(String args[]) { int max = 100; boolean prime[] = new boolean[max+1]; int n,m; // initialize the array (entries 0,1 unused) for (n = 2; n <= max; n++) { prime[n] = true; } // cross out multiples of uncrossed numbers for (n = 2; n <= max; n++) { if (prime[n]) { for (m = 2*n; m <= max; m += n) { prime[m] = false; } } } // print out primes: uncrossed numbers for (n = 2; n <= max; n++) { if (prime[n]) { System.out.print(n+" "); } } System.out.println(); } }
Queue class Queue { char q[]; // this array holds the queue int putloc, getloc; // the put and get indices Queue(int size) { q = new char[size+1]; // allocate memory for queue putloc = getloc = 0; } boolean isEmpty(){ if (getloc == putloc) return true; else return false; } boolean isFull(){ if (putloc == q.length-1) return true; else return false; }
// put a characer into the queue void put(char ch) { if (isFull()) { System.out.println(" -- Queue is full."); return; } putloc++; q[putloc] = ch; } // get a character from the queue char get() { if (isEmpty()){ System.out.println(" -- Queue is empty."); return (char) 0; } getloc++; return q[getloc]; } }
// Demonstrate the Queue class. class QueueDemo { public static void main(String args[]) { Queue bigQ = new Queue(100); Queue smallQ = new Queue(4); char ch; int i; System.out.println("Using bigQ to store the alphabet."); // put some numbers into bigQ for(i=0; i < 26; i++) bigQ.put((char) ('A' + i)); // retrieve and display elements from bigQ System.out.print("Contents of bigQ: "); for(i=0; i < 26; i++) { ch = bigQ.get(); if(ch != (char) 0) System.out.print(ch); } System.out.println("\n");
System.out.println("Using smallQ to generate erros."); // Now, use smallQ to generate some errors for(i=0; i < 5; i++) { System.out.print("Attempting to store " + (char) ('Z' - i)); smallQ.put((char) ('Z' - i)); System.out.println(); } System.out.println(); // more errors on smallQ System.out.print("Contents of smallQ: "); for(i=0; i < 5; i++) { ch = smallQ.get(); if(ch != (char) 0) System.out.print(ch); } } }
Stack class Stack { int st[]; // this array holds the stack int sp; // the stack pointer index Stack(int size) { st = new int[size+1]; // allocate memory for stack sp = 0; } boolean isFull(){ if (sp == (st.length-1)) return true; else return false; } boolean isEmpty(){ if (sp == 0) return true; else return false; }
void push(int n) { if(isFull()) { System.out.println(" -- Stack is full."); return; } sp++; st[sp] = n; } int pop() { int top; if(isEmpty()) { System.out.println(" -- Stack is empty."); return -1000; } top = st[sp]; sp--; return top; } }
// Demonstrate the Stack class. class StackDemo { public static void main(String args[]) { Stack st1 = new Stack(5); int i,n; System.out.println("Using stack st1 to store numbers."); // put some numbers into st1 for(i=1; i < 7; i++) st1.push(i); // pop and display elements from st1 System.out.println("Contents of st1: "); for(i=1; i < 7; i++) { n = st1.pop(); if (n != -1000) System.out.println(n); } } }