Buscar

atividade1

Prévia do material em texto

MINISTÉRIO DA EDUCAÇÃO 
UNIVERSIDADE FEDERAL DOS VALES DO JEQUITINHONHA E MUCURI 
CAMPUS AVANÇADO DO MUCURI – TEÓFILO OTONI - MG 
BACHARELADO EM CIÊNCIA E TECNOLOGIA 
 
 
Aluno: ___Brenda Ketilyn Lages de Paula__ _________________________ Algoritmos 
 
 
 
Questão 1) 
 
a) Quais as 3 informações mais importantes de uma função 
 
 A função se caracteriza por deixar o programa organizado facilitando a leitura de 
terceiros, sendo assim, facilita para achar erros e impede que o desenvolvedor tenha que 
repetir o código diversas vezes pois, pode-se dividir o programa em diversas pequenas 
funções. 
 
 
b) Dê exemplo de 3 funções para manipulação de string 
 
Strcmp: devolve o valor, se as duas strings forem iguais, logo, a resposta da função será 
o valor 0, caso contrário a resposta será um valor positivo ou negativo. 
Strcpy: copia o conteúdo de uma string x para a string y, ou seja, é útil quando precisa 
realizar atribuições entre strings. 
 Strlen: devolve o número de caracteres armazenados na string. 
 
c) Defina: 
a) #define: serve para definir constantes no programa. 
b) rand():usada junto com srand (time(NULL)) para criar valores aleatórios. 
c) break: serve para quebrar a execução de um comando (switch) ou interromper a 
execução de qualquer loop (for, while, do while). 
 
Questão 2) Escreva as estruturas dos comandos abaixo: 
 
a) Comando do while: do{ 
 declaração; 
 } while (condição); 
b) Ternário: Operador- Condição? Verdadeiro: Falso. 
c) Switch: switch (Variavel) { 
 Case constante n: 
 declaração n; 
 break; 
 default: 
 declaração_default; 
 } 
d) Função: Tipo de retorno nome da função (Declaração de Parâmetros) { 
 Corpo da Função 
 } 
 
 
MINISTÉRIO DA EDUCAÇÃO 
UNIVERSIDADE FEDERAL DOS VALES DO JEQUITINHONHA E MUCURI 
CAMPUS AVANÇADO DO MUCURI – TEÓFILO OTONI - MG 
BACHARELADO EM CIÊNCIA E TECNOLOGIA 
 
 
Aluno: ___Brenda Ketilyn Lages de Paula__ _________________________ Algoritmos 
 
 
 
Questão 3) Que informação será impressa após a execução do código abaixo. 
 
 
main(){ 
 int i=0, j, k; 
 do{ 
 k=1; 
 while( k < 5){ 
 for(j=0; j < 10;j++){ 
 if (j == 2) 
 break; 
 printf("\nI K J = %d %d %d",i,k,j); 
 } 
 ++k; 
 } 
 i++; 
 }while(i < 3); 
} 
 
Os valores do ikj serão impressos, logo temos o k=1 então ele entrará no for onde i=0, 
j=0 e k=1. Quando o j entrar no if, ele irá valer 1, então será feito a comparação, mas se 
1 não é igual a 2, ele retornará para o for, onde k= 2, então ele vai sair do for e retornará 
ao while, pois k vale 2 e j=1 e i=0, e assim sucessivamente. 
 
Questão 4) Elabore uma função maior que recebe 2 numero de e retorna o maior 
utilizando o comando ternário. 
 
#include<stdio.h> 
#include<stdlib.h> 
 
int Maior(int n1, int n2){ 
 return (n1>n2?n1:n2); 
} 
 
int main(){ 
 
 int numero1, numero2, maior; 
 
 printf("\n Digite primeiro numero:"); 
 scanf("%d",&numero1); 
 printf("\n Digite segundo numero:"); 
 scanf("%d",&numero2); 
 
MINISTÉRIO DA EDUCAÇÃO 
UNIVERSIDADE FEDERAL DOS VALES DO JEQUITINHONHA E MUCURI 
CAMPUS AVANÇADO DO MUCURI – TEÓFILO OTONI - MG 
BACHARELADO EM CIÊNCIA E TECNOLOGIA 
 
 
Aluno: ___Brenda Ketilyn Lages de Paula__ _________________________ Algoritmos 
 
 
 
 maior=Maior(numero1,numero2); 
 printf("\n O maior numero e: %d",maior); 
 
 
 return 0; 
 
 } 
 
Questão 5) Elabore uma função void que recebe um numero e utilizando um menu 
e switch case imprima os dias da semana Ex. 1 segunda ... 
 
#include<stdio.h> 
#include<stdlib.h> 
 
void Diadasemana(int numero){ 
 
 switch(numero){ 
 case 1:printf("\n Domingo"); 
 break; 
 case 2:printf("\n Segunda"); 
 break; 
 case 3:printf("\n Terca"); 
 break; 
 case 4:printf("\n Quarta"); 
 break; 
 case 5:printf("\n Quinta"); 
 break; 
 case 6:printf("\n Sexta"); 
 break; 
 case 7:printf("\n Sabado"); 
 break; 
 default: 
 printf("Opcao invalida"); 
 break; 
 } 
} 
 
int main(){ 
 int numero; 
 
 do{ 
 printf("\n Digite um numero de 1 a 7: "); 
 
MINISTÉRIO DA EDUCAÇÃO 
UNIVERSIDADE FEDERAL DOS VALES DO JEQUITINHONHA E MUCURI 
CAMPUS AVANÇADO DO MUCURI – TEÓFILO OTONI - MG 
BACHARELADO EM CIÊNCIA E TECNOLOGIA 
 
 
Aluno: ___Brenda Ketilyn Lages de Paula__ _________________________ Algoritmos 
 
 
 scanf("%d",&numero); 
 
 }while(numero <1 || numero>7); 
 
 
 Diadasemana(numero); 
 
 return 0; 
} 
 
 
Questão 6) Declare variáveis globais: Vetor de inteiro V1 (50) 
 
a) Cria uma função que armazena números aleatórios de 12 a 200 utilizando 
um apontador 
b) Cria uma função que retorna a soma dos valores do vetor 
c) Cria uma função que retorna o maior valor do vetor 
d) Crie uma função que imprima o conteúdo do vetor 
 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
 
int tamanho = 50; 
int v1[50]; 
 
void Armazenar(){ 
 int i; 
 
 srand((unsigned)time(NULL)); 
 
 for(i=0;i<tamanho;i++){ 
 do{ 
 v1[i]=rand()%200; 
 }while(v1[i]<12); 
 } 
} 
 
int somaVetor (){ 
 int soma=0; 
 int i=0; 
 
 for(i=0;i<tamanho;i++){ 
 
MINISTÉRIO DA EDUCAÇÃO 
UNIVERSIDADE FEDERAL DOS VALES DO JEQUITINHONHA E MUCURI 
CAMPUS AVANÇADO DO MUCURI – TEÓFILO OTONI - MG 
BACHARELADO EM CIÊNCIA E TECNOLOGIA 
 
 
Aluno: ___Brenda Ketilyn Lages de Paula__ _________________________ Algoritmos 
 
 
 soma=soma+v1[i]; 
 } 
 return soma; 
} 
 
int Maiorvalorvetor(){ 
 int maior=0; 
 int i,j; 
 
 for(i=0;i<tamanho;i++){ 
 maior=v1[i]; 
 for(j=0;j<tamanho;j++){ 
 if(maior<v1[j])maior=v1[j]; 
 } 
 } 
 return maior; 
} 
 
void Imprimirvetor(int soma, int maior){ 
 int i; 
 for(i=0;i<tamanho;i++){ 
 printf(" %d",v1[i]); 
 } 
 printf("\n A soma de todos os numeros do vetor: %d",soma); 
 printf("\n O maior numero do vetor: %d\n\n",maior); 
 
} 
 
int main(){ 
 int soma; 
 int maior; 
 
 Armazenar(); 
 soma=somaVetor(); 
 maior=Maiorvalorvetor(); 
 Imprimirvetor(soma,maior); 
 
 return 0; 
} 
 
 
Questão 7) Declare variáveis globais: Matriz M1 5 x 7. 
 
 
MINISTÉRIO DA EDUCAÇÃO 
UNIVERSIDADE FEDERAL DOS VALES DO JEQUITINHONHA E MUCURI 
CAMPUS AVANÇADO DO MUCURI – TEÓFILO OTONI - MG 
BACHARELADO EM CIÊNCIA E TECNOLOGIA 
 
 
Aluno: ___Brenda Ketilyn Lages de Paula__ _________________________ Algoritmos 
 
 
a) Cria uma função que armazena números aleatórios de 5 a 80 
b) Cria uma função que recebe um numero e retorno a soma da linha da 
matriz M1 
c) Criar uma função que retorna a soma da matriz M1 
d) Cria uma função que imprima a matriz (organizada linha e coluna) 
String 
 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
 
int tamanho_linha = 5, tamanho_coluna = 7; 
int m1[10][10]; 
void armazenar_matriz(){ 
 int i,j; 
 
 srand((unsigned)time(NULL)); 
 for(i=0;i<tamanho_linha;i++){ 
 for(j=0;j<tamanho_coluna;j++){ 
 do{ 
 m1[i][j]=rand()%80; 
 }while(m1[i][j]<5); 
 } 
 } 
} 
int soma_da_linha(int linha){ 
 int i; 
 int soma=0; 
 
 for(i=0;i<tamanho_coluna;i++){ 
 soma=soma+m1[linha-1][i];//-1, pois o vetor da matriz começa com "0".} 
 return soma; 
} 
int soma_matriz(){ 
 int soma=0,i,j; 
 
 for(i=0;i<tamanho_linha;i++){ 
 for(j=0;j<tamanho_coluna;j++){ 
 soma=soma+m1[i][j]; 
 } 
 } 
 
 
MINISTÉRIO DA EDUCAÇÃO 
UNIVERSIDADE FEDERAL DOS VALES DO JEQUITINHONHA E MUCURI 
CAMPUS AVANÇADO DO MUCURI – TEÓFILO OTONI - MG 
BACHARELADO EM CIÊNCIA E TECNOLOGIA 
 
 
Aluno: ___Brenda Ketilyn Lages de Paula__ _________________________ Algoritmos 
 
 
 return soma; 
} 
void Imprimir(){ 
 int i,j; 
 
 printf("\n"); 
 for(i=0;i<tamanho_linha;i++){ 
 for(j=0;j<tamanho_coluna;j++){ 
 
 printf(" %d ",m1[i][j]); 
 } 
 printf("\n"); 
 } 
} 
 
int main(){ 
 int linha, soma; 
 
 armazenar_matriz(); 
 printf("\n Digite o numero da linha: "); 
 scanf("%d",&linha); 
 
 printf("\n A soma da linha %d e: %d",linha,soma=soma_da_linha(linha)); 
 
 printf("\n A soma da matriz e: %d",soma=soma_matriz()); 
 
 Imprimir(); 
 
 return 0; 
} 
 
 
Questão 8) Crie uma função que lê uma string, calcula o cumprimento de uma 
string e retorna o mesmo. 
 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
 
int tamanhostring(char string[]){ 
 int tamanho=0; 
 
 while(string[tamanho]!='\0')tamanho++; 
 
MINISTÉRIO DA EDUCAÇÃO 
UNIVERSIDADE FEDERAL DOS VALES DO JEQUITINHONHA E MUCURI 
CAMPUS AVANÇADO DO MUCURI – TEÓFILO OTONI - MG 
BACHARELADO EM CIÊNCIA E TECNOLOGIA 
 
 
Aluno: ___Brenda Ketilyn Lages de Paula__ _________________________ Algoritmos 
 
 
 
 return tamanho; 
} 
 
int main(){ 
 int tamanho=10; 
 char texto[tamanho]; 
 
 printf("\n Digite uma Texto: "); 
 fflush(stdin); 
 gets(texto); 
 
 tamanho=tamanhostring(texto); 
 
 printf("\n Tamanho da palavra %s: %d\n",texto,tamanho); 
 
 return 0; 
} 
 
 
Questão 9) Elabore uma função que lê 2 String verifique se a s1 e s2 são iguais ou 
não, retornando 0 se os mesmo são iguais e 1 caso contrário. 
 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
 
int Verifica(char string1[],char string2[]){ 
 int i=0,j=0; 
 
 while(string1[i]!='\0'&&string2[i!='\0']){ 
 if(string1[i]==string2[i])j++; 
 i++; 
 } 
 
 return (i==j?0:1); 
} 
 
int main(){ 
 int tamanho=10; 
 char texto1[tamanho]; 
 char texto2[tamanho]; 
 
 
MINISTÉRIO DA EDUCAÇÃO 
UNIVERSIDADE FEDERAL DOS VALES DO JEQUITINHONHA E MUCURI 
CAMPUS AVANÇADO DO MUCURI – TEÓFILO OTONI - MG 
BACHARELADO EM CIÊNCIA E TECNOLOGIA 
 
 
Aluno: ___Brenda Ketilyn Lages de Paula__ _________________________ Algoritmos 
 
 
 printf("\n Digite uma Texto 1: "); 
 fflush(stdin); 
 gets(texto1); 
 
 printf("\n Digite uma Texto 2: "); 
 fflush(stdin); 
 gets(texto2); 
 
 tamanho=Verifica(texto1,texto2); 
 
 tamanho==0?(printf("\n %s e %s sao iquais",texto1,texto2)):(printf("\n %s e %s sao 
diferentes",texto1,texto2)); 
 
 return 0; 
} 
Questão 10) Elabore um programa Principal em C 
a) Chame todas as funções desenvolvida no trabalho, passando parâmetro 
quando necessário e recebendo e imprimindo quando houver retorno. 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
 
int Maior(int n1, int n2){ 
 return (n1>n2?n1:n2); 
} 
 
void Diadasemana(int numero){ 
 
 switch(numero){ 
 case 1:printf("\n Domingo"); 
 break; 
 case 2:printf("\n Segunda"); 
 break; 
 case 3:printf("\n Terca"); 
 break; 
 case 4:printf("\n Quarta"); 
 break; 
 case 5:printf("\n Quinta"); 
 break; 
 case 6:printf("\n Sexta"); 
 
MINISTÉRIO DA EDUCAÇÃO 
UNIVERSIDADE FEDERAL DOS VALES DO JEQUITINHONHA E MUCURI 
CAMPUS AVANÇADO DO MUCURI – TEÓFILO OTONI - MG 
BACHARELADO EM CIÊNCIA E TECNOLOGIA 
 
 
Aluno: ___Brenda Ketilyn Lages de Paula__ _________________________ Algoritmos 
 
 
 break; 
 case 7:printf("\n Sabado"); 
 break; 
 default: 
 printf("Opcao invalida"); 
 break; 
 } 
} 
 
int tamanho = 50; 
int v1[50]; 
 
void Armazenar(){ 
 int i; 
 
 srand((unsigned)time(NULL)); 
 
 for(i=0;i<tamanho;i++){ 
 do{ 
 v1[i]=rand()%200; 
 }while(v1[i]<12); 
 } 
} 
 
int somaVetor (){ 
 int soma=0; 
 int i=0; 
 
 for(i=0;i<tamanho;i++){ 
 soma=soma+v1[i]; 
 } 
 return soma; 
} 
 
int Maiorvalorvetor(){ 
 int maior1=0; 
 int i,j; 
 
 for(i=0;i<tamanho;i++){ 
 maior1=v1[i]; 
 for(j=0;j<tamanho;j++){ 
 if(maior1<v1[j])maior1=v1[j]; 
 } 
 
MINISTÉRIO DA EDUCAÇÃO 
UNIVERSIDADE FEDERAL DOS VALES DO JEQUITINHONHA E MUCURI 
CAMPUS AVANÇADO DO MUCURI – TEÓFILO OTONI - MG 
BACHARELADO EM CIÊNCIA E TECNOLOGIA 
 
 
Aluno: ___Brenda Ketilyn Lages de Paula__ _________________________ Algoritmos 
 
 
 } 
 return maior1; 
} 
 
void Imprimirvetor(int soma, int maior1){ 
 int i; 
 for(i=0;i<tamanho;i++){ 
 printf(" %d",v1[i]); 
 } 
 printf("\n A soma de todos os numeros do vetor: %d",soma); 
 printf("\n O maior numero do vetor: %d\n\n",maior1); 
 
} 
 
int tamanho_linha = 5, tamanho_coluna = 7; 
int m1[10][10]; 
void armazenar_matriz(){ 
 int i,j; 
 
 srand((unsigned)time(NULL)); 
 for(i=0;i<tamanho_linha;i++){ 
 for(j=0;j<tamanho_coluna;j++){ 
 do{ 
 m1[i][j]=rand()%80; 
 }while(m1[i][j]<5); 
 } 
 } 
} 
int soma_da_linha(int linha){ 
 int i; 
 int soma=0; 
 
 for(i=0;i<tamanho_coluna;i++){ 
 soma=soma+m1[linha-1][i];//-1, pois o vetor da matriz começa com "0". 
 } 
 return soma; 
} 
int soma_matriz(){ 
 int soma=0,i,j; 
 
 for(i=0;i<tamanho_linha;i++){ 
 for(j=0;j<tamanho_coluna;j++){ 
 soma=soma+m1[i][j]; 
 
MINISTÉRIO DA EDUCAÇÃO 
UNIVERSIDADE FEDERAL DOS VALES DO JEQUITINHONHA E MUCURI 
CAMPUS AVANÇADO DO MUCURI – TEÓFILO OTONI - MG 
BACHARELADO EM CIÊNCIA E TECNOLOGIA 
 
 
Aluno: ___Brenda Ketilyn Lages de Paula__ _________________________ Algoritmos 
 
 
 } 
 } 
 
 return soma; 
} 
void Imprimir(){ 
 int i,j; 
 
 printf("\n"); 
 for(i=0;i<tamanho_linha;i++){ 
 for(j=0;j<tamanho_coluna;j++){ 
 
 printf(" %d ",m1[i][j]); 
 } 
 printf("\n"); 
 } 
} 
 
int tamanhostring(char string[]){ 
 int tamanho1=0; 
 
 while(string[tamanho1]!='\0')tamanho1++; 
 
 return tamanho1; 
} 
 
int Verifica(char string1[],char string2[]){ 
 int i=0,j=0; 
 
 while(string1[i]!='\0'&&string2[i!='\0']){ 
 if(string1[i]==string2[i])j++; 
 i++; 
 } 
 
 return (i==j?0:1); 
} 
 
 
int main(){ 
 int opcao; 
 int numero1, numero2, maior; 
 int numero; 
 int soma, maior1; 
 
MINISTÉRIO DA EDUCAÇÃO 
UNIVERSIDADE FEDERAL DOS VALES DO JEQUITINHONHA E MUCURI 
CAMPUS AVANÇADO DO MUCURI – TEÓFILO OTONI - MG 
BACHARELADO EM CIÊNCIA E TECNOLOGIA 
 
 
Aluno: ___Brenda Ketilyn Lages de Paula__ _________________________ Algoritmos 
 
 
 int linha, soma1; 
 int tamanho=10; 
 char texto[tamanho]; 
 int tamanho1=10; 
 char texto1[tamanho]; 
 char texto2[tamanho]; 
 
 
 printf("\n Escolha a opcao:"); 
 printf("\n 1-Elabore uma funcao maior que recebe 2 numero de e retorna o maior 
utilizando o comando ternario"); 
 printf("\n 2-Elabore uma funcao void que recebe um numero e utilizando um 
menu e switch case imprima os dias da semana"); 
 printf("\n 3-Declare variaveis globais: Vetor de inteiro V1(50)"); 
 printf("\n a)Cria uma funcao que armazena numeros aleatorios de 12 a 200 
utilizando um apontador \n b)Cria uma funcao que retorna a soma dos valores do vetor\n c)Cria uma funcao que retorna o maior valor do vetor \n d)Crie uma funcao que 
imprima o conteudo do vetor"); 
 printf("\n 4-Declare variaveis globais-Matriz M1 5 x 7:"); 
 printf("\n a)Cria uma funcao que armazena numeros aleatorios de 5 a 80\n 
b)Cria uma funcao que recebe um numero e retorno a soma da linha da matriz M1\n 
c)Criar uma funcao que retorna a soma da matriz M1\n d) Cria uma funcao que 
imprima a matriz (organizada linha e coluna)"); 
 printf("\n 5-Crie uma funcao que le uma string, calcula o cumprimento de uma 
string e retorna o mesmo"); 
 printf("\n 6-Elabore uma funcao que le 2 String verifique se a s1 e s2 sao iguais 
ou nao, retornando 0 se os mesmo sao iguais e 1 caso contrario\n"); 
 
 scanf("%d", &opcao); 
 switch(opcao){ 
 case 1: 
 printf("\n Digite primeiro numero:"); 
 scanf("%d",&numero1); 
 printf("\n Digite segundo numero:"); 
 scanf("%d",&numero2); 
 
 maior=Maior(numero1,numero2); 
 printf("\n O maior numero e: %d",maior); 
 
 break; 
 case 2: 
 do{ 
 printf("\n Digite um numero de 1 a 7: "); 
 
MINISTÉRIO DA EDUCAÇÃO 
UNIVERSIDADE FEDERAL DOS VALES DO JEQUITINHONHA E MUCURI 
CAMPUS AVANÇADO DO MUCURI – TEÓFILO OTONI - MG 
BACHARELADO EM CIÊNCIA E TECNOLOGIA 
 
 
Aluno: ___Brenda Ketilyn Lages de Paula__ _________________________ Algoritmos 
 
 
 scanf("%d",&numero); 
 
 }while(numero <1 || numero>7); 
 Diadasemana(numero); 
 
 break; 
 
 case 3: 
 Armazenar(); 
 soma=somaVetor(); 
 maior1=Maiorvalorvetor(); 
 Imprimirvetor(soma,maior1); 
 break; 
 
 case 4: 
 armazenar_matriz(); 
 printf("\n Digite o numero da linha: "); 
 scanf("%d",&linha); 
 
 printf("\n A soma da linha %d e: %d",linha,soma1=soma_da_linha(linha)); 
 
 printf("\n A soma da matriz e: %d",soma1=soma_matriz()); 
 
 Imprimir(); 
 break; 
 
 case 5: 
 printf("\n Digite uma Texto: "); 
 fflush(stdin); 
 gets(texto); 
 
 tamanho=tamanhostring(texto); 
 
 printf("\n Tamanho da palavra %s: %d\n",texto,tamanho); 
 break; 
 
 case 6: 
 printf("\n Digite uma Texto 1: "); 
 fflush(stdin); 
 gets(texto1); 
 
 printf("\n Digite uma Texto 2: "); 
 fflush(stdin); 
 
MINISTÉRIO DA EDUCAÇÃO 
UNIVERSIDADE FEDERAL DOS VALES DO JEQUITINHONHA E MUCURI 
CAMPUS AVANÇADO DO MUCURI – TEÓFILO OTONI - MG 
BACHARELADO EM CIÊNCIA E TECNOLOGIA 
 
 
Aluno: ___Brenda Ketilyn Lages de Paula__ _________________________ Algoritmos 
 
 
 gets(texto2); 
 
 tamanho1=Verifica(texto1,texto2); 
 
 tamanho1==0?(printf("\n %s e %s sao iquais",texto1,texto2)):(printf("\n %s e %s sao 
diferentes",texto1,texto2)); 
 break; 
 
 } 
 
 return 0; 
 }

Continue navegando