C++primer plus 6th edition 4.13 Programming Exercise answers

Source: Internet
Author: User


1. Answer:
#include <iostream>
#include <string>


int main ()
{
using namespace Std;
char* fname = new CHAR[10];
char* lname = new Char[6];
char grade;
int age;
cout<< "What is your first name?";
Cin.getline (fname,10);
cout<< "What's your last name?";
Cin.getline (lname,6);
cout<< "What's letter grade does you deserve?";
cin>>grade;
cout<< "What's your age?";
cin>>age;
cout << "Name:" <<lname<< "," <<fname<<endl;
cout << "Grade:" <<char (grade+1) <<endl;
cout << "Age:" <<age;


delete [] fname;
delete [] lname;
return 0;

}

2. Answer:



#include <iostream>
#include <string>


int main ()
{
using namespace Std;
String name;
String dessert;


cout << "Enter your name:\n";
Getline (Cin,name);
cout<< "Enter your favorite dessert:\n";
Getline (Cin,dessert);
cout<< "I have some delicious" << dessert;
cout << "For You," << name << ". \ n";
return 0;
}

3. Answer:



#include <iostream>
#include <cstring>


int main ()
{
using namespace Std;
char* fname = new char [15];
char* lname = new char [8];


cout << "Enter Your First name:";
Cin.getline (fname,5);
cout<< "Enter Your last Name:";
Cin.getline (lname,8);
strcat (fname, ",");
strcat (Fname,lname);
cout << "Here's" the information in a single string: "
<<fname;
delete [] fname;
delete [] lname;
return 0;
}

4. Answer:

#include <iostream>
#include <string>


int main ()
{
using namespace Std;
String fname;
String lname;


cout << "Enter Your First name:";
Getline (Cin,fname);
cout<< "Enter Your last Name:";
Getline (Cin,lname);
FName + = ",";
FName + = lname;
cout << "Here's" the information in a single string: "
<<fname;


return 0;
}

5. Answer

#include <iostream>
#include <string>


using namespace Std;


struct CandyBar
{
String brand;
float weight;
int Carlo;


};


int main ()
{
CandyBar snack = {"Mocha Munch", 2.3,350};
cout<< "brand =" <<snack.brand<<endl
<< "weight =" <<snack.weight<<endl
<< "Carlo =" <<snack.carlo<<endl;
return 0;

}

6. Answer

#include <iostream>
#include <string>


using namespace Std


struct candybar
{
string brand;
float weight;
int Carlo;


};


Int main ()
{
candybar Snack[3] = {"Mocha Munch", 2.3,350},{" Mocha Middle ", 2.0,300},{" Mocha Little ", 3.2,600}};
for (int i=0;i<3;i++)
cout << "Snack[i].brand =" <<snack[i].brand<<endl
<< " Snack[i].weight = "<<snack[i].weight<<endl
<<" Snack[i " . Carlo = "<<snack[i].carlo<<endl;
return 0;

}

7. Answer

#include <iostream>
#include <string>


using namespace Std;


struct PIZAINFO
{
String Coname;
int diameter;
float weight;


};


int main ()
{

Pizainfo P1;
cout << "Enter the infomation:\n";
cout << "corpotion name:";
Getline (Cin,p1.coname);
cout << "Piza diameter:";
cin>>p1.diameter;
cout << "Piza weight:";
CIN >>p1.weight;
cout<< "\ n" << "corpotion Name:" <<p1.coname<<endl
<< "Piza Diameter:" <<p1.diameter<<endl
<< "Piza Weight:" <<p1.weight;
}

8. Answer

#include <iostream>
#include <string>


using namespace Std;


struct PIZAINFO
{
String Coname;
int diameter;
float weight;


};


int main ()
{

pizainfo* PS = new Pizainfo;


cout << "Enter the infomation:\n";
cout << "Piza diameter:";
cin>>ps->diameter;
cout << "corpotion name:";
cin>>ps->coname;


cout << "Piza weight:";
CIN >>ps->weight;
cout<< "\ n" << "corpotion Name:" <<ps->coname<<endl
<< "Piza Diameter:" <<ps->diameter<<endl
<< "Piza Weight:" <<ps->weight;
Delete PS;


}

9. Answer

#include <iostream>
#include <string>


using namespace Std;

struct CandyBar
{
String brand;
float weight;
int Carlo;
};

int main ()
{
candybar* snack = new Candybar[3];
Snack[0].brand = "Mocha Munch";
Snack[0].carlo= 2.3;
Snack[0].weight = 350;
Snack[1].brand = "Mocha Middle";
Snack[1].carlo= 2.0;
Snack[1].weight = 300;
Snack[2].brand = "Mocha Little";
Snack[2].carlo= 3.2;
Snack[2].weight = 600;


for (int i=0;i<3;i++)
cout<< "Snack[i].brand =" <<snack[i].brand<<endl
<< "snack[i].weight =" <<snack[i].weight<<endl
<< "Snack[i].carlo =" <<snack[i].carlo<<endl;


delete [] snack;


return 0;

}

10. Answer

#include <iostream>
#include <array>


using namespace Std;


int main ()
{
Array<float,3> scores;
cout<< "Please enter three times score of 40-meters-run: \ n";
for (int i =0;i<3;i++)
{
cout<< "the" <<i<< "Time Score:";
cin>>scores[i];
}

cout << "The average of scores is:"
<< (Scores[0]+scores[1]+scores[2])/3;


return 0;


}

C++primer plus 6th edition 4.13 Programming Exercise answers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.