Created
May 6, 2011 03:46
-
-
Save carlosbrando/958418 to your computer and use it in GitHub Desktop.
Apresenta as potências dos números de 1 a 10.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Apresenta as potências dos números de 1 a 10. | |
| * Nota: muito embora esse programa esteja correto | |
| * alguns compiladores apresentarão uma mensagem de | |
| * advertência com relação aos argumentos para as funções | |
| * table() e show(). Se isso acontecer, ignore */ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| int pwr(int a, int b); | |
| void table(int p[40][10]); | |
| void show(int p[40][10]); | |
| main(void) | |
| { | |
| int *p; | |
| p = malloc(40 * sizeof(int)); | |
| if (!p) { | |
| printf("Falha na solicitação de memória.\n"); | |
| exit(1); | |
| } | |
| /* aqui, p é simplesmente um ponteiro */ | |
| table(p); | |
| show(p); | |
| } | |
| /* Contrói a tabela de potências */ | |
| void table(int p[4][10]) /* Agora o compilador tem uma matriz para trabalhar */ | |
| { | |
| register int i, j; | |
| for (j = 1; j < 11; ++j) | |
| for (i = 1; i < 5; ++i) | |
| p[i-1][j-1] = pwr(j, i); | |
| } | |
| void show(int p[4][10]) /* Agora o compilador tem uma matriz para trabalhar */ | |
| { | |
| register int i, j; | |
| printf("%10s %10s %10s %10s\n", "N", "N^2", "N^3", "N^4"); | |
| for (j = 1; j < 11; ++j) { | |
| for (i = 1; i < 5; ++i) | |
| printf("%10d ", p[i-1][j-1]); | |
| printf("\n"); | |
| } | |
| } | |
| /* Eleva um inteiro a uma potência espefífica. */ | |
| int pwr(int a, int b) | |
| { | |
| register int t = 1; | |
| for (; b; b--) | |
| t = t * a; | |
| return t; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment