]> web.ist.utl.pt Git - iaed.git/commitdiff
Adding ex4 in aula03.
authorAlexandre P Francisco <aplf@ist.utl.pt>
Thu, 13 Mar 2014 16:05:03 +0000 (16:05 +0000)
committerAlexandre P Francisco <aplf@ist.utl.pt>
Thu, 13 Mar 2014 16:05:03 +0000 (16:05 +0000)
aula03/Makefile
aula03/ex4.c [new file with mode: 0644]

index 92b3f5c7ad56ae4b318bf80792c7d98c929a8b4b..4ec33b6317b8e0912d8b13bdb7d9930876964b86 100644 (file)
@@ -1,11 +1,11 @@
 CC=gcc
 CFLAGS=-Wall -ansi -pedantic
 
-EXECS=ex1 ex2 ex3
+EXECS=ex1 ex2 ex3 ex4
 EX1OBJ=ex1.o aux.o
 EX2OBJ=ex2.o aux.o
 EX3OBJ=ex3.o aux.o
-
+EX4OBJ=ex4.o aux.o
 
 all: ${EXECS}
 
@@ -18,5 +18,8 @@ ex2: ${EX2OBJ} aux.h
 ex3: ${EX3OBJ} aux.h
        gcc ${CFLAGS} -o $@ ${EX3OBJ}
 
+ex4: ${EX4OBJ} aux.h
+       gcc ${CFLAGS} -o $@ ${EX4OBJ}
+
 clean:
        rm -f *.o ${EXECS}
diff --git a/aula03/ex4.c b/aula03/ex4.c
new file mode 100644 (file)
index 0000000..d55971f
--- /dev/null
@@ -0,0 +1,27 @@
+#include <stdio.h>
+#include <limits.h>
+#include "aux.h"
+
+#define NUMELEMS 100
+
+int procura(int v[], int tamanho, int k);
+
+int main() {
+  int v[NUMELEMS], tam, k;
+
+  scanf("%d", &tam);
+  leVector(v, tam);
+  scanf("%d", &k);
+  printf("%d found at pos: %d\n", k, procura(v, tam, k));
+
+  return 0;
+}
+
+int procura(int v[], int tamanho, int k) {
+  while (tamanho-- > 0)
+    if (v[tamanho] == k)
+      return tamanho;
+
+  return -1;
+}
+