Nick Raptis avatar Nick Raptis committed eab4f6f

Exercises for week 1 (11/03/2011)

- Eykleid algorithm
- Same with recursion

Comments (0)

Files changed (3)

+[patterns]
+** = native
+
+[repository]
+native = CRLF
+#include <stdio.h>
+#include <stdlib.h>
+#define DEBUG 0
+
+main()
+{
+    int x, y, z, count, fx, fy;
+    
+    do
+    {
+        printf("Eisagete akeraio X :\n");
+        scanf("%d", &x);
+    } while(x<=0);
+    printf("\n");
+    
+    do
+    {
+        printf("Eisagete akeraio Y :\n");
+        scanf("%d", &y);
+    } while(y<=0);
+    printf("\n");
+    
+    fx = x;
+    fy = y;
+    count = 0;
+    z = y;
+    
+    while(z!=0)
+    {
+        z = x%y;
+        x = y;
+        y = z;
+        count++;
+        if(DEBUG)
+            printf("%3d %10d %10d %10d\n", count, x, y, z);
+    }
+    
+    printf("\nO MKD twn %d, %d einai o %d\n", fx, fy, x);
+    printf("Brethike meta apo %d bhmata\n\n", count);
+
+    system("pause");
+}

1550_Ask1_Eykl_rec.c

+#include <stdio.h>
+#include <stdlib.h>
+#define DEBUG 0
+
+int mkd(int x, int y);
+
+int count=0;
+
+main()
+{
+    int x, y;
+    
+    do
+    {
+        printf("Eisagete akeraio X :\n");
+        scanf("%d", &x);
+    } while(x<=0);
+    printf("\n");
+    
+    do
+    {
+        printf("Eisagete akeraio Y :\n");
+        scanf("%d", &y);
+    } while(y<=0);
+    printf("\n");
+
+
+    printf("\nO MKD twn %d, %d einai o %d\n", x, y, mkd(x,y));
+    printf("Brethike meta apo %d bhmata\n\n", count);
+
+
+    system("pause");
+}
+
+
+int mkd(int x, int y)
+{
+    count++;
+    if(DEBUG)
+        printf("%3d %10d %10d %10d\n", count, x, y, (y!=0)?x%y:0);
+
+    if(y==0)
+        return x;
+    else
+    {
+        return mkd(y, x%y);
+    }
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.