Chapter 7 (Arrays and Strings) - Exercises

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Write a function called reversit() that reverses a C-string (an array of char). Use a for loop that swaps the first and last characters, then the second and next-to-last characters, and so on. The string should be passed to reversit() as an argument. Write a program to exercise reversit(). The program should get a string from the user, call reversit(), and print out the result. Use an input method that allows embedded blanks. Test the program with Napoleon's famous phrase, "Able was I ere I saw Elba."

// ex7_1.cpp // reverses a C-string #include <iostream> #include <cstring> //for strlen() using namespace std; int main() { void reversit( char[] ); //prototype const int MAX = 80; //array size char str[MAX]; //string cout << "\nEnter a string: "; //get string from user cin.get(str, MAX); reversit(str); //reverse the string cout << "Reversed string is: "; //display it cout << str << endl; return 0; } //-------------------------------------------------------------- //reversit() //function to reverse a string passed to it as an argument void reversit( char s[] ) { int len = strlen(s); //find length of string for(int j = 0; j < len/2; j++) //swap each character { // in first half char temp = s[j]; // with character s[j] = s[len-j-1]; // in second half s[len-j-1] = temp; } } // reversit() // function to reverse a string passed to it as an argument void reversit( char s[] ) { int len = strlen(s); // find length of string for(int j = 0; j < len/2; j++) // swap each character { // in first half char temp = s[j]; // with character s[j] = s[len-j-1]; // in second half s[len-j-1] = temp; } }

Create a class called employee that contains a name (an object of class string) and an employee number (type long). Include a member function called getdata() to get data from the user for insertion into the object, and another function called putdata() to display the data. Assume the name has no embedded blanks. Write a main() program to exercise this class. It should create an array of type employee, and then invite the user to input data for up to 100 employees. Finally, it should print out the data for all the employees.

// ex7_2.cpp // employee object uses a string as data #include <iostream> #include <string> using namespace std; //////////////////////////////////////////////////////////////// class employee { private: string name; long number; public: void getdata() //get data from user { cout << "\nEnter name: "; cin >> name; cout << "Enter number: "; cin >> number; } void putdata() //display data { cout << "\n Name: " << name; cout << "\n Number: " << number; } }; //////////////////////////////////////////////////////////////// int main() { employee emparr[100]; //an array of employees int n = 0; //how many employees char ch; //user response do { //get data from user cout << "\nEnter data for employee number " << n+1; emparr[n++].getdata(); cout << "Enter another (y/n)? "; cin >> ch; } while( ch != 'n' ); for(int j=0; j<n; j++) //display data in array { cout << "\nEmployee number " << j+1; emparr[j].putdata(); } cout << endl; return 0; }

Write a program that calculates the average of up to 100 English distances input by the user. Create an array of objects of the Distance class, as in the ENGLARAY example in this chapter. To calculate the average, you can borrow the add_dist() member function from the ENGLCON example in Chapter 6. You'll also need a member function that divides a Distance value by an integer. Here's one possibility: void Distance::div_dist(Distance d2, int divisor) { float fltfeet = d2.feet + d2.inches/12.0; fltfeet /= divisor; feet = int(fltfeet); inches = (fltfeet-feet) * 12.0; }

// ex7_3.cpp // averages an array of Distance objects input by user #include <iostream> using namespace std; //////////////////////////////////////////////////////////////// class Distance // English Distance class { private: int feet; float inches; public: Distance() //constructor (no args) { feet = 0; inches = 0; } Distance(int ft, float in) //constructor (two args) { feet = ft; inches = in; } void getdist() //get length from user { cout << "\nEnter feet: "; cin >> feet; cout << "Enter inches: "; cin >> inches; } void showdist() //display distance { cout << feet << "\'-" << inches << '\"'; } void add_dist( Distance, Distance ); //declarations void div_dist( Distance, int ); }; //-------------------------------------------------------------- //add Distances d2 and d3 void Distance::add_dist(Distance d2, Distance d3) { inches = d2.inches + d3.inches; //add the inches feet = 0; //(for possible carry) if(inches >= 12.0) //if total exceeds 12.0, { //then decrease inches inches -= 12.0; //by 12.0 and feet++; //increase feet } //by 1 feet += d2.feet + d3.feet; //add the feet } //-------------------------------------------------------------- //divide Distance by int void Distance::div_dist(Distance d2, int divisor) { float fltfeet = d2.feet + d2.inches/12.0; //convert to float fltfeet /= divisor; //do division feet = int(fltfeet); //get feet part inches = (fltfeet-feet) * 12.0; //get inches part } //////////////////////////////////////////////////////////////// int main() { Distance distarr[100]; //array of 100 Distances Distance total(0, 0.0), average; //other Distances int count = 0; //counts Distances input char ch; //user response character do { cout << "\nEnter a Distance"; //get Distances distarr[count++].getdist(); //from user, put cout << "\nDo another (y/n)? "; //in array cin >> ch; }while( ch != 'n' ); for(int j=0; j<count; j++) //add all Distances total.add_dist( total, distarr[j] ); //to total average.div_dist( total, count ); //divide by number cout << "\nThe average is: "; //display average average.showdist(); cout << endl; return 0; }


Ensembles d'études connexes

MC 1100 medical front office procedure quiz revie

View Set

N210/220 Assignment 3: Health & Physical Assessment

View Set

General Mathematics - Number Systems and Sets

View Set

NovSpA - DLU1 - Tpc02A - Cuando nací: Los 12 meses del año

View Set

Vocabulary Unit 4 Argumentative and Persuasive Text

View Set

Loops(for loop, while loop, do while loop) - Basic Java Tutorials For Selenium WebDriver

View Set

Module 5: Cancer and the Immune System

View Set