Hey guys,

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!
Double posting 'cus I got it working

here's the working code [and formatted]:

Code:
////////////////////////////////////////
// { prgmCALCPI } { v1.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 */
int main(void) {
    char one[18] = "Total Iterations:";
    char two[11] = "Max Value:";
    int a;
    int i;
    float c = 1.;
    int m;
    float f, g;
    float p;
    char inputBuffer[INPUT_SIZE];
    char * ptr;
    char astr[9], pstr[9];

    os_ClrHomeFull();
    os_GetStringInput(one, inputBuffer, INPUT_SIZE);
    i = strtod(inputBuffer, & ptr);
    os_NewLine();

    os_GetStringInput(two, inputBuffer, INPUT_SIZE);
    m = strtod(inputBuffer, & ptr);
    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, 0, 3);
        sprintf(astr, "%d", a);
        printText(astr, 2, 4);
    }
    while (!os_GetCSC());
    os_ClrHomeFull();

    return 0;
}
/* 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, 8, 1, 8);
}


f&g weren't floats so the modulo operation(s) in getGcd() didn't compute how they were supposed to.


EDIT :
NEW VERSION

Code:
////////////////////////////////////////
// { prgmCALCPI } { v1.3 }
// 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>
#include "izder456.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 */
int main(void) {
    char one[18] = "Total Iterations:";
    char two[11] = "Max Value:";
    int a;
    int i;
    float c = 1.;
    int m;
    float f, g;
    float p;
    char inputBuffer[INPUT_SIZE];
    char * ptr;
    char astr[9], pstr[9];
    char prompt[] = "";

    os_ClrHomeFull();
    prompt[0] = 0;
    printText(one, 0, 0);
    os_GetStringInput(prompt, inputBuffer, INPUT_SIZE);
    i = strtol(inputBuffer, & ptr, 0);

    prompt[0] = 0;
    printText(two, 0, 1);
    os_GetStringInput(prompt, inputBuffer, INPUT_SIZE);
    m = strtol(inputBuffer, & ptr, 0);


    printText("Estimate of pi:", 0, 2);
    printText("I=", 0, 4);
    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, 0, 3);
        sprintf(astr, "%d", a);
        printText(astr, 2, 4);
    }
    while (!os_GetCSC());
    os_ClrHomeFull();

    return 0;
}


this update [ver 1.3] fixes the error where the "max value" string would be printed too far down, getting in the way of the output. This was caused by the os_NewLine() function being called to much, so I replaced it with the print text function, also there is a new include file, izder456.h just to keep it neat and tidy.

INCLUDE FILE :

Code:
/* 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>

/* 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, 1, 9);
}
Don't put functions in headers, or variables in headers. Put function prototypes in headers. Otherwise they may be compiled multiple times into your binary, depending on which source files include them. Keep in mind each source file is compiled independently of each other.
MateoConLechuga wrote:
Don't put functions in headers, or variables in headers. Put function prototypes in headers. Otherwise they may be compiled multiple times into your binary, depending on which source files include them. Keep in mind each source file is compiled independently of each other.

so something like this?

Code:
////////////////////////////////////////
// { prgmCALCPI } { v1.3 }
// 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>
#include "izder456.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); <-- is this what you mean mateo?
//void float2str(float value, char * str);
//int getGcd(int n1, int n2);

#define INPUT_SIZE 10

/* Put all your code here */
int main(void) {
    char one[18] = "Total Iterations:";
    char two[11] = "Max Value:";
    int a;
    int i;
    float c = 1.;
    int m;
    float f, g;
    float p;
    char inputBuffer[INPUT_SIZE];
    char * ptr;
    char astr[9], pstr[9];
    char prompt[] = "";

    os_ClrHomeFull();
    prompt[0] = 0;
    printText(one, 0, 0);
    os_GetStringInput(prompt, inputBuffer, INPUT_SIZE);
    i = strtol(inputBuffer, & ptr, 0);

    prompt[0] = 0;
    printText(two, 0, 1);
    os_GetStringInput(prompt, inputBuffer, INPUT_SIZE);
    m = strtol(inputBuffer, & ptr, 0);


    printText("Estimate of pi:", 0, 2);
    printText("I=", 0, 4);
    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, 0, 3);
        sprintf(astr, "%d", a);
        printText(astr, 2, 4);
    }
    while (!os_GetCSC());
    os_ClrHomeFull();

    return 0;
}


edit: forgot to format as code in phpbb
I don't understand what you posted.

Also make sure you put your custom headers at the top before all the standard headers.
MateoConLechuga wrote:
I don't understand what you posted.

Also make sure you put your custom headers at the top before all the standard headers.


I'm just gonna put it all in one main.c again...
Here’s some code I have that estimates PI using random numbers.


Code:

0→T:0→B
ClrHome
Lbl Z
rand*100→X
rand*100→Y
If X*X+Y*Y≤10000:B+1→B
T+1→T
B/T*4→P
Output(5,9,P)
Output(9,13,T)
Goto Z
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are GMT - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement