Hey guys,
I am workin' on a C program the estimates pi using random integers and a gcd function I created.
Code:
Currently, the code prints, asks for variables and then flashes the var a and the estimate for a split second, then quits.
I expect it to keep printing an estimate over itself and iterate a and print that after "I="
I'm giving 100 for i and 10 for m
Do any of you's know what the problem may be?
Here's a link To the PB
https://tiplanet.org/pb/?id=249849_1584975690_baaef42163
EDIT :
Code:
I fixed the strtol() functions as jacobly mentioned, but there is a new problem.
now it does everything as above mentioned, but it no longer flashes the a value and the estimate. It still quits right away.
Here's the TI-BASIC equivalent of what I want it to do :
Code:
EDIT 2 :
Code:
I optimised the getGcd() function to be a smaller byte size, by using recursion.
A new problem is here though, the estimate kind of works
by that I mean, it alternates through a select few numbers as the estimate.
The estimate should change a lot in the start as c is small [denominator]
As c grows [when gcd(f,g)==1] there should be a lot less change in the estimate [becoming more precise].
^^this may be a problem in llvm, bc the other was caused by llvm, as pointed out by jacobly.
cheers!
I am workin' on a C program the estimates pi using random integers and a gcd function I created.
Code:
////////////////////////////////////////
// { prgmCALCPI } { a0.1 }
// Author: izder456
// License: n/a
// Description: calculates pi
////////////////////////////////////////
/* Keep these headers */
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <tice.h>
/* Standard headers */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Other available headers: stdarg.h, setjmp.h, assert.h, ctype.h, float.h, iso646.h, limits.h, errno.h */
/* uint8_t is an unsigned integer that can range from 0-255. */
/* It performs faster than just an int, so try to use it (or int8_t) when possible */
/* Put function prototypes here */
void printText(const char * text, uint8_t x, uint8_t y);
void float2str(float value, char * str);
int getGcd(int8_t n1, int8_t n2);
#define INPUT_SIZE 10
/* Put all your code here */
void main(void) {
char one[18] = "Total Iterations:";
char two[11] = "Max Value:";
int a;
int8_t i = 0;
int8_t c = 1;
int8_t m = 0;
int8_t f,g;
float p;
char inputBuffer[INPUT_SIZE];
char * ptr;
char astr[9],pstr[9];
os_ClrHomeFull();
os_GetStringInput(one, inputBuffer, INPUT_SIZE);
strtol(inputBuffer, &ptr, i);
os_NewLine();
os_GetStringInput(two, inputBuffer, INPUT_SIZE);
strtol(inputBuffer, &ptr, m);
os_NewLine();
os_PutStrFull("Estimate of pi:");
os_NewLine();
os_NewLine();
os_PutStrFull("I=");
for (a=0; a<=i; a++)
{
f = randInt(0,m);
g = randInt(0,m);
if(getGcd(f,g) == 1) {
c++;
}
p = sqrt(6*a/c);
float2str(p, pstr);
printText(pstr, 1,4);
sprintf(astr, "%d", a);
printText(astr, 3,5);
}
}
/* Draw text on the homescreen at the given X/Y cursor location */
void printText(const char *text, uint8_t xpos, uint8_t ypos)
{
os_SetCursorPos(ypos, xpos);
os_PutStrFull(text);
}
/* get the gcd of two numbers */
int getGcd(int8_t n1, int8_t n2)
{
int r;
int8_t gcd;
for(r=1; r <= n1 && r <= n2; ++r)
{
// Checks if i is factor of both integers
if(n1%r==0 && n2%r==0)
gcd = r;
}
return gcd;
}
/* convert float to string */
void float2str(float value, char * str) {
real_t tmp_real = os_FloatToReal(value);
os_RealToStr(str, & tmp_real, 8, 1, 10);
}
Currently, the code prints, asks for variables and then flashes the var a and the estimate for a split second, then quits.
I expect it to keep printing an estimate over itself and iterate a and print that after "I="
I'm giving 100 for i and 10 for m
Do any of you's know what the problem may be?
Here's a link To the PB
https://tiplanet.org/pb/?id=249849_1584975690_baaef42163
EDIT :
Code:
////////////////////////////////////////
// { prgmCALCPI } { a0.2 }
// Author: izder456
// License: n/a
// Description: calculates pi
////////////////////////////////////////
/* Keep these headers */
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <tice.h>
/* Standard headers */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Other available headers: stdarg.h, setjmp.h, assert.h, ctype.h, float.h, iso646.h, limits.h, errno.h */
/* uint8_t is an unsigned integer that can range from 0-255. */
/* It performs faster than just an int, so try to use it (or int8_t) when possible */
/* Put function prototypes here */
void printText(const char * text, uint8_t x, uint8_t y);
void float2str(float value, char * str);
int getGcd(int8_t n1, int8_t n2);
#define INPUT_SIZE 10
/* Put all your code here */
void main(void) {
char one[18] = "Total Iterations:";
char two[11] = "Max Value:";
int a;
int8_t i;
int8_t c = 1;
int8_t m;
int8_t f,g;
float p;
char inputBuffer[INPUT_SIZE];
char * ptr;
char astr[9],pstr[9];
os_ClrHomeFull();
os_GetStringInput(one, inputBuffer, INPUT_SIZE);
i = strtol(inputBuffer, &ptr, 0);
os_NewLine();
os_GetStringInput(two, inputBuffer, INPUT_SIZE);
m = strtol(inputBuffer, &ptr, 0);
os_NewLine();
os_PutStrFull("Estimate of pi:");
os_NewLine();
os_NewLine();
os_PutStrFull("I=");
for (a=0; a<=i; a++)
{
f = randInt(0,m);
g = randInt(0,m);
if(getGcd(f,g) == 1) {
c++;
}
p = sqrt(6*a/c);
float2str(p, pstr);
printText(pstr, 1,4);
sprintf(astr, "%d", a);
printText(astr, 3,5);
}
}
/* Draw text on the homescreen at the given X/Y cursor location */
void printText(const char *text, uint8_t xpos, uint8_t ypos)
{
os_SetCursorPos(ypos, xpos);
os_PutStrFull(text);
}
/* get the gcd of two numbers */
int getGcd(int8_t n1, int8_t n2)
{
int r;
int8_t gcd;
for(r=1; r <= n1 && r <= n2; ++r)
{
// Checks if i is factor of both integers
if(n1%r==0 && n2%r==0)
gcd = r;
}
return gcd;
}
/* convert float to string */
void float2str(float value, char * str) {
real_t tmp_real = os_FloatToReal(value);
os_RealToStr(str, & tmp_real, 8, 1, 10);
}
I fixed the strtol() functions as jacobly mentioned, but there is a new problem.
now it does everything as above mentioned, but it no longer flashes the a value and the estimate. It still quits right away.
Here's the TI-BASIC equivalent of what I want it to do :
Code:
1->C
ClrHome
Input "Total Iterations:",I
Input "Max Value:",M
Disp "Estimate of greek_pi"
Disp ""
Disp "I="
For(A,0,I)
randInt(0,M)->F
randInt(0,M)->G
If gcd(F,G)=1 // this line is the getGcd function as in the c program
C+1->C
sqrt(6A/C)->P
Output(4,1,P)
Output(5,3,A)
End
EDIT 2 :
Code:
////////////////////////////////////////
// { prgmCALCPI } { b0.1 }
// Author: izder456
// License: n/a
// Description: calculates pi
////////////////////////////////////////
/* Keep these headers */
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <tice.h>
/* Standard headers */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Other available headers: stdarg.h, setjmp.h, assert.h, ctype.h, float.h, iso646.h, limits.h, errno.h */
/* uint8_t is an unsigned integer that can range from 0-255. */
/* It performs faster than just an int, so try to use it (or int8_t) when possible */
/* Put function prototypes here */
void printText(const char * text, uint8_t x, uint8_t y);
void float2str(float value, char * str);
int getGcd(int n1, int n2);
#define INPUT_SIZE 10
/* Put all your code here */
void main(void) {
char one[18] = "Total Iterations:";
char two[11] = "Max Value:";
int a;
int i;
int c = 1;
int m;
int f,g;
float p;
char inputBuffer[INPUT_SIZE];
char * ptr;
char astr[9],pstr[9];
os_ClrHomeFull();
os_GetStringInput(one, inputBuffer, INPUT_SIZE);
i = strtol(inputBuffer, &ptr, 0);
os_NewLine();
inputBuffer[0] = 0;
os_GetStringInput(two, inputBuffer, INPUT_SIZE);
m = strtol(inputBuffer, &ptr, 0);
os_NewLine();
os_PutStrFull("Estimate of pi:");
os_NewLine();
os_NewLine();
os_PutStrFull("I=");
for (a=0; a<=i; a++)
{
f = randInt(0,m);
g = randInt(0,m);
if(getGcd(f,g) == 1) {
c=c+1;
}
p = sqrt((6*a)/c);
float2str(p, pstr);
printText(pstr, 0,3);
sprintf(astr, "%d", a);
printText(astr, 2,4);
}
while (!os_GetCSC());
os_ClrHomeFull();
}
/* Draw text on the homescreen at the given X/Y cursor location */
void printText(const char *text, uint8_t xpos, uint8_t ypos)
{
os_SetCursorPos(ypos, xpos);
os_PutStrFull(text);
}
/* get the gcd of two numbers */
int getGcd(int n1, int n2)
{
if (n2 != 0)
return getGcd(n2, n1 % n2);
else
return n1;
}
/* convert float to string */
void float2str(float value, char * str) {
real_t tmp_real = os_FloatToReal(value);
os_RealToStr(str, & tmp_real, 9, 0, 9);
}
I optimised the getGcd() function to be a smaller byte size, by using recursion.
A new problem is here though, the estimate kind of works
by that I mean, it alternates through a select few numbers as the estimate.
The estimate should change a lot in the start as c is small [denominator]
As c grows [when gcd(f,g)==1] there should be a lot less change in the estimate [becoming more precise].
^^this may be a problem in llvm, bc the other was caused by llvm, as pointed out by jacobly.
cheers!