OpenCores
URL https://opencores.org/ocsvn/uart_block/uart_block/trunk

Subversion Repositories uart_block

[/] [uart_block/] [trunk/] [docs/] [C Sample/] [gcd.c] - Blame information for rev 29

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 25 leonardoar
# include <stdio.h>
2
# include <stdlib.h>
3
 
4
 
5
int main(void){
6
 
7
 
8
int n,m,value;
9
int values[3];
10
int u=rand();
11
int v=rand();
12
int modulo;
13
values[0]=u;
14
values[1]=v;
15
int ComputeGcd(int n, int m);
16
void ComputeGcdRandom(int u, int v, int values[]);
17
int  Modulo(int u, int v);
18
int Division (int n, int m);
19
 
20
 
21
//printf("Can you please write the first number for the gcd computation: \n");
22
//scanf("%i" , &n);
23
 
24
//printf("Can you please write the second number for the gcd computation: \n");
25
//scanf("%i" , &m);
26
 
27
 
28
//value = ComputeGcd (n,m);
29
//printf("The GCD of %i and %i is: %i\n",n,m,value);
30
 
31
//ComputeGcdRandom (u,v,values);
32
//printf("The GCD of the random values %i and %i is: %i\n",values[0],values[1],values[2]);
33
 
34
//modulo=Modulo(u, v);
35
//printf("The modulo operation of %i and %i is: %i\n",u,v,modulo);
36
 
37
 
38
printf("Can you please write the first number for the Division Operation: \n");
39
scanf("%i" , &n);
40
 
41
printf("Can you please write the second number for the Division Operation: \n");
42
scanf("%i" , &m);
43
 
44
 
45
value = Division(n,m);
46
printf("The GCD of %i and %i is: %i\n",n,m,value);
47
 
48
 
49
return 0;
50
 
51
}
52
 
53
//Compute GCD Function 
54
int ComputeGcd(int n, int m){
55
 
56
int temp;
57
 
58
while(m!=0){
59
 
60
temp=Modulo(n,m);
61
n=m;
62
m=temp;
63
}
64
 
65
return n;
66
}
67
 
68
 
69
//Compute GCD Function for Random Numbers
70
void ComputeGcdRandom(int u, int v , int values []){
71
 
72
int temp;
73
 
74
while(v!=0){
75
 
76
temp=Modulo(u,v);
77
u=v;
78
v=temp;
79
}
80
values[2]=u;
81
 
82
}
83
 
84
//Compute Modulo Function
85
int  Modulo(int u, int v){
86
 
87
int temp;
88
 
89
if(u<v){
90
temp=u;
91
u=v;
92
v=temp;
93
}
94
 
95
while(u>0){
96
u=u-v;
97
}
98
 
99
if(u<0){
100
u=u+v;
101
}
102
 
103
return u;
104
}
105
 
106
 
107
//Compute Division Function
108
int Division (int n, int m){
109
int answer=0;
110
int temp;
111
 
112
if(n<m){
113
temp=n;
114
n=m;
115
m=temp;
116
}
117
 
118
temp= Modulo(n, m);
119
 
120
n=n-temp;
121
 
122
 
123
while(n>0){
124
n=n-m;
125
answer++;
126
}
127
 
128
if(n<0){
129
answer--;
130
}
131
 
132
 
133
return answer;
134
}
135
 
136
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.