EECE 230 Homework 6, Fall 2012

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

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

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

EECE 230 Homework 6, Fall 2012, page 2 EECE 230 Homework 6, Fall 2012, page 3 EECE 230 Homework 6, Fall 2012, page 4 EECE 230 Homework 6, Fall 2012, page 5 EECE 230 Homework 6, Fall 2012, page 6

More EECE 230 files

Show the text of this homework
#include <cmath> #include <iostream> using namespace std; // function prototype double distance(double,double,double,double); double radius(double,double,double,double); double circumference(double); double area(double); int main() { double Xcenter,Ycenter,Xpoint,Ypoint, circleRadius, cicleCircumference, circleArea; cout << "Please enter the coordinates of the circle center Xc and Yc."<<endl; cin>>Xcenter>>Ycenter; cout << "Please enter the coordinates of the point on the circle Xp and Yp."<<endl; cin>>Xpoint>>Ypoint; //radius function call circleRadius = radius(Xcenter,Ycenter,Xpoint,Ypoint); cout<<"The raduis of the circle is: "<<circleRadius<<endl; //circumference function call cicleCircumference = circumference(circleRadius); cout<<"The circumference of the circle is: "<<cicleCircumference<<endl; //area function call circleArea = area (circleRadius); cout<<"The area of the circle is: "<<circleArea<<endl; return 0; } // distance function definition double distance(double x1,double y1,double x2,double y2) { double dist=pow(pow(x1-x2,2)+pow(y1-y2,2),0.5); return dist; } // raduis function definition double radius(double xc,double yc,double xp,double yp) { double rad=distance(xc, yc, xp, yp); return rad; } // circumference function definition double circumference(double radius) { double circum=2*3.14*radius; return circum; } // area function definition double area(double radius) { double a=3.14*pow(radius,2); return a; } #include <iostream> using namespace std; void arrayPrint(int* a, int n); int arraySmallest(int* a,int start,int end); void swap(int& x,int& y); void selectionSort(int* a, int n); void main(){ int *a; cout << "Please enter the number of integers" << endl; int n,i; cin >> n; cout << "Please enter " << n << " integers" << endl; a = new int[n]; for(i=0;i<n;i++) cin >> a[i]; cout << "Before Sorting" << endl; arrayPrint(a,n); selectionSort(a,n); cout << "After Sorting" << endl; arrayPrint(a,n); delete[] a; } void arrayPrint(int a[], int n){ for(int i=0;i<n;i++){ cout << a[i] << " " ; } cout << endl; } int arraySmallest(int* a,int start,int end){ int indexOfMinimum = start; for(int i=start;i<=end;i++) if(a[i]<a[indexOfMinimum]) indexOfMinimum = i; return indexOfMinimum; } void swap(int& x,int& y){ int temp = x; x = y; y = temp; } void selectionSort(int* a, int n){ for(int i=0;i<n;i++){ int k = arraySmallest(a,i,n-1); swap(a[i],a[k]); } } #include <iostream> using namespace std; char myStringDeleteChar(char* str,int i); void main(){ char s[] = "eece230"; char c = myStringDeleteChar(s,3); cout << c << endl; cout << s << endl; return ; } char myStringDeleteChar(char* str,int i){ int len = strlen(str); if((i<0) || (i>=len)) return '\0'; else{ char deleted = str[i]; for(int k=i;k<len;k++) str[k] = str[k+1]; return deleted; } } #include <iostream> #include <cstring> using namespace std; char myStringDeleteChar(char* str,int i); void main(){ char s[] = "eece230"; //We should NOT write char *s = "eece230" ,because no memory would be allocated for the string char c = myStringDeleteChar(s,3); cout << c << endl; cout << s << endl; return ; } char myStringDeleteChar(char* str,int i){ int len = strlen(str); if((i<0) || (i>=len)) return '\0'; else{ char deleted = *(str+i); for(int k=i;k<len;k++) *(str+k) = *(str+k+1); return deleted; } } #include<iostream> using namespace std; void removeDuplicateSpaces(char *,char *); int main() { char s1[500], s2[500]; cout << "Enter string:"; cin.get(s1,500);// read a line (possibly containing spaces) of length // at most 499 characters and strote it into s1. //cout<< s1; removeDuplicateSpaces(s1,s2); cout << "Duplicate spaces removed:"<<endl; cout<< s2<<endl; return 0; } void removeDuplicateSpaces(char *s1,char *s2) { int i = 0,j=0; while(s1[i]!='\0') { if(j==0 || !(s1[i]==' ' && s2[j-1]==' ')) { s2[j] = s1[i]; j++; } i++; } s2[j] = '\0'; }