Diferenças
Aqui você vê as diferenças entre duas revisões dessa página.
— |
user:1577657:progest20102_aval01_correcao [2016/06/28 00:25] (atual) |
||
---|---|---|---|
Linha 1: | Linha 1: | ||
+ | ==== AVALIAÇÃO 01 - CORREÇÃO ==== | ||
+ | |||
+ | **Questão 1** | ||
+ | <code> | ||
+ | #include <iostream> | ||
+ | #include <stdlib.h> | ||
+ | #include <cctype> | ||
+ | using namespace std; | ||
+ | |||
+ | main(){ | ||
+ | | ||
+ | int A[] = {1,1,2,3,5,8,13,21,34}; | ||
+ | | ||
+ | //exibe soma | ||
+ | //a) Exibir a soma dos elementos | ||
+ | int soma = 0; | ||
+ | for(int i=0; i<9; i++){ | ||
+ | soma += A[i]; | ||
+ | } | ||
+ | cout << "soma = " << soma << endl; | ||
+ | | ||
+ | //b) Exibir apenas os números pares do vetor | ||
+ | cout << "Pares:" <<endl; | ||
+ | for(int i=0; i<9; i++){ | ||
+ | if(A[i]%2==0){ | ||
+ | cout << A[i] << endl; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | //c) Exibir apenas os numeros primos do vetor valor: 25 | ||
+ | //{1,1,2,3,5,8,13,21,34}; | ||
+ | cout << "Primos:" <<endl; | ||
+ | int elemAtual; | ||
+ | for(int i=0; i<9; i++){ | ||
+ | elemAtual = A[i]; | ||
+ | | ||
+ | if(elemAtual == 1){ | ||
+ | cout << elemAtual << endl; | ||
+ | continue; | ||
+ | } | ||
+ | | ||
+ | int ehPrimo = 1; //true | ||
+ | for(int j=2; j<elemAtual; j++){ | ||
+ | if ((elemAtual%j) == 0){ | ||
+ | ehPrimo = 0; //false | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | if (ehPrimo) { | ||
+ | cout << A[i] << endl; | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | | ||
+ | system("pause"); | ||
+ | | ||
+ | } | ||
+ | |||
+ | </code> | ||
+ | |||
+ | |||
+ | **Questão 2** | ||
+ | <code> | ||
+ | #include <iostream> | ||
+ | #include <stdlib.h> | ||
+ | #include <cctype> | ||
+ | using namespace std; | ||
+ | |||
+ | main(){ | ||
+ | | ||
+ | int A[3][3] = { | ||
+ | {4, 2, 6}, | ||
+ | {7, 6, 2}, | ||
+ | {3, 9, 8}, | ||
+ | }; | ||
+ | | ||
+ | int B[3][3] = { | ||
+ | {6, 4, 2}, | ||
+ | {3, 5, 9}, | ||
+ | {11, 2, 7}, | ||
+ | }; | ||
+ | |||
+ | | ||
+ | //a) Exibir os elementos da diagonal principal da matriz A | ||
+ | cout << "Diagonal principal:" << endl; | ||
+ | for (int i=0; i<3; i++){ | ||
+ | cout << A[i][i]<< endl; | ||
+ | } | ||
+ | cout << " ou " << endl; | ||
+ | for (int i=0; i<3; i++){ | ||
+ | for (int j=0; j<3; j++){ | ||
+ | if(i==j){ | ||
+ | cout << A[i][j]<< endl; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | //b) Exibir a soma de todos os elementos da matriz B | ||
+ | cout << "Exibir a soma de todos os elementos da matriz B:" << endl; | ||
+ | int soma = 0; | ||
+ | for (int i=0; i<3; i++){ | ||
+ | for (int j=0; j<3; j++){ | ||
+ | soma += B[i][j]; | ||
+ | } | ||
+ | } | ||
+ | cout << " soma = "<< soma << endl; | ||
+ | |||
+ | |||
+ | //c) Exibir a soma das matrizes, ou seja: A+B. valor: 35 | ||
+ | cout << "Exibir a soma das matrizes, ou seja: A+B:" << endl; | ||
+ | int C[3][3]; | ||
+ | for (int i=0; i<3; i++){ | ||
+ | for (int j=0; j<3; j++){ | ||
+ | C[i][j] = A[i][j] + B[i][j]; | ||
+ | cout << C[i][j] << " " ; | ||
+ | } | ||
+ | cout << endl ; | ||
+ | } | ||
+ | | ||
+ | system("pause"); | ||
+ | | ||
+ | } | ||
+ | |||
+ | </code> | ||
+ | |||
+ | **Questão 3** | ||
+ | <code> | ||
+ | #include <iostream> | ||
+ | #include <stdlib.h> | ||
+ | #include <cctype> | ||
+ | using namespace std; | ||
+ | |||
+ | main(){ | ||
+ | | ||
+ | string s = ""; | ||
+ | //cout << "Digite um texto:" << endl; | ||
+ | //getline(cin, s); | ||
+ | s = "Hoje vou tirar cem na prova"; | ||
+ | s = " "; | ||
+ | cout << s << endl; | ||
+ | | ||
+ | | ||
+ | //a) O tamanho da string | ||
+ | cout << "O tamanho da string: " << s.size() <<endl; | ||
+ | | ||
+ | //b) A quantidade de palavras da string | ||
+ | //c) As palavras da string que possuem menos de 4 caracteres | ||
+ | |||
+ | int contaPalavras = 0; | ||
+ | int procurandoEspaco = 0; | ||
+ | int indiceInicialPalavra = 0; | ||
+ | int indiceFinalPalavra = 0; | ||
+ | |||
+ | string palavrasComMenos4Caracteres=""; | ||
+ | //adiciona um espaço ao final para não | ||
+ | //dar problema qdo for contar palavras com menos de 4 caracteres | ||
+ | s += " "; | ||
+ | |||
+ | for (int i=0; i<s.size(); i++){ | ||
+ | if( (s[i]!=' ') && (procurandoEspaco==0) ){ | ||
+ | contaPalavras++; | ||
+ | procurandoEspaco=1; | ||
+ | indiceInicialPalavra=i; | ||
+ | } | ||
+ | else if( (s[i]==' ') && (procurandoEspaco!=0) ){ | ||
+ | procurandoEspaco=0; | ||
+ | indiceFinalPalavra=i-1; | ||
+ | int qtdCaracteresDaPalavra = indiceFinalPalavra - indiceInicialPalavra + 1; | ||
+ | if(qtdCaracteresDaPalavra < 4){ | ||
+ | palavrasComMenos4Caracteres += | ||
+ | s.substr(indiceInicialPalavra, qtdCaracteresDaPalavra)+","; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | cout << "Qdte Palavras = " << contaPalavras << endl; | ||
+ | cout << "Palavras com menos de 4 caracteres= " << palavrasComMenos4Caracteres<< endl; | ||
+ | | ||
+ | |||
+ | |||
+ | //d) Os caracteres pares em maiúsculo e os caracteres ímpares em minúsculo. Considerar o índice | ||
+ | // 0 (zero) como sendo par. | ||
+ | for (int i=0; i<s.size(); i++){ | ||
+ | if(i%2==0){ | ||
+ | s[i]=toupper(s[i]); | ||
+ | } else { | ||
+ | s[i]=tolower(s[i]); | ||
+ | } | ||
+ | } | ||
+ | cout << s << endl; | ||
+ | | ||
+ | system("pause"); | ||
+ | | ||
+ | } | ||
+ | |||
+ | </code> | ||