Question: C++ Programming HW Im trying to understand why my cout < cin are not working. The hw is almost complete. Please help the program run

C++ Programming HW

Im trying to understand why my cout < cin are not working. The hw is almost complete.

Please help the program run successfully.

The problems are from chapter 11 of Introduction to Programming with C++ by Daniel Liang #include #include #include #include //#include"Circle2D.h" #define PI 3.14159265 using namespace std;

//PE 11.1 int Prog11_1() { // Declare the variables int i,j,arraySize,sum=0,count=0,value; double average=0; // Declare the pointer variable int *numbers;

// Read the size of an array cout << "Enter array size: "; cin >> arraySize; /* Declare a pointer variable as dynamic array and initialize its size */ numbers = new int[arraySize];

cout << "Enter integer values : "; for (i = 0; i < arraySize; i++) { // Read and store numbers in an array cin >> value; *(numbers + i) = value; sum=sum+value; } // Calculate the average of the numbers average = sum/arraySize; cout << "Average number is: " ;

cout << "Above average numbers are: " <

for (j = 0; j < arraySize; j++) { if(*(numbers + j)>average) { cout<<*(numbers + j)<< " "; count++; } cout<<"Total of above average number is " < }

// Pause the system system("pause"); // Return the value return 0; }

//PE11.3

//Function prototype int *doubleCapacity(int *list, int size);

int Prog11_3() { // Declaration section int size,*list,*newlist,doubleSize,i,j,value; cout << "Enter the size of the array:"; cin >> size; // Dynamically initialize the size of the array list = new int[size]; // Use for loop to read and store values in an array cout << "Enter integer values :"; for (i = 0; i { cin >> value; *(list + i) = value; } /* Processing section to increase the array to double in size*/ doubleSize = size * 2;

newlist=doubleCapacity(list, doubleSize); /* Use for loop to read and store additional numbers in an array*/ cout << "Array gets double in size, so enter more values: "; for (i = size; i { cin >> value; *(newlist + i) = value; }

/* Use for loop to print the numbers stored in an array*/ cout << "Array Elements after double in size : "; for (j = 0; j { cout<<*(newlist + j) <<" "; } //Pause the system system("pause"); //Return the value return 0; }

int *doubleCapacity(int *list, int size) { /* Create a new dynamic array, and initialize the size*/ int *p = new int[size]; for (int i = 0; i { /*Copy the content from old array to new dynamic array*/ p[i] = list[i]; } //Return the new size increased array return p; } //PE11.4 //Function prototype int average(int *array, int size); double average(double *array, int size); //Function main int Prog11_4() { const int SIZE = 10; // Declare the array variable double list[SIZE];

/*Read input from the user, calculate the average of the array, and return it.*/ cout << "Enter ten numbers: "; for (int i = 0; i < SIZE; i++) { cin >> list[i]; }

cout << "Average is " << average(list, SIZE)<< endl; //Pause the system system("pause"); // Return the value return 0; }

int average(int *array, int size) { int total = 0; //Calculate the sum of the array for (int i = 0; i < size; i++) { total += *(array + i); } //Calculate the average of the array and return it return total / size; }

double average(double * array, int size) { double total = 0; //Calculate the sum of the array for (int i = 0; i < size; i++) { total += *(array + i); } //Calculate the average of the array and return it return total / size; } // PE 11.5

int minIndex(double *list, int size); int Prog11_5() { //Declare the pointer variable double *list; //Declare the variable int num;

cout <<"Enter array size :"; cin >> num; //Create the pointer array variable list=new double[num];

//Read input from the user for (int i=0; i { cout <<"Enter number " << i+1 << " : "; cin >> list[i]; } //Display the index of the minimum value cout << " The index of the minimum value is " << minIndex(list, num)+1 << endl; //Display the smallest element in the array cout << "The minimum value is " < //Pause the system system("pause"); //Return the value return 0; }

int minIndex(double *list, int size) { /* Assign the first array element as the minimum value for the comparison operation */ double min = list[0]; int index = 0;

for (int i = 1; i < size; i++) { /*Compare all the array elements with min variable to find the minimum value*/ if (list[i] < min ) { min = list[i]; index = i; } } // Return index of the minimum value return index; }

//PE11.8 #ifndef CIRCLE_H #define CIRCLE_H

class Circle2D { public: Circle2D(); Circle2D(double, double, double); //declaration of get functions //for x axis double getX()const; //for y axis double getY()const; //for redius double getRadius()const; //functions to calculate area and perimeter double getArea()const; double getPerimeter()const; bool contains(double, double); bool contains(const Circle2D& circle); bool overlaps(const Circle2D& circle); private: double x; double y; double radius; }; #endif

//creates a no-arg object Circle2D::Circle2D() { x = 0 y = 0; radius = 1; }

// object is created with specified input values Circle2D::Circle2D(double x, double y, double r) { x = x; y = y; radius = r; } //get functions for radius and (x,y) coordinates double Circle2D::getRadius()const { return radius; } double Circle2D::getX()const { return x; } double Circle2D::getY()const { return y; } //to calculate area of circle double Circle2D::getArea()const { return PI*radius*radius; } //to calculate perimeter of circle double Circle2D::getPerimeter()const { return 2*PI*radius; } //function to check whether a specified point lies inside circle bool Circle2D::contains(double x, double y) { double pointX = x; double pointY = y; double circleRadius = radius;

double temp = pow( pow((pointX - this->x),2) + pow((pointY - this->y),2) , 0.5); // Check if the distance from the center to the radius is larger than center to the point if (temp < circleRadius) return true; else false; } //function to check whether two circles overlapped bool Circle2D::overlaps( const Circle2D &circle) { double c2Radius = circle.getRadius(); double c2X = circle.getX(); double c2Y = circle.getY(); // 'this' pointer points to the object which invokes // the function double c1Radius = this->getRadius(); double c1X = this->getX(); double c1Y = this->getY();

// distance between two centers D double X = c2X - c1X; double Y = c2Y - c1Y; double D = (X*X + Y*Y); //summation of two radii double sumRad = c2Radius + c1Radius; double sr = sumRad*sumRad; //difference of two radii double diffRad = c2Radius - c1Radius; double dr = diffRad*diffRad; // condition that must hold for overlapping if (D <= sr && D>=dr) return true; else return false; } //function to check whether specified circle lies inside the circle bool Circle2D::contains(const Circle2D& circle) { double c2Radius = circle.getRadius(); double c2X = circle.getX(); double c2Y = circle.getY(); // 'this' pointer points to the object which invokes the function double c1Radius = this->getRadius(); double c1X = this->getX(); double c1Y =this->getY();

//condition must hold for lying specified circle within a circle if((contains(c2X,c2Y))&&(c1Radius>c2Radius)) return true; else return false; } /********************************************************** * Test program for Circle2D class-TestCircle2D.cpp * **********************************************************/

using namespace std; int Prog11_8() { Circle2D c1( 2, 2, 5.5 ); Circle2D c2( 2, 2, 5.5 ); Circle2D c3( 4, 5, 10.5 ); //determine area and perimeter of circle c1 cout<<"The area of circle ( 2, 2, 5.5 ) = "<

//check whether circle c2 lies inside circle c1 if(c1.contains(c2)) cout<<"The circle ( 2, 2, 5.5 ) is inside the circle ( 2, 2, 5.5 )."<

//check whether circle c3 overlaps circle c1 if(c1.overlaps(c3)) cout<<"The circle ( 4, 5, 10.5 ) overlaps the circle ( 2, 2, 5.5 )."<

bool contains(double, double); bool contains(const Circle2D& circle); bool overlaps(const Circle2D& circle); private: double x; double y; double radius; }; #endif

Default constructor creates circle having center ( 0, 0 ) and radius = 1. //creates a no-arg object Circle2D::Circle2D() { x = 0;

y = 0; radius = 1; } Argument constructor initiates all data fields with the specified values that have been passed as an argument in constructor. // object is created with specified input values Circle2D::Circle2D(double x, double y, double r) { x = x; y = y; radius = r; } //get functions for radius and (x,y) coordinates double Circle2D::getRadius()const { return radius; } double Circle2D::getX()const { return x; } double Circle2D::getY()const { return y; } //to calculate area of circle double Circle2D::getArea()const { return PI*radius*radius; } //to calculate perimeter of circle double Circle2D::getPerimeter()const { return 2*PI*radius; } //function to check whether a specified point lies inside circle bool Circle2D::contains(double x, double y) { double pointX = x; double pointY = y; double circleRadius = radius; a point( x1, y1 ) lies within a circle ( x0, y0, radius) if double temp = pow( pow((pointX - this->x),2) + pow((pointY - this->y),2) , 0.5); // Check if the distance from the center to the radius is larger than center to the point if (temp < circleRadius) return true; else false; } //function to check whether two circles overlapped

bool Circle2D::overlaps( const Circle2D &circle) { double c2Radius = circle.getRadius(); double c2X = circle.getX(); double c2Y = circle.getY(); // 'this' pointer points to the object which invokes // the function double c1Radius = this->getRadius(); double c1X = this->getX(); double c1Y = this->getY(); Two circles overlapped or intersect if the distance between their centers is lies between the sum and the difference between their radii. Means circle (x0, y0, r0) and circle (x1, y1, r1) overlaps if // distance between two centers D double X = c2X - c1X; double Y = c2Y - c1Y; double D = (X*X + Y*Y); //summation of two radii double sumRad = c2Radius + c1Radius; double sr = sumRad*sumRad; //difference of two radii double diffRad = c2Radius - c1Radius; double dr = diffRad*diffRad; // condition that must hold for overlapping if (D <= sr && D>=dr) return true; else return false; } //function to check whether specified circle lies inside the circle bool Circle2D::contains(const Circle2D& circle) { double c2Radius = circle.getRadius(); double c2X = circle.getX(); double c2Y = circle.getY(); // 'this' pointer points to the object which invokes the function double c1Radius = this->getRadius(); double c1X = this->getX(); double c1Y =this->getY(); For lying a specified circle within another circle, the center of the specified circle must lie inside the circle and the radius of specified circle must less than the radius of another circle. //condition must hold for lying specified circle within a circle if((contains(c2X,c2Y))&&(c1Radius>c2Radius)) return true; else return false; }

#include #include"Circle2D.h" using namespace std; int main() { Circle2D c1( 2, 2, 5.5 ); Circle2D c2( 2, 2, 5.5 ); Circle2D c3( 4, 5, 10.5 ); //determine area and perimeter of circle c1 cout<<"The area of circle ( 2, 2, 5.5 ) = "<

//check whether circle c3 overlaps circle c1 if(c1.overlaps(c3)) cout<<"The circle ( 4, 5, 10.5 ) overlaps the circle ( 2, 2, 5.5 )."<

the circle ( 2, 2, 5.5 )."<

int* count(const string& s) { int* TotalCount = new int[26];

// for loop to count the total character for (int a = 0; a < 26; a++) TotalCount[a] = 0; for (int a = 0; a < s.size(); a++) { if (isalpha(s[a])) TotalCount[tolower(s[a]) - 'a'] ++; } // get the total count return TotalCount; }

// main function int Prog11_10() { // get the string to get the count int* TotalCount = count(string("ABcab")); for (int a = 0; a < 26; a++) cout << static_cast(a + 'a') << " " << TotalCount[a] << endl; system("Pause"); return 0; }

//PE11.14 //Implementation of MyString class class MyString { public: //default sets string to NULL MyString(void); //Takes in a string and creates an object MyString(const char* input); //displays length of string excluding null int length();

MyString();MyString(const char * cString);char at(int index) const ;int length() const ;void clear

//clear the inputted string void clear(); // if string is empty returns true else it returns //false char& at(int loc bool empty(); //compare one string with another int compare(const MyString &s) ; // compare a string with its index and number of //characters int compare(int index,int n, const MyString &s);

void copy (char s[],int index,int n);

int find(char ch);

int find(char ch,int index);

int find (const MyString& S, int index); private: char* nstring; short nlength; }; //default constructor MyString::MyString() { nlength = 1; nstring = new char[nlength]; nstring[nlength-1] = '\0'; } //takes a string with object created MyString::MyString(const char *input) { nlength = strlen(input)+1 ; nstring = new char[nlength]; for (int i = 0; i < (nlength - 1); i++) { nstring[i] = input[i]; } nstring[(nlength-1)] = '\0'; }

int MyString::length() { int i = 0; while (nstring[i] != '\0') { i++; }

return i; }

void MyString::clear() { std::string str; int i=0; std::cout << "Please enter a string to clear"; cin>>str; while(str[i] != '\0') { str[i]=' ';i++; } } char& MyString::at(int loc) { return nstring[loc-1]; }

bool MyString::empty() { if(nstring[0] == '\0'&& nlength == 1){return true;} else {return false;} }

int MyString::compare(const MyString &str2) { string str1; nlength = str2.nlength; nstring = new char[nlength]; for (int i = 0; i < (nlength - 1); i++) { nstring[i] = str2.nstring[i]; } int i=0,flag=0; while (str1[i]!='\0' && nstring[i]!='\0') { if(str1[i]!=nstring[i]){ flag=1; break; } i++; } if (flag==0 && str1[i]=='\0' && nstring[i]=='\0') return 1; else

return 0; }

int MyString::compare(int index,int n,const MyString &str2) { string str="ab"; nlength = str2.nlength; nstring = new char[nlength]; for (int i = 0; i < (nlength - 1); i++) { nstring[i] = str2.nstring[i]; } int k=0; for(int j=index; j

void MyString::copy(char s[],int index,int n) { char s1[50]; int j=0; for(int i=index;i

int MyString::find(char ch) { string str; cout<<"Enter string for finding a character:"; cin>>str; for(int i=0; str[i]!='\0';i++) { if(str[i]==ch) return 1; } return 0; }

int MyString::find(char ch, int index)

{ string str; cout<<"Enter string for finding a character in a particular position:"; cin>>str; if(str[index]==ch) return 1; else return 0; }

int MyString::find(const MyString &s, int index) { nlength = s.nlength; nstring = new char[nlength]; for (int i = 0; i < (nlength - 1); i++) { nstring[i] = s.nstring[i]; } if(nstring[index]=='a') return 1; else return 0; } //main function int Prog11_14() { //object created MyString A; string s, str; cout<<"Enter a string:"<>k; char c=s.at(k); cout<<"The character is:"<>str; int i=str.compare(s);

if(i==0) cout<<"They are equal"<>index; cout<<"Enter no of chars:"; cin>>n; int j=str.compare(index,n,str); if(j==0) cout<<"The characters are equal:"<>index; cout<<"Enter no of chars:"; cin>>n; char st[]={"pqrst"}; A.copy(st,index,n); int f=A.find('c'); if(f==0) cout<<"character not found in the string."<>str; cout<<"Enter the position"<>pos; int f2=s.find(str,pos); if(f2==0) cout<<"character 'a' not found in the string at that position."<

class MyString { public: //default constructor MyString() { size = 0; }

//constructor to declar string with char ch MyString(const char ch, int size) { value[0] = ch; this->size = size; }

//constructor to declare string with size MyString(const char chars[], int size) { for (int i = 0; i < size; i++) value[i] = chars[i]; this->size = size; }

//method to append two strings MyString append (const MyString& s) { size += s.size; while(size >= MAXIMUM_SIZE) { grow(); } strcat(value, s.value); value[size + 1] = '\0'; return *this; }

//method to append second string from given index to n //chars to string 1 MyString append (MyString & s, int index, int n) { size =size+n-index; while(size >= MAXIMUM_SIZE) { grow(); } value[index + 1] = '\0'; strncat(value, s.value,n); value[size + 1] = '\0'; return *this; }

//method to append char n times to string 1 end MyString append(int n,char ch) { size=size+n; while(size >= MAXIMUM_SIZE) { grow(); } for(int i=0;i

//method to assing string2 to string 1 MyString assign(const char* chars) { // Copy the new char* data into the string data strcpy(value, chars); // Return myself return *this; }

//method to assign string2 from given index to n position // to string 1 MyString assign (MyString & s, int index, int n) { size=n-index; strcpy(value,"\0"); for(int i=index,j=0;i

//method to assing first n chars of string2 to string 1 MyString assign (MyString & s, int n) { size=n; strcpy(value,"\0"); for(int i=0,j=0;i

//method to assign n times char ch MyString assign (int n,char ch) { size=n; strcpy(value,"\0"); for(int i=0;i

//method to take substring from string 1 and assign to // string 2 MyString substr(int index, int n)const { MyString s; if(n>size) { cout<<" not enough length:"; return *this; } else { for(int i=index,j=0;i<=n;i++,j++) { s.value[j]=value[i]; }

s.value[n-index]='\0'; return s; } }

//method to take substring from given index MyString substr(int index)const { MyString s; for(int i=index,j=0;i

//method to earse the chars from index to n MyString erase(int index,int n) { size = n; value[index] = '\0'; return *this; }

//method to resize the array of char void grow() { char* temp; temp = value; char value[MAXIMUM_SIZE]; strcpy(value, temp); value[size] = '\0'; delete [] temp; }

//data members private: char value[MAXIMUM_SIZE]; int size; };

//main function to test the above functions int Prog11_15() { char name[20]="Hello World!"; char name2[20]="Good day"; MyString stringObject1(name,12); MyString stringObject2(name2,8); MyString str=stringObject1.append(stringObject2); printf(" %s",str); MyString(); str=stringObject1.append(stringObject2,3,3); printf(" %s",str); MyString(); str=stringObject1.append(3,'!'); printf(" %s",str); MyString(); str=stringObject1.assign("Hello"); printf(" %s",str); MyString(); str=stringObject1.assign(stringObject2,2,5); printf(" %s",str); MyString(); str=stringObject1.assign(stringObject2,3); printf(" %s",str); MyString(); str=stringObject1.assign(10,'*'); printf(" %s",str); MyString(); str=stringObject2.substr(2,6); printf(" %s",str); MyString(); str=stringObject2.substr(2); printf(" %s",str); str=stringObject2.erase(2,3); printf(" %s",str); cout << endl; return 0; }

*/

int main() { while (true) { system("cls"); cout << " Main Menu - Chapter 11" << endl; cout << "==============================" << endl; cout << " 1: Programming Exercise 11.1" << endl; cout << " 3: Programming Exercise 11.3" << endl; cout << " 4: Programming Exercise 11.4" << endl; cout << " 5: Programming Exercise 11.5" << endl; cout << " 8: Programming Exercise 11.8" << endl; cout << "10: Programming Exercise 11.10" << endl; cout << "14: Programming Exercise 11.14" << endl; cout << "15: Programming Exercise 11.15" << endl; cout << "other: Exit" << endl; cout << "==============================" << endl; cout << "Enter an exercise: "; char exercise[2]; cin >> exercise; cout << endl; switch (atoi(exercise)) { case 1: Prog11_1(); break; case 3: Prog11_3(); break; case 4: Prog11_4(); break; case 5: Prog11_5(); break; case 8: Prog11_8(); break; case 10: Prog11_10(); break; case 14: Prog11_14(); break; case 15: Prog11_15(); break; default: exit(0); } cout << endl; system("pause"); cin.clear(); } return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!