EECE 231 Exam 1 Solution, Fall 2014

American University of Beirut · Introduction to Programming Using C++ and MATLAB · 4 pages · Prof. Fadi Zaraket · with solution

All EECE 231 files
EECE 231 Exam 1 Solution, Fall 2014, page 1

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

EECE 231 Exam 1 Solution, Fall 2014, page 2 EECE 231 Exam 1 Solution, Fall 2014, page 3 EECE 231 Exam 1 Solution, Fall 2014, page 4

More EECE 231 files

Show the text of this exam
#include<iostream> using namespace std; int main() { double px, py, w,h; cout << "Enter p_x and p_y: "; cin>>px>>py; cout << "Enter width: "; cin>>w; cout << "Enter height: "; cin >> h; cout<<"a) Coordinates of the other 3 corners: "<<"("<<px+w<<","<<py<<"), "<<"("<<px<<","<<py+h<<"), "<<"("<<px+w<<","<<py+h<<"), "<<endl<<endl; cout << "b) Enter x and y: "; double x,y; cin>>x>>y; if( px<=x && x<=px+w && py<=y && y<=py+h ) cout<<"YES: ("<<x<<","<<y<<") is inside the rectangle"; else cout<<"NO: ("<<x<<","<<y<<") is not inside the rectangle"; cout<<endl<<endl; cout << "c) Points with integer coordinates inside the rectangle:"<<endl; int i0=static_cast<int>(px); if(i0<px) i0=i0+1; int j0=static_cast<int>(py); if(j0<py) j0=j0+1; for(int i=i0;i<px+w;i++) { for(int j=j0;j<py+h;j++) cout<<"("<<i<<","<<j<<") "; cout<<endl; } cout<<endl; return 0; } #include<iostream> using namespace std; int main() { int n; cout<<"Enter n:"; cin>>n; if(n<=0) { cout<<"Input not postive"<<endl; return 0; } bool answer=false; int d; for(d = 2;d*(d+1)<=n; d++) if (n%d ==0 && n%(d+1)==0) { answer = true; break; } if(answer) cout<<"YES " << n<< " is divisible by the consecutive integers "<<d<<" and "<<d+1; else cout<<"NO"; cout<<endl; return 0; } #include<iostream> using namespace std; int main() { while(true){ int n; cout<<"Enter n:"; cin>>n; if(n<=0) { cout<<"Input not postive"<<endl; return 0; } bool answer=false; int d; int m = 1; for(d = 2;(d-1)*d<=n; d++) if (n%d ==0) { m = m*d; if(n==m && n!=2) { answer = true; break; } } else m = 1; if(answer) cout<<"YES "; else cout<<"NO"; cout<<endl; } return 0; } #include<iostream> using namespace std; #include<cmath> int main() { //////////////////////////////////////////////////////// /// read the integers ////////////////////////////////// /*double A[1000]; int n; cout << "Enter number of integers in the list:"; cin>>n; cout << "Enter " << n <<" integers:"; for(i=0;i<n;i++) cin>>A[i];*/ double A[] = {1.5,10.1,3,2.2,6,7}; int n = 6; //a) ////////////////////////////////////////////////////// double sum =0; for(int j = 0;j<n;j++) sum = sum + abs(A[j]-A[0]); cout<<"Cumulative distance from first element: "<<sum<<endl; //////////////////////////////////////////////////////// //b) ////////////////////////////////////////////////////// double minSum =sum; int iOpt = 0; for(int i = 1;i<n;i++) { double sum = 0; for(int j = 0;j<n;j++) sum = sum + abs(A[j]-A[i]); if(sum<minSum) { iOpt = i; minSum = sum; } } cout<<"iOpt: "<< iOpt<<endl; // Solution 2: // Copy A into another array B // Sort the array B using selection sort // Find index iOpt of B[n/2] in A // return iOpt // This works since it can be shown that the median is the best choice //////////////////////////////////////////////////////// return 0; }