You are on page 1of 12

/*Exercise 5.

5*/ int main() { int x, left, inputed, sum=0; cout << "Enter the amount of numbers to be entered: "; cin >> left; for(x=1; x<=left; x++) { cout << "\nEnter the number: "; cin >> inputed; sum+=inputed; } cout << "The sum is: " << sum << endl; getchar(); getchar(); } /*Exercise 5.6*/ int main() { int x, inputed, sum=0; int eh=130; cout << "Digite um numero (9999 para sair): "; cin >> inputed; for(x=1;; x++) { sum+=inputed; cout << "Digite um numero (9999 para sair): "; cin >> inputed; if(inputed==9999) break; } cout << "A media " << static_cast<char>(eh) << ": " << sum/x; getchar(); getchar(); } /*Exercise 5.8*/ int main() { int left, min; long signed int enter; cout << "Quantos? "; cin >> left; if(left>0) { cout << "digite um numero: "; cin >> enter; min=enter; for(int i=1; i<left; i++) { if(min>enter) min=enter; cout << "Digite um numero: "; cin >> enter; } if(min>enter)

min=enter; cout << "O menor foi: " << min; } getchar(); getchar(); } /*Exercise 5.9*/ int main() { int prod=1; for(int i=1; i<=15; i+=2) { prod*=i; } cout << prod; getchar(); } /*Exercise 5.10*/ int fatorial(int n) { return (n==0)? 1: fatorial(n-1)*n; } int main() { for(int i=1; i<=5; i++) { cout << fatorial(i) << '\t'; } cout << endl; getchar(); } //Well, is impossible to surpass the limit for int, even for long unsigned int t he value of 20! //is to big to the function return a logic and acceptable value... /*Exercise 5.11*/ int main() { double amount; double principal=1000; double rate=.05; cout << "Year" << setw(21) << "Amount on deposit" << endl; for(int i=0; i<=5; rate+=.01, i++) { cout << "\t***RATE = " << rate << "***\t" << endl; for(int year=1; year<=10; ++year) { amount = principal*pow(1.0+rate, year); cout << setw(4) << year << setw(21) << amount << endl; } cout << endl; } getchar();

//cansei

} /*Exercise 5.12*/ //a int main() { for(int i=0; i<=10; i++) { for(int j=i; j>0; j--) cout << '*'; cout << ' ' << i << endl; } getchar(); } //b int main() { for(int i=10; i>=0; i--) { for(int j=i; j>0; j--) cout << '*'; cout << ' ' << i << endl; } getchar(); } //c int main() { for(int i=10; i>=1; i--) { for(int j=1; j<=10; j++) { if(j<=10-i) cout << ' '; else cout << '*'; } cout << setw(2) << i << endl; } getchar(); } //d int main() { for(int i=9; i>=0; i--) { for(int j=10; j>=1; j--) { if(j>10-i) cout << ' '; else cout << '*'; } cout << 10-i << endl; } getchar(); }

/*Exercise 5.13*/ int main() { int enter; for(int i=0; i<5; i++) { cout << "Digite um numero: "; cin >> enter; if(enter>=1 && enter<=30) for(int j=0; j<enter; j++) cout << '*'; else { cout << "Erro, fora do limite..."; --i; } cout << endl; } getchar(); } /*Exercise 5.14*/ int main() { int product; int quantity; int product1, product2, product3, product4, product5; product1=product2=product3=product4=product5=0; double sum=0.0; char ce=135; do { cout << "Digite o numero do produto seguido da quantidade (20 pa ra sair): "; cin >> product >> quantity; if(quantity<1) cout << "Quantidade invalida..."; else switch(product) { case 1 : sum+=2.98*quantity; product1+=quantity; break; case 2 : sum+=4.50*quantity; product2+=quantity; break; case 3 : sum+=9.98*quantity; product3+=quantity; break; case 4 : sum+=4.49*quantity; product4+=quantity; break; case 5 : sum+=6.87*quantity; product5+=quantity; break; case 20:

break; default: cout << "Numero invalido, digite outro numero!! "; break; } cout << endl; } while(product!=20); cout << "Produtos" << setw(15) << "Quantidade" << setw(15) << "Pre" <<ce <<"o" << endl; if(product1!=0) cout << "Produto1" << setw(15) << product1 << setw(15) << "$2.98 " << endl; if(product2!=0) cout << "Produto2" << setw(15) << product2 << setw(15) << "$4.50 " << endl; if(product3!=0) cout << "Produto3" << setw(15) << product3 << setw(15) << "$9.98 " << endl; if(product4!=0) cout << "Produto4" << setw(15) << product4 << setw(15) << "$4.49 " << endl; if(product5!=0) cout << "Produto5" << setw(15) << product5 << setw(15) << "$6.87 " << endl; cout << "Total: " << sum; getchar(); getchar(); } /*Exercise 5.15*/ //Alteraes feitas //Adicionado double gradePointAverage(); /*Exercise 5.16*/ oluo melhor #include <iostream> #include <iomanip> #include <cmath> using namespace std; //Fica como resposta, mesmo provavelmente tendo alguma s

int main() { int pennies; int dollars; int principal=100000; int amount; double rate=.05; for(int i=1; i<=12; i++) { amount=principal*pow(1.0+rate, static_cast<double>(i))-principal ; dollars=((principal*pow(1.0+rate, static_cast<double>(i)))/100); pennies=amount%100; if(pennies<=9) cout << "US$" << dollars << ".0" << pennies<< endl; else cout << "US$" << dollars << '.' << pennies << endl; }

getchar(); } /*Exercise 5.17*/ /*a) 1 /*b) 0 /*c) 1 /*d) 0 /*e) 1 /*f) 0 /*g) 0 /*h) 1 /*i) 0 */ /*Exercise 5.18*/ #include <iostream> #include <string> #include <iomanip> using namespace std; int main() { string teste; char saida[10]; cout << "Decimal\t" << "\tHexadecimal\t" << "\tOctal\t" << "\tBinario" < < endl; for(int i=1; i<257; i++) { int res=i; int j=0; while(res>0) { saida[j++]=(res%2)+'0'; res/=2; } saida[j]='\0'; teste=saida; teste=string(teste.rbegin(), teste.rend()); cout << setw(3) << dec << i << "\t" << setw(15) << hex << i << " \t\t" << setw(15) << oct << i << "\t\t" << setw(8) << teste << endl; getchar(); } } /*Exercise 5.19*/ int main() { double pi=0.0; int i, j; cout << fixed << setprecision(12); for(i=0, j=1; j<1000; i++, j+=2) { pi+=(4.0*(pow(-1, static_cast<double>(i))))/j; if(i%20==0) getchar(); else cout << pi << endl; }

getchar(); } /*Exercicio 5.20*/ #include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { int side1, side2, hipotenusa; int i=0; int teste=0; for(i=0, side1=1; side1<=500; i++, side1++) { side2=side1; while(side2<=500) { hipotenusa=side1; while(hipotenusa<=500) { if(((side1*side1)+(side2*side2))==(hipotenusa*hi potenusa)) { cout << "pronto\n" << "side1: " << side1 << "\nside2: " << side2 << "\nhipotenusa: " << hipotenusa << endl; break; } else { hipotenusa++; i++; } } i++; side2++; } if(i%50==0) { getchar(); } } cout << "Rodou essa quantidade, talvez com duplicados.. : " << i << endl ; getchar(); } /*Exercise 5.21*/ #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { double managersWorkers=0.0, hourlyWorkers=0.0, comissionWorkers=0.0, pie ceWorkers=0.0; int typeOfEmployee; double salary;

int int int int int int int int int int int do {

quantity; hours; pieces; quantityOf1=0; quantityOf2=0; quantityOf3=0; quantityOf4=0; in1=0; in2=0; in3=0; in4=0;

cout << "Digite o genero de empregado que deseja ver: " << endl << "1 - Gerentes" << endl << "2 - Trabalhadores por hora" << endl << "3 - Trabalhadores por comisso" << endl << "4 - Trabalhadores por pea" << endl; cin >> typeOfEmployee; switch(typeOfEmployee) { case 1 :cout << "Digite quanto este empregado ganha por semana: "; cin >> salary; managersWorkers=salary; cout << "Quantos gerentes? "; cin >> quantity; quantityOf1+=quantity; in1=1; break; case 2 :cout << "Digite quanto este empregado ganha por hora: "; cin >> salary; cout << "Quanta horas eles trabalharam? "; cin > > hours; cout << "Quantos empregados? "; cin >> quantity; quantityOf2+=quantity; if(hours<=40) { hourlyWorkers=salary*hours; } else { hours-=40; hourlyWorkers=(salary*40)+((salary*1.5)* hours); } in2=1; break; case 3 :cout << "Digite quanto este empregado vendeu nesta seman a: "; cin >> salary; cout << "Quantos empregados? "; cin >> quantity; quantityOf3+=quantity; comissionWorkers=250.0+(salary*0.057); break; case 4 :cout << "Digite quanto este empregado ganha por pea produ zida: "; cin >> salary; cout << "Quantos peas eles produziram? "; cin >> pieces; cout << "Quantos empregados? "; cin >> quantity; quantityOf4+=quantity; pieceWorkers=salary*pieces; in3=1; break; case 20: break; default:cout << "Digitou algo errado ai.. mal.."; in4=1; break; }

} while(typeOfEmployee!=20); cout << "Empregados" << setw(15) << "Salarios" << setw(15) << "Quantidad e de Empregados" << setw(15) << "Total gasto com eles" << endl; if(in1!=0) { cout << "Gerente" << setw(15) << managersWorkers << setw(18) << quantityOf1 << setw(21) << managersWorkers*quantityOf1 << endl; } if(in2!=0) { cout << "Trabalhadores de hora" << setw(15) << hourlyWorkers << setw(18) << quantityOf2 << setw(21) << hourlyWorkers*quantityOf2 << endl; } if(in3!=0) { cout << "Trabalhadores de comisso" << setw(15) << comissionWorker s << setw(18) << quantityOf3 << setw(21) << comissionWorkers*quantityOf3 << endl ; } if(in4!=0) { cout << "Trabalhadores de peas" << setw(15) << pieceWorkers << se tw(18) << quantityOf4 << setw(21) << pieceWorkers*quantityOf4 << endl; } if(in1!=0 || in2!=0 || in3!=0 || in4!=0) { cout << "\nTotal gasto com os empregados: " << (managersWorkers* quantityOf1)+(hourlyWorkers*quantityOf2)+(comissionWorkers*quantityOf3)+(pieceWo rkers*quantityOf4) << endl; } else cout << "\t\t****NADA PARA MOSTRAR.. SORRYE****" << endl; getchar(); getchar(); } /*Exercise 5.22*/ #include <iostream> using namespace std; int main() { int x=1, y=8, a=2, b=2, g=3, i=3, j=4; cout << boolalpha; cout << ((!(x <5) && !(y>=7))==(!((x<5) ||(y>=7)))) cout << ((!(a==b) || !(g!=5))==(!((a==b)&&(g!=5)))) cout << ((!((x<=8) && (y>4))==(!(x<=5) || !(y>4)))) cout << ((!((i>4) || (j<=6))==(!(i>4) && !(j<=6)))) getchar(); } /*Exercise 5.23*/ int main() { int i, j, l, k; for(i=9, k=1, l=5; i>0; i--) { for(j=1; j<=9; j++)

<< << << <<

endl; endl; endl; endl;

{ if((j==l) || (j>l-k && j<l+k)) cout << '*'; else cout << ' '; } if(i>5) { k++; } else { k--; } cout << endl; } getchar(); } /*Exercise 5.24*/ #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { int i, j, l, k, m; do { cout << "Digite o tamanho do diamante (30 para sair): "; cin >> i; if(i==30) break; else if(i%2!=0) for(k=1, l=(i+1)/2, m=i; i>0; i--) { for(j=1; j<=m; j++) { if((j==l) || (j>l-k && j<l+k)) cout << '*'; else cout << ' '; } if(i>l) { k++; } else { k--; } cout << endl; } else cout << "So numeros impares so validos" << endl; cout << endl; } while(i!=30); getchar();

} /*Exercicio 5.25*/ /*To not use the break statement can put a "break test" in the test condition /*To don't need the continue put a if(condition) null statement */ /*Exercicio 5.26*/ /*Apenas imprime "quadrados" de '*', 4x3 e 5 quadrados */ /*Exercicio 5.27*/ //Usando a Fig 5.14 como exemplo int main() { for(int count=1; count<=10; count++) { if(count!=5) cout << count << ' '; } cout << endl; } /*Exercicio 5.28*/ #include <iostream> using namespace std; int main() { int day; string date; for(day=1; day<=12; day++) { switch(day) { case 1 : date="first"; case 2 : date="second"; case 3 : date="third"; case 4 : date="fourth"; case 5 : date="fifth"; case 6 : date="sixth"; case 7 : date="seventh"; case 8 : date="eighth"; case 9 : date="ninth"; case 10: date="tenth"; case 11: date="eleventh"; case 12: date="twelfth"; } cout << "On the " << day << " day of Christmas" << endl; cout << "my true love sent to me:" << endl; switch(day) { case 12: cout << "12 Drummers Drumming" << endl; case 11: cout << "Eleven Pipers Piping" << endl; case 10: cout << "Ten Lords a Leaping" << endl; case 9 : cout << "Nine Ladies Dancing" << endl; case 8 : cout << "Eight Maids a Milking" << endl; case 7 : cout << "Seven Swans a Swimming" << endl; case 6 : cout << "Six Geese a Laying" << endl;

case case case case case } getchar(); } getchar(); }

5 4 3 2 1

: : : : :

cout cout cout cout cout

<< << << << <<

"Five Golden Rings" << endl; "Four Calling Birds" << endl; "Three French Hens" << endl; "Two Turtle Doves " << endl << "and "; "A Partridge in a Pear Tree" << endl;

/*Exercicio 5.29*/ int main() { double principal=24.00; double rate=0.05; double amount; for(int i=0; i<=5; i++) { cout << fixed; amount=principal*(pow(1.0+rate, static_cast<double> (384))); cout << "Rate: " << rate << "\tAmount: " << amount << endl; rate+=0.01; } getchar(); }

You might also like