Skip to content

Instantly share code, notes, and snippets.

@Jessidhia
Created November 17, 2011 02:13
Show Gist options
  • Save Jessidhia/1372174 to your computer and use it in GitHub Desktop.
Save Jessidhia/1372174 to your computer and use it in GitHub Desktop.
EE2 test
CC=gcc
CFLAGS=-Wall -Wextra -Werror
LDFLAGS=-Wl,--as-needed
LDLIBS=-lm
all: q1 q2 q3 q4 q5
q%: q%.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int mat[20];
char fname[20][21], lname[20][21];
float grade[20][3], avg;
int pick, i, j;
char *grnames[3] = {"Primeira", "Segunda", "Terceira"};
printf("*** Modo de cadastro\n");
for(i = 0; i < 20; i++) {
printf("Matricula (0 para sair): ");
scanf("%d", &mat[i]);
if (!mat[i]) break;
fgets(&fname[i][0], 20, stdin); // Read the leftover \n
printf("Primeiro nome: ");
fgets(&fname[i][0], 20, stdin);
fname[i][strlen(fname[i])-1] = '\0'; // Remove the trailing \n
printf("Sobrenome: ");
fgets(&lname[i][0], 20, stdin);
lname[i][strlen(lname[i])-1] = '\0'; // Remove the trailing \n
for(j = 0; j < 3; j++)
do {
printf("%s nota: ", grnames[j]);
scanf("%f", &grade[i][j]);
} while (grade[i][j] < 0 || grade[i][j] > 10);
printf("\n");
}
printf("*** Modo de consulta\n");
while (1) {
printf("Matricula (0 para sair): ");
scanf("%d", &pick);
if (!pick) break;
for (i = 0; i < 20; i++)
if (mat[i] == pick)
break;
if (i == 20) {
printf("Matricula nao encontrada\n\n");
continue;
}
avg = (grade[i][0] + grade[i][1] + grade[i][2]) / 3;
printf("A media de %s %s e %.2g\n\n", fname[i], lname[i], avg);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int i, j, k, m[20][20], sz, maxlen = 0, det = 1;
//int mat[20][20], sz, i, j, m, n;
printf("Ordem (max 20): ");
fflush(stdout);
scanf("%d", &sz);
if (sz < 0 || sz > 20) {
printf("Ordem invalida ou nao suportada\n");
return 1;
}
for (i = 0; i < sz; i++)
for (j = 0; j < sz; j++) {
int aux;
printf("M_{%d,%d} = ", i+1, j+1);
fflush(stdout);
scanf("%d", &m[i][j]);
aux = (int)(log10(m[i][j])+1);
if (aux > maxlen) maxlen = aux;
}
for (i = 0; i < sz; i++) {
printf("| ");
for (j = 0; j < sz; j++)
printf("%*d ", maxlen, m[i][j]);
printf("|\n");
}
for (i = 0; det && i < sz; i++) {
int acc1 = 0, acc2 = 0;
int rat1n, rat1d, rat2n, rat2d;
for (j = 0; j < sz; j++) {
if (m[i][j]) acc1 = 1;
if (m[j][i]) acc2 = 1;
}
if (acc1 == 0 || acc2 == 0) {
det = 0;
break;
}
acc1 = acc2 = 0;
/* verificar proporcao e diferenca entre linhas / colunas */
for (j = i + 1; det && j < sz; j++) {
int diff1, diff2;
int mdc1, mdc2;
int u, v;
int prop1, prop2;
/* proporcao do primeiro elemento das linhas */
rat1n = m[i][0];
rat1d = m[j][0];
/* proporcao do primeiro elemento das colunas */
rat2n = m[0][i];
rat2d = m[0][j];
/* diferenca entre os primeiros elementos respectivos */
diff1 = rat1n - rat1d;
diff2 = rat2n - rat2d;
/* verificar se diferenca e constante */
prop1 = prop2 = 1;
for(k = 1; k < sz; k++) {
if (prop1 && m[i][k] - m[j][k] == diff1)
{}
else
prop1 = 0;
if (prop2 && m[k][i] - m[k][j] == diff2)
{}
else
prop2 = 0;
}
if (prop1 || prop2) {
det = 0;
break;
}
/* diferenca nao constante; verificar proporcionalidade */
/* calcular mdc pelo algoritmo Stein para reduzir as razoes */
/* mdc1 */
if (rat1n == 0 || rat1d == 0)
mdc1 = rat1n | rat1d;
else {
u = rat1n;
v = rat1d;
for (k = 0; (u % 2 || v % 2) == 0; ++k) {
u /= 2;
v /= 2;
}
while (u % 2 == 0)
u /= 22 ;
do {
while (v % 2 == 0)
v /= 2;
if (u < v) {
v -= u;
} else {
long long diff = u - v;
u = v;
v = diff;
}
v /= 2;
} while (v);
mdc1 = u * (int) pow(2,k);
}
rat1n /= mdc1;
rat1d /= mdc1;
/* mdc2 */
if (rat2n == 0 || rat2d == 0)
mdc2 = rat2n | rat2d;
else {
u = rat2n;
v = rat2d;
for (k = 0; (u % 2 || v % 2) == 0; ++k) {
u /= 2;
v /= 2;
}
while (u % 2 == 0)
u /= 22 ;
do {
while (v % 2 == 0)
v /= 2;
if (u < v) {
v -= u;
} else {
long long diff = u - v;
u = v;
v = diff;
}
v /= 2;
} while (v);
mdc2 = u * (int) pow(2,k);
}
rat2n /= mdc2;
rat2d /= mdc2;
prop1 = prop2 = 1;
for(k = 1; k < sz; k++) {
if (prop1 && m[i][k] % rat1n == 0 && m[j][k] % rat1d == 0 && m[i][k] / rat1n == m[j][k] / rat1d)
{}
else
prop1 = 0;
if (prop2 && m[k][i] % rat2n == 0 && m[k][j] % rat2d == 0 && m[k][i] / rat2n == m[k][j] / rat2d)
{}
else
prop2 = 0;
}
if (prop1 || prop2) det = 0;
}
}
if (det)
printf("A matriz e inversivel\n");
else
printf("A matriz nao e inversivel\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int lambda, n, k, i;
double acc = 0;
double aux;
printf("Entre lambda: ");
scanf("%d", &lambda);
printf("Entre n: ");
scanf("%d", &n);
for (k = 0; k <= n; k++) {
aux = pow(lambda, k);
/* Calcular fatorial diretamente excede a precisao do double e causa infinitos */
for (i = 2; i <= k; i++)
aux /= i;
acc += aux;
}
printf("Resultado apos %d iteracoes: %g\n", n, acc);
return 0;
}
/* 4) Faca um programa que solicite x, y e n e prove a expansao binomial: (x+y)^n = \sum_{i=0}^{n}C(n,i)x^{n-i}y{i} */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int x, y, n, i, j;
/* Usar 'long long' para poder fazer comparacao confiavel */
long long acc1, acc2;
printf("Entre x: ");
scanf("%d", &x);
printf("Entre y: ");
scanf("%d", &y);
printf("Entre n: ");
scanf("%d", &n);
/* Calcular primeiro lado */
acc1 = (long long) pow(x + y, n);
/* Calcular segundo lado */
acc2 = 0;
for (i = 0; i <= n; i++) {
long long comb = 1;
/* C(n,i) = n! / i! ... */
for (j = n; j > i; j--)
comb *= j;
/* ... / (n-i)! */
for (j = 2; j <= (n - i); j++)
comb /= j;
acc2 += (long long) (comb * pow(x, n-i) * pow(y, i));
}
printf("%lld %s= %lld\n", acc1, acc1 == acc2 ? "=" : "!", acc2);
return 0;
}
/* 5) Faca um programa que leia uma lista de numeros inteiros e imprima a lista em ordem inversa de entrada. A lista pode ter ate 100 elementos e o flag e um numero negativo. */
#include <stdio.h>
#include <stdlib.h>
int main() {
int list[100];
int i, j;
for (i = 0; i < 100; i++) {
printf("Entre numero: ");
scanf("%d", &list[i]);
if (list[i] < 0)
break;
}
if (i >= 100) --i; /* Evitar ler apos o fim se as 100 posicoes forem usadas */
for (j = i; j >= 0; j--) {
printf("%d ", list[j]);
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment