EECE 230 Homework 10, Fall 2012

American University of Beirut · Introduction to Programming · 14 pages · Dr. Louay Bazzi

All EECE 230 files
EECE 230 Homework 10, Fall 2012, page 1

Stuck on a question? A tutor can walk you through it.

EECE 230 Homework 10, Fall 2012, page 2 EECE 230 Homework 10, Fall 2012, page 3 EECE 230 Homework 10, Fall 2012, page 4 EECE 230 Homework 10, Fall 2012, page 5 EECE 230 Homework 10, Fall 2012, page 6 EECE 230 Homework 10, Fall 2012, page 7 EECE 230 Homework 10, Fall 2012, page 8 EECE 230 Homework 10, Fall 2012, page 9 EECE 230 Homework 10, Fall 2012, page 10

Showing the first 10 of 14 pages. Download the PDF to see all of it.

More EECE 230 files

Show the text of this homework
#include <iostream> using namespace std; #include<cmath> class quadraticPolynomial { public: double a; double b; double c; quadraticPolynomial() {a=b=c=0;}; quadraticPolynomial(double aValue, double bValue,double cValue) { a=aValue;b=bValue;c=cValue; } double evaluate(double w) {return a*w*w+b*w+c;} void print() {cout<<a<<"*x^2 + "<<b<<"*x + "<<c<<endl;} int realRoots(double & x1,double & x2) { if(a==0 && b==0) { if(c==0) return 999; else return 0; } if(a==0) {x1 = -c/b;; return 1;} double Delta = b*b-4*a*c; if(Delta<0) return 0; x1 = (-b+sqrt(Delta))/(2*a); x2 = (-b-sqrt(Delta))/(2*a); return 2; } bool isSquare() { if(a<=0) return false; // corrected on Jan 31(previous version a==0) double Delta = b*b-4*a*c; if(Delta ==0) return true; else return false; } quadraticPolynomial derivative() { quadraticPolynomial D(0,2*a,b); return D; } }; quadraticPolynomial sum(const quadraticPolynomial & P, const quadraticPolynomial & Q) { quadraticPolynomial R; R.a = P.a+Q.a; R.b = P.b+Q.b; R.c = P.c+Q.c; return R; } int main() { quadraticPolynomial p(1,5,3),q(1,2,1), r; cout<<"p: ";p.print(); cout<<"q: ";q.print(); cout<<"r: ";r.print(); cout<<"p(2)="<<p.evaluate(2)<<endl; r = sum(p,q); cout<<"p+q: ";r.print(); r = p.derivative(); cout<<"derivative r of p: ";r.print(); double x1,x2; if(p.realRoots(x1,x2)==2) cout<<"roots of p : "<<x1<<","<<x2<<endl; if(q.realRoots(x1,x2)==2) cout<<"roots of q : "<<x1<<","<<x2<<endl; if(r.realRoots(x1,x2)==1) cout<<"root of r : "<<x1<<endl; if(q.isSquare()) cout<<"q is a square"; else cout<<"q is not a square"; cout<<endl; if(p.isSquare()) cout<<"p is a square"; else cout<<"p is not a square"; cout<<endl; return 0; } #include <iostream> using namespace std; #include <cmath> //////////////////////////////////////////////////////////////////////////////// //////////////////// class myDynamicArray { private: double * A; int size; public: myDynamicArray(int sizeValue); myDynamicArray(); myDynamicArray(const myDynamicArray & other); ~myDynamicArray(); const myDynamicArray & operator=(const myDynamicArray & other); void copy(const myDynamicArray & copied); void set(int i, double x); double get(int i) const; int getSize() const; void print() const; int search(double query) const; bool isSorted() const; void insertionSort(); void concatinate(myDynamicArray & B); }; //////////////////////////////////////////////////////////////////////////////// //////////////////// void main() { myDynamicArray X(3),Y; X.set(0,3.1); X.set(1,3.2); X.set(2,1.1); Y.print(); Y=X; Y.print(); } //////////////////////////////////////////////////////////////////////////////// //////////////////// myDynamicArray ::myDynamicArray() { A= new double[10]; size=10; for(int i=0;i<size;i++) A[i]=0; } myDynamicArray ::myDynamicArray(int sizeValue) { size=sizeValue; A=new double[size]; for(int i=0;i<size;i++) A[i]=0; } myDynamicArray ::~myDynamicArray() { delete [] A; size = 0; } myDynamicArray ::myDynamicArray(const myDynamicArray & other) { copy(other); } const myDynamicArray & myDynamicArray ::operator=(const myDynamicArray & other) { if(this != & other) copy(other); return *this; } void myDynamicArray ::set(int i, double x) { A[i] = x; } double myDynamicArray ::get(int i) const { return A[i]; } int myDynamicArray ::getSize() const { return size; } int myDynamicArray ::search(double query) const { for(int i=0; i<size; i++) if(A[i]==query) return i; return -1; } void myDynamicArray ::print() const { for(int i=0;i<size;i++) cout<<A[i]<<" "; cout<<endl; } bool myDynamicArray ::isSorted() const { bool sorted=true; for(int i=1;i<size;i++) if(A[i-1]>A[i]) sorted=false; return sorted; } void myDynamicArray ::insertionSort() { double temp; for(int j=1;j<size;j++) { temp=A[j]; int i; for(i=j-1; (A[i]>temp) && (i>-1);i--) A[i+1]=A[i]; A[i+1]=temp; } } void myDynamicArray ::copy(const myDynamicArray & copied) { delete [] A; A = new double[copied.size]; size=copied.size; for(int i=0; i<size; i++) A[i]=copied.A[i]; } void myDynamicArray ::concatinate(myDynamicArray & B) { double * final; final = new double[size+B.size]; for(int i=0;i<size;i++) final[i]=A[i]; int i; for(i=size; i<(size+B.size); i++) final[i]=B.A[i-size]; delete [] A; A = final; size=size + B.size; } //////////////////////////////////////////////////////////////////////////////// ///////////////////// #include<iostream> using namespace std; #include <ctime> #include "mySortingAlgorithms.h" void main() { //////////////////////////////////////////// int A[] = {1,23,5,2,4}; //arrayPrint(A,5);cout<<endl; insertionSort(A,5); arrayPrint(A,5);cout<<endl; double B[] = {1.1,23.1,5.1,2.1,4.1}; //arrayPrint(B,5);cout<<endl; insertionSort(B,5); arrayPrint(B,5);cout<<endl; ///////////////////////////////////////////// int C[] = {1,23,5,2,4}; //arrayPrint(C,5);cout<<endl; selectionSort(C,5); arrayPrint(C,5);cout<<endl; double D[] = {1.1,23.1,5.1,2.1,4.1}; //arrayPrint(C,5);cout<<endl; selectionSort(C,5); arrayPrint(C,5);cout<<endl; ///////////////////////////////////////////// int E[] = {1,23,5,2,4}; //arrayPrint(E,5);cout<<endl; mergeSort(E,0,4); arrayPrint(E,5);cout<<endl; double F[] = {1.1,23.1,5.1,2.1,4.1}; //arrayPrint(F,5);cout<<endl; mergeSort(F,0,4); arrayPrint(F,5);cout<<endl; ///////////////////////////////////////////// int G[] = {1,23,5,2,4}; //arrayPrint(G,5);cout<<endl; srand((unsigned)time(NULL)); randQuickSort(G,0,4); arrayPrint(G,5);cout<<endl; double H[] = {1.1,23.1,5.1,2.1,4.1}; //arrayPrint(H,5);cout<<endl; randQuickSort(H,0,4); arrayPrint(H,5);cout<<endl; ///////////////////////////////////////////// } #include <iostream> using namespace std; template<class T> void arrayPrint(T a[], int n); template<class T> void insertionSort(T A[], int n); template<class T> int arraySmallest(T a[],int start,int end); template<class T> void selectionSort(T a[], int n); template<class T> void mergeSort(T A[],int p,int r); template<class T> void Merge(T A[], int p, int q, int r); template<class T> void merge15(const T L[],int m,const T R[], int n, T C[] ); template<class T> int partition(T A[],int,int); template<class T> void randQuickSort(T A[], int low, int high); /////////////////////////////////////////////////////////////// template<class T> void arrayPrint(T a[], int n){ for(int i=0;i<n;i++){ cout << a[i] << " " ; } cout << endl; } ///////////////////////////////////////////////////////////// template<class T> void insertionSort(T A[], int n) { for(int j=1;j<n;j++) { T key = A[j]; // Insert A[j] into the sorted sequence A[0j-1] int i = j-1; while(i>=0 && A[i] > key) { A[i+1] = A[i]; // move the elements ahead i =i-1; // and go one step back } A[i+1] = key; } } ///////////////////////////////////////////////////////////////// template<class T> int arraySmallest(T a[],int start,int end) { int indexOfMinimum = start; for(int i=start;i<=end;i++) if(a[i]<a[indexOfMinimum]) indexOfMinimum = i; return indexOfMinimum; } template<class T> void selectionSort(T a[], int n) { for(int i=0;i<n;i++){ int k = arraySmallest(a,i,n-1); swap(a[i],a[k]); } } /////////////////////////////////////////////////////////// template<class T> void mergeSort(T A[],int p,int r) { if(p<r) { int q = (p+r)/2; mergeSort(A,p,q); mergeSort(A,q+1,r); Merge(A,p,q,r); } } template<class T> void Merge(T A[], int p, int q, int r) { T * B = new T[q-p+1]; T * C = new T[r-q]; T * D = new T[r-p+1]; for(int i=p;i<=q;i++) B[i-p]=A[i]; //copy A[p...q] to B for(i=q+1;i<=r;i++) C[i-q-1]=A[i]; //copy A[q+1...r] to C merge15(B,q-p+1,C,r-q,D); // from lecture 15 // merges C and B into a sorted array and stores it in D for(i=p;i<=r;i++) A[i]=D[i-p]; //copy D to A[p...r] delete [] B; delete [] C; delete [] D; } template<class T> void merge15(const T L[],int m,const T R[], int n, T C[] ) // C is passed by reference. //Thus we assume that memory is allocated for C before calling the function { int i =0, j = 0; for(int k = 0;k<m+n;k++) { if (i< m && j < n) if (L[i]<R[j]) { C[k] = L[i]; i++; } else { C[k] = R[j]; j++; } else if (i==m) {C[k] = R[j]; j = j+1;} // if all the elements of L are in C, dump R in C else if (j==n) {C[k] = L[i]; i = i+1;} // if all the elements of R are in C, dump L in C } } ///////////////////////////////////////////////////////////////// template<class T> void randQuickSort(T A[], int low, int high) { if(high>low){ int randomIndex = low + rand()%(high-low+1); swap(A[randomIndex],A[high]); int q = partition(A,low,high); randQuickSort(A,low,q-1); randQuickSort(A,q+1,high); } } template<class T> int partition(T A[],int low,int high) { int i=low; if(high-low<1) return 0; int j=high-1; while (i<j) { while (A[i]<=A[high] && i<j) i++; while (A[j]>=A[high] && i<j) j--; swap(A[i],A[j]); } if(A[i]>=A[high]) { swap(A[i],A[high]); return i; } else { swap(A[i+1],A[high]); return i+1; } } ///////////////////////////////////////////////////////////////// #include <iostream> using namespace std; const int matrixSize = 4 ; void createArithmeticSeq(int A[matrixSize*matrixSize]); void matricize(int A[matrixSize*matrixSize],int B[matrixSize][matrixSize]); void magicCheck(int A[matrixSize*matrixSize],int B[matrixSize][matrixSize]); void reverseDiagonal(int B[matrixSize][matrixSize]); void printMatrix(int B[matrixSize][matrixSize]); void testMagicCheck(); int main() { int list[matrixSize*matrixSize]; int matrix[matrixSize][matrixSize]; createArithmeticSeq(list); matricize(list,matrix); reverseDiagonal(matrix); printMatrix(matrix); magicCheck(list,matrix); return 0; } void createArithmeticSeq(int A[matrixSize*matrixSize]){ int first,diff; cout << "Please enter the first term of the arithmetic sequence" <<endl; cin >> first; cout << "Please enter the difference between 2 consecutive terms"<<endl; cin >> diff; int val = first; for(int i=0;i<matrixSize*matrixSize;i++){ A[i]=val; val+=diff; } } void matricize(int A[matrixSize*matrixSize],int B[matrixSize][matrixSize]){ int k=0; for(int i=0;i<matrixSize;i++) for(int j=0;j<matrixSize;j++) B[i][j]=A[k++]; } void reverseDiagonal(int B[matrixSize][matrixSize]){ for(int i=0;i<matrixSize/2;i++){ swap(B[i][i],B[matrixSize-i-1][matrixSize-i-1]); swap(B[i][matrixSize-i-1],B[matrixSize-i-1][i]); } } void magicCheck(int A[matrixSize*matrixSize],int B[matrixSize][matrixSize]){ int sum=0,magicNumber; int i,j; for(i=0;i<matrixSize*matrixSize;i++) sum += A[i]; magicNumber = sum/4; bool isMagic=true; int rowSums[matrixSize]={0}; int colSums[matrixSize]={0}; int firstDiagonalSum=0; int secondDiagonalSum=0; for(i=0;i<matrixSize;i++){ for(j=0;j<matrixSize;j++){ rowSums[i]+=B[i][j]; colSums[j]+=B[i][j]; if(i==j) firstDiagonalSum+=B[i][j]; if(i==matrixSize-1-j) secondDiagonalSum+=B[i][j]; } } for(i=0;i<matrixSize;i++){ if((rowSums[i]!=magicNumber)||(colSums[i]!=magicNumber)){ isMagic = false; break; } } if((firstDiagonalSum!=magicNumber)||(secondDiagonalSum!=magicNumber)) isMagic = false; if(isMagic) cout << "It is a magic square" << endl; else cout << "it is not a magic square" << endl; } void printMatrix(int B[matrixSize][matrixSize]){ cout << endl; for(int i=0;i<matrixSize;i++){ for(int j=0;j<matrixSize;j++){ cout << B[i][j] << "\t" ; } cout <<endl<<endl<<endl<<endl<<endl ; } }