Postagens

Mostrando postagens com o rótulo Codigo

C - Usando atoi e gets

#include stdio.h #include stdlib.h void main() { int houses, hotels, total; char temp[4]; printf("Enter the number of houses:"); gets(temp); houses=atoi(temp); printf("Enter the number of hotels:"); gets(temp); hotels=atoi(temp); total=houses*40+hotels*115; printf("You owe the bank $%i.\n",total); } referencia - gets referencia - atoi

C - Pergunta YES ou NO

/* YORN - a program to get a yes or no answer */ #include stdio.h #include conio.h #define TRUE 1 //this is "true" in C #define FALSE !TRUE //this is "not true" void main() { char c; printf("Please answer Y for Yes or N for No:"); while(TRUE)//this means "loop forever" { c=getch(); if(c=='Y' || c=='y') break; if(c=='N' || c=='n') break; } printf("%c\n",c); }

C - Ordenar números em um Vetor

#include <stdio.h> #include <stdlib.h> #define SWAP(a,b) { int t; t=a; a=b; b=t; } #define INDEX 4 void ordenar(int a[], int n); int main() { printf("Exemplo de ordenacao de numeros inteiros num vetor!\n"); //inicializo as minhas variaveis int a = 3, b = 7; int vetor[INDEX] = {14, 58, 10, 5}; //chama a funcao ordena ordenar(vetor, INDEX); //imprimo os valores ja ordenados int i; for(i = 0; i < INDEX; i++) printf("%d ", vetor[i]); printf("\n"); system("PAUSE"); return 0; } void ordenar( int a[], int n ) { int i, j; for(i = 0; i < n; i++) { for(j = 1; j < (n-i); j++) { if(a[j-1] > a[j]) SWAP(a[j-1],a[j]); } } }

C - Passagem de variável por referência

/* AUTOR: Alcides Duarte Exemplo de passagem de parametros para um funcao atraves de referencia, ou seja, a funcao recebe o endereco de memoria que aponta para a variavel. Quando a funcao altera os valores, esta indo direto no endereco de memoria onde se localiza o valor da variavel */ #include <stdio.h> #include <stdlib.h> void troca(int *px, int *py); int main() {     printf("Hello world!\n");     int a = 3, b = 7;     troca(&a, &b);     printf("a: %d - b: %d\n", a, b);     system("PAUSE");     return 0; } void troca(int *px, int *py) {     int z;     z = *px;     *px = *py;     *py = z; }

C - Escrevendo 40 Caracteres

#include #include <stdio.h> #include <stdlib.h> #define ENTER 0x0d        //Enter key void main() {     int x;     char c;     printf("You can type up to 40 characters.\n");     for(x=0;;x++)     {         c=getch();         if(c==ENTER || x==40)    //look for CAPS             break;                              //make it little         putch(c);                            //display character     } }

C - incremento(++) pre e pós-fixado

#include <stdio.h> #include <stdlib.h> void main() { int x = 10; int soma = 10; soma = ++x; printf("Soma: %d\n", soma); soma = x++; printf("Soma: %d\n", soma); }

C - Array

/* Simple array program */ #include <stdio.h> #include <stdlib.h> void main() {     int x,f;     char input[3];    //for storing text input     int favs[4];    //five items in array     printf("Please enter five of your favorite numbers\n");     printf("Between 1 and 100.\n");     for(x=0;x     {         printf("#%i favorite number is:",x+1);         f=atoi(gets(input));         favs[x]=f;     } }