p259.4
1#include <iostream>2#include <cstdlib>3#include <cctype>4 using namespacestd;5 structstringy{6 Char*str;7 intCT;8 };9 void Set(Stringy &,Const Char*);Ten voidShowConstStringy &intTimes =1); One voidShowConst Char*,intTimes =1); A - intMain () { - stringy Beany; the CharTesting[] ="reality isn ' t what it used to be."; - Set(Beany, testing); - Show (Beany); -Show (Beany,2); + DeleteBeany.str; -testing[0] ='D'; +testing[1] ='u'; A Show (testing); atShow (Testing,3); -Show"done!"); -System"Pause"); - return 0; - } - in void Set(Stringy &beany,Const Char*testing) { - intnum =strlen (testing); toBeany.str =New Char[Num +1]; + strcpy (beany.str, testing); -Beany.ct =num; the } * $ voidShowConstStringy &beany,intTimes ) {Panax Notoginseng for(inti =0; I < times; i++) -cout << beany.str <<Endl the<< Beany.ct <<Endl; + } A the voidShowConst Char* Testing,intTimes ) { + for(inti =0; I < times; i++) -cout << Testing <<Endl; $}
p296.4
1 header File2 namespaceSALES3 {4 Const intQuarters =4;5 structsales{6 DoubleSales[quarters];7 Doubleaverage;8 DoubleMax;9 Doublemin;Ten }; One voidSetsales (Sales &s,Const DoubleAr[],intn); A voidSetsales (Sales &s); - voidShowsales (Constsales&s); - } the - Method -#include <iostream> -#include"Sales.h" + using namespacestd; - + voidSales::setsales (Sales &s,Const DoubleAr[],intN) { A intNum, I, sum=0; at if(N <=quarters) -num =N; - Elsenum =quarters; - for(i =0; i < num; i++){ -S.sales[i] =Ar[i]; -Sum + =S.sales[i]; in } -S.average = SUM/num; toS.max = s.sales[0]; +S.min = s.sales[0]; - for(i =1; i < num; i++){ the if(S.max <S.sales[i]) *S.max =S.sales[i]; $ if(s.min>S.sales[i])Panax NotoginsengS.min =S.sales[i]; - } the } + A voidSales::setsales (Sales &s) { thecout <<"enter double into an array\n"; + inti; - Doublesum =0; $ for(i =0; I < quarters; i++){ $CIN >>S.sales[i]; -Sum + =S.sales[i]; - } theS.average = SUM/quarters; -S.max = s.sales[0];WuyiS.min = s.sales[0]; the for(i =1; I < quarters; i++){ - if(S.max <S.sales[i]) WuS.max =S.sales[i]; - if(s.min>S.sales[i]) AboutS.min =S.sales[i]; $ } - } - - voidSales::showsales (ConstSales &s) { A for(inti =0; I < quarters; i++) +cout << S.sales[i] <<" "; thecout <<"Average is"<< s.average << Endl << - "Max is"<< S.max << Endl << $ "min is"<< S.min <<Endl; the } the the Drive the#include <iostream> -#include <cstdlib> in#include <string> the#include <cstring> the#include"Sales.h" About using namespacestd; the the intMain () { thecout <<"Enter a number you want to fill the array\n"; + intn, I; -CIN >>N; the Double*ar =New Double[n+1];Bayicout <<"Enter an double array\n"; the for(i =0; I < n; i++) theCIN >>Ar[i]; - sales::sales Ex1, ex2; - sales::setsales (Ex1, AR, n); the sales::setsales (EX2); the sales::showsales (EX1); the sales::showsales (EX2); the Delete[]ar; -System"Pause"); the return 0; the}
C + + Primer plus Exercise Answer (2)