EECE 230 Problems, Spring 2008

American University of Beirut · Introduction to Programming · 7 pages

All EECE 230 files
EECE 230 Problems, Spring 2008, page 1

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

EECE 230 Problems, Spring 2008, page 2 EECE 230 Problems, Spring 2008, page 3 EECE 230 Problems, Spring 2008, page 4 EECE 230 Problems, Spring 2008, page 5 EECE 230 Problems, Spring 2008, page 6 EECE 230 Problems, Spring 2008, page 7

More EECE 230 files

Show the text of this problems
#include <iostream> using namespace std; int desiredMove = 0; int currentMove = 0; int numOfTransfers(int n){ if(n==0) return 0; if(n==1) return 1; return 2*numOfTransfers(n-1) + 1; } void towersOfHanoi(char source, char mid, char dest, int n){ if(n>0){ int a = numOfTransfers(n-1); if(a<desiredMove-currentMove){ currentMove+=a; currentMove++; if(currentMove == desiredMove){ cout << source << " to "<< dest << " (Disk" << n << ")" << endl; } towersOfHanoi(mid,source,dest,n-1); } else{ towersOfHanoi(source, dest, mid, n-1); } } } int main(){ int n; cout << "Please enter the number of disks" << endl; cin >> n; cout << "Please enter the rank of the transfer you wish to display" << endl; cin >> desiredMove; towersOfHanoi('A','B','C',n); return 0; } #include <iostream> using namespace std; int desiredMove,currentMove; void towersOfHanoi(char source, char mid, char dest, int n){ if(n>0){ towersOfHanoi(source,dest,mid,n-1); currentMove++; if(currentMove == desiredMove){ cout << source << " to "<< dest << " (Disk" << n << ")" << endl; } towersOfHanoi(mid,source,dest,n-1); } } int main(){ cout << "Please enter the number of Disks" << endl; int n; cin >> n; cout << "Please enter the rank of the desired transfer" << endl; cin >> desiredMove; currentMove = 0; towersOfHanoi('A','B','C',n); return 0; } #include <iostream> using namespace std; void towersOfHanoi(char source, char mid, char dest, int n){ if(n>0){ towersOfHanoi(source,dest,mid,n-1); cout << source << " to "<< dest << " (Disk" << n << ")" << endl; towersOfHanoi(mid,source,dest,n-1); } } int main(){ cout << "Please enter n" << endl; int n; cin >> n; towersOfHanoi('A','B','C',n); return 0; } //Parts b and c are left without solution, as to allow the students to find the solution by themselves //These problems are all optional and not required for the exam. #include <iostream> using namespace std; bool IntegerSum(int a[], int target,int pos, int n){ if(pos==n) return false; if(a[pos]==target) return true; if(IntegerSum(a,target-a[pos],pos+1,n)) return true; if(IntegerSum(a,target,pos+1,n)) return true; return false; } int main(){ int a[20] = {4,21,23,31,32,51,16,7,46, 28,36,99, 67, 73, 2, 3, 19, 18, 29, 30}; int target; cin >> target; if(IntegerSum(a,target,0,20)) cout << "There is a subset whose element sum is equal to the target" << endl; else cout << "There is no subset whose element sum is equal to the target" << endl; return 0; } #include <iostream> using namespace std; void printstars(int n){ if(n==0) { cout << endl; return; } cout << "*" ; printstars(n-1); } void Pattern1(int n){ if(n==1){ cout <<"*"<<endl; return; } printstars(n); Pattern1(n-1); printstars(n); } void Pattern2(int n,int m){ if(n==m){ printstars(n); return; } printstars(n); Pattern2(n+1,m); printstars(n); } void Pattern3(int n){ if(n==1){ cout <<"0"<<endl<<"*"<<endl<<"0"<<endl; return; } Pattern3(n-1); printstars(n); Pattern3(n-1); } int main(){ cout << "Please enter n" << endl; int n; cin >> n; cout << "Pattern 1" << endl; Pattern1(n); cout <<endl<<"-----------------------------------------------------------"<<endl; cout << "Pattern 2" << endl; Pattern2(1,n); cout <<endl<<"-----------------------------------------------------------"<<endl; cout << "Pattern 3" << endl; Pattern3(n); return 0; } #include<iostream> using namespace std; int reverse(int n,int& size){//returns both the reversed integer and the largest power of 10 that is smaller than it. if(n<10){ size=1; return n; } int lastDigit = n%10; int remainingPart = n/10; int p10; int rev = reverse(remainingPart,p10); int returnValue = rev + lastDigit*p10*10; size = p10*10; return returnValue; } int main(){ int z; int k = reverse(10345,z); cout << k << endl; return 0; } #include <iostream> using namespace std; //Note:Part B is left without solution, since those problems are optional and //not required for the quiz, and to give some incentive to students to solve //the problem themselves. double howManyWays(int a,int b){ double retval=0; if((a==0) && (b==1)) return 1.0; if(a>0) retval = howManyWays(a-1,b+1); if(b>0) retval+=howManyWays(a,b-1); return retval; } int main(){ cout << "The initial number of disks on peg B is assumed to be 0"<<endl; cout << "Please enter the number n of disks on peg A (n>0)" << endl; int n; cin >> n; double d = howManyWays(n,0); cout << "the number of possible ways to transfer " << n << " disks " << "from peg A to peg C is " << d << endl; return 0; }