Exercício 12279

Setembro 11, 2008

E com este exercício está concluído o capitulo. Espero que tenham aproveitado. Qualquer comentário ou dica é só mandar.

Abraço,
Fernando

/* 12279.c 20080911 fmc
 * Write a program to read two integers with the following significance.
 * The first integer value represents a time of day on a 24 hour clock,
 * so that 1245 represents quarter to one mid-day, for example.
 * The second integer represents a time duration in a similar way, so
 * that 345 represents three hours and 45 minutes.
 * This duration is to be added to the first time, and the result
 * printed out in the same notation, in this case 1630 which is the
 * time 3 hours and 45 minutes after 12.45.
 * Typical output might be
 * Start time is 1415. Duration is 50. End time is 1505.
 * There are a few extra marks for spotting.
 * Start time is 2300. Duration is 200. End time is 100.
 *
 * compile: gcc 12278.c -o 12278
 */

 #include<stdio.h>
 main() {
	 int inicio,duracao,termino,h_dur,h_start,m_dur,m_start,ter_h,ter_min;
	 printf("\n\nEntre com o horario de inicio [hhmm]: ");
	 scanf("%d",&inicio);
	 printf("\nEntre com a duração [hhmm]: ");
	 scanf("%d",&duracao);

	 h_start=inicio/100;
	 m_start=inicio-(h_start*100);
  	 h_dur=duracao/100;
	 m_dur=duracao-(h_dur*100);
	 ter_h=h_start+h_dur;
	 ter_min=m_start+m_dur;
	 if (ter_min>59) {
		 ter_h++;
		 ter_min=ter_min-60;
	 }
	 if (ter_h>23) {
		 ter_h=ter_h-24;
	 }
	termino=(ter_h*100)+ter_min;

	printf("\nInicio %d. Duração %d. Termino %d\n\n",inicio,duracao,termino);
	 return 0 ;
 }

Exercício 12278

Setembro 11, 2008
/* 12278.c 20080911 fmc
 * Given as input an integer number of seconds, print as output the
 * equivalent time in hours, minutes and seconds. Recommended output
 * format is something like
 *
 * 7322 seconds is equivalent to 2 hours 2 minutes 2 seconds.
 * compile: gcc 12278.c -o 12278
 */

#include<stdio.h>
main()
{
	int total,h,m,s;
	printf("\n Quantos segundos? ");
	scanf("%d",&total);

	h=total/3600;
	m=(total/60)-(h*60);
	s=total-(h*3600)-(m*60);

	printf("\n%d segundos é equivalente a %d horas %d minutos %d segundos\n\n",total,h,m,s);
	return 0;

}

Exercicio 12277

Agosto 23, 2008
 
/* 12277.c 20080820 fmc
 * Exercise 12277
 * Given as input a floating (real) number of centimeters, print out the
 * equivalent number of feet (integer) and inches (floating, 1 decimal), with
 * the inches given to an accuracy of one decimal place.
 * Assume 2.54 centimeters per inch, and 12 inches per foot.
 * If the input value is 333.3, the output format should be:
 * 333.3 centimeters is 10 feet 11.2 inches.
 * compile:
 * cc 12277.c -o 12277
 */

#include<stdio.h>
main() {
  float cm,pol,pes,pes_fracional,in;
  
  printf("\n\nEntre com o valor em centimetros: ");
  scanf("%f",&cm);
  // Convertendo centimetros em polegadas pol = cm *  0,3937008
  pol=cm*0.3937008;
  // Convertendo as polegadas em pés pes=pol/12
  pes=pol/12;
  // Extraindo a parte inteira do numero
  int a = (int)pes;
  pes_fracional=pes-a;
  // Convertendo a parte fracional de pes em polegadas
  in=pes_fracional*12;
  printf("\n\n%.1f centimeters is %d feet %.1f inches.s\n\n",cm,a,in);
}

Exercicio 12276

Agosto 23, 2008
/* 12276.c 20080819 fmc
 * Exercise 12276
 * Write a program to read a number of units of length (a float) and print out
 * the area of a circle of that radius. Assume that the value of pi is 3.14159
 * (an appropriate declaration will be given you by ceilidh - select setup).
 * Your output should take the form: The area of a circle of radius ... units
 * is .... units.
 * If you want to be clever, and have looked ahead in the notes, print the
 * message Error: Negative values not permitted. if the input value is
 * negative.
 * compile:
 * cc 12276.c -o 12276
 */
#include<stdio.h>
#include<math.h>
#define pi 3.14159

main() {
  float raio,area ;
  inicio:
  printf("\n\nEntre com o raio: ");
  scanf("%f",&raio);  
  if (raio<0) {
    printf("\n\nErro: Valores negativos não são permitidos");
    goto inicio; }
  else {
    area=pi*pow(raio,2);
    printf("\n\nA area de um circulo de raio %.2f unidades é %.2f unidades\n\n",raio,area);
  }
}

Exercício 12275

Agosto 23, 2008

Nossa esse eu fiz muito toscamente… 

N!=3*2*1=6 possibilidades para os três números
 Apenas mostrei as variáveis a,b e c nas diferentes ordens
 sei q mais a frente no curso vou aprender a forma correta
 de trocar as variáveis usando um array e usar um loop para mostrar 
e trocar as variaveis

 

/* 12275.c 20080815 fmc
 * Exercise 12275
 * Write a program to read a positive integer at
 * least equal to 3, and print
 * out all possible permutations of three positive integers less or equal to
 * than this value.
 * compile:
 * cc 12275.c -o 12275
 */
#include<stdio.h>
main()
{
 int a,b,c,i;
 i=6;
 inicio:
 printf("\nEntre um número inteiro positivo maior ou igual a três:");
 scanf("\%d",&c);
 if (c<3)
   {
    printf("\nValor invalido digite novamente!\n");
    goto inicio;
   }
 else
  {
   c=c-1;
   b=c-1;
   a=b-1;
  }
 printf("\n\n\t{ [%d], [%d], [%d] }\n",a,b,c);
 printf("\t{ [%d], [%d], [%d] }\n",a,c,b);
 printf("\t{ [%d], [%d], [%d] }\n",b,c,a);  
 printf("\t{ [%d], [%d], [%d] }\n",b,a,c);  
 printf("\t{ [%d], [%d], [%d] }\n",c,a,b);
 printf("\t{ [%d], [%d], [%d] }\n\n",c,b,a);  
}

Exercício 12272

Agosto 23, 2008
/* 12272.c 20080815 fmc
 * Exercise 12272
 * Write a program that works out the largest
 * and smallest values from a
 * set of 10 inputted numbers.
 * compile:
 * cc 12272.c -o 12272
 */
#include<stdio.h>
main()
{
 int a,b,c,i;
 i=1;
// Lê os números
  le_proximo:
 printf("Entre o número[%d]: ",i);
 scanf("%d",&c);
// Compara
 if (c>a)
  { a=c ; } //  maior -> a
 else if (c<b)
   { b=c ; } // menor -> b
 ++i;
 if (i!=11)
  { goto le_proximo; }
 printf("\nO maior número é %d",a);
 printf("\nO menor número é %d\n",b);
}

Exercícios 12273 e 12274

Agosto 20, 2008

Estes também parecem bem tranquilos, não sei se não entendi bem o enunciado mas o 12274 é só um monte de printf com \n rs… rs… Mas a formatação na hora de mostrar um número real com quantas casas foi legal de descobrir rs…
[]´s

/* 12273.c 20080815 fmc
  * Exercise 12273
  * Write a program to read a "float" representing a number of degrees Celsius,
  * and print as a "float" the equivalent temperature in degrees Fahrenheit.
  * Print your results in a form such as 100.0 degrees Celsius converts to
  * 212.0 degrees Fahrenheit.
  * compile:
  * cc 12273.c -o 12273
  */

#include<stdio.h>
main()
{
   float c,f ;
   printf("\n\nTemperatura em °C : ");
   scanf("%f",&c);
// Converte °C para °F
   f=((c*9)/5)+32;
   printf("\n%.1f degrees Celsius converts to %.1f degrees Fahrenheit.\n\n",c,f);
// %.nf -> n casas decimais depois da virgula
}
/*	12274.c 20080815 fmc
 *	Exercise 12274
 *	Write a program to print several lines (such as your name and address). You
 *	may use either several printf instructions, each with a newline character
 *	in it, or one printf with several newlines in the string.
 * 	compile:
 * 	cc 12274.c -o 12274
 */

#include<stdio.h>
main() {

printf("\nFMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMXMXFMXFM\n");
printf("FMXFMXFMXFMXFMXFMXFMX----------------FMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMX\n");
printf("FMXFMXFMXFMXFMXFMXFMX----!!!!!!!!----FMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMX\n");
printf("FMXFMXFMXFMXFMXFMXFMX-----0----0-----FMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMX\n");
printf("FMXFMXFMXFMXFMXFMXFMX-------d--------FMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMX\n");
printf("FMXFMXFMXFMXFMXFMXFMX-----|~~~~|-----FMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMX\n");
printf("FMXFMXFMXFMXFMXFMXFMX--------|-------FMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMX\n");
printf("FMXFMXFMXFMXFMXFMXFMX--------V-------FMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMX\n");
printf("FMXFMXFMXFMXFMXFMXFMX----------------FMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMX\n");
printf("FMXFMXFMXFMXFMXFMXFMX----------------FMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMX\n");
printf("FMXFMXFMXFMXFMXFMXFMX----------------FMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMX\n");
printf("FMXFMXFMXFFMXFMXFMXFMXFMXFMXFMXFMXMFMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMXFMX\n");

}

Exercícios 12270 e 12271

Agosto 20, 2008

Estes primeiros exercicios me pareceram simples. Só para constar estou usando como ambiente o Debian “Etch” e como IDE o Anjuta + gcc. Se alguem tiver sugestões de ambiente/IDE é só mandar. Instalei também o Solaris 10 que tem o ambiente CDE usado no curso, mas o Debian me pareceu mais fácil… rs Apesar de o Solaris ter uma interface muito bonita e robusta.

 

/* 12270.c 20080814 fmc
 * Exercise 12270
 * Input two numbers and work out their sum,
 * average and sum of the squares
 * of the numbers.
 * compile:
 * cc 12270.c -o 12270
 */

#include <math.h>
#include <stdio.h>
main()
{
   int a,b,sum,sum2;
   float average;
   printf("\nEntre com o primeiro número: ");
   scanf("%d",&a);
   printf("\nEntre com o segundo número: ");
   scanf("%d",&b);
   sum=a+b;
   average=(a+b)/2;
   sum2=pow(a,2)+pow(b,2);
   printf("\nA soma de %d e %d é: %d",a,b,sum);
   printf("\nA média de %d e %d é: %f",a,b,average);
   printf("\nA soma dos quadrados de %d e %d é: %d\n\n",a,b,sum2);
}
/* 12271.c 20080814 fmc
 * Exercise 12271
 * Input and output your name, address and
 * age to an appropriate structure.
 * compile:
 * cc 12271.c -o 12271
 *
 */

#include <stdio.h>
main()
{
   char name[35], address[35];
   int age;

   /* não usar o gets pq ele pode capturar mais
    * valores do que comporta a variavel, melhor
    * usar o fgets(variavel,tamanho, stdin)
    */
   printf("Nome: ");
   fgets(name,35,stdin);
   printf("\nEndereço: ");
   fgets(address,35,stdin);
   printf("\nIdade: ");
   scanf("%d",&age);
   printf("\n\tNome:\t\t %s\n",name);
   printf("\n\tEndereço:\t %s\n",address);
   printf("\n\tIdade:\t\t %d\n\n",age);
}

Aprendendo a Linguagem C

Agosto 19, 2008

Olá a todos! Trabalho com TI e gosto muito da área de programação, apesar de não trabalhar diretamente com esta parte. Para me divertir um pouco comecei a estudar um pouquinho programação. A algum tempo atrás perguntei a um grande mestre nesta arte por onde começar e ele me indicou um curso que estou achando muito bom… Estou seguindo este curso e resolvendo os exercicios propostos. A medida que for avançando irei colocando as minhas respostas e quem estiver a fim de estudar junto pode ir comentando e colocando suas dúvidas, críticas e  sugestões.

Abaixo segue o site do treinamento.

http://www.cs.cf.ac.uk/Dave/C/CE.html

Gostaria de agradecer ao mestre AlexF pela indicação de onde começar e ao professor Dave Marshall pelo excelente material.

 Um grande abraço a todos!

Fernando