program makes no sense to me

This is a discussion on program makes no sense to me within the C Programming forums, part of the General Programming Boards category; Ok, I was searching for a program that can computer pi, well hey brain-**** can do it, C should be ...

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    program makes no sense to me

    Ok, I was searching for a program that can computer pi, well hey brain-**** can do it, C should be able to do it also!
    I found this POS
    Code:
    #include <stdio.h>
    int a=10000,b,c=2800,d,e,f[2801],g;
    
    int main(){
    	for(;b-c;)
    		f[b++]=a/5;
    	for( ; d=0, g=c*2; c-=14 ,printf("%.4d",e+d/a) ,e=d%a)
    		for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);
    	return 0;
    }
    I made some modifications such as #include <stdio.h>!
    but the for loops make no sense!
    for(; <--- WHERE IS THE FIRST VALUE!?
    anyway, if someone could explain what is going on in this program it would be nice. But if you can help me find a better program for calculating pi, that would be better.
    oh, and without the use of math.h's global PI
    thank you.

    --------
    3.14159265358979323...
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The parameters of a for loop are optinal.

    for(a;b;c)

    a == initialization
    b == truth check
    c == incrementation

    Examples:

    for( ; x < 20; )
    {
    ... do something...
    }

    Here, we must have initialized 'x' before we use it in our loop.
    Additionally, our loop must have some way to make the "x<20" check fail.
    Finally, it must have some incrementation, or method of changint "x"; as per the previous sentence.

    for( x = 10; ; x++ )
    {
    }

    Here, we initialze x and we increment x, but we have no check for breaking from the loop. We must assume that some place in the code of the loop then, it has a way to break out.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    thanks, after staring at that program till my head hurt I began to slowly understand it but it still is CRAP to me, but it works!
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    oh, anyway, if you just want to make a variable hold the value of pi, you can simply do this:

    //make sure to include <math.h> or some other math library that supports trig functions

    long double /*float*/ pi = 4*atan(1); //4 times the arc tangent of 1

    or

    long double pi = 2*asin(1); //2 times the arc sine of 1

    or

    long double pi = acos(-1); //arc cosine of -1

    // don't forget that pi is 180 degrees

    here is just a sample list of the first few values of pi.
    Attached Files Attached Files
    think only with code.
    write only with source.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. hOW TO MAKE A PROGRAM WHICH MAKES MY pC TO SHUTDOWN?
    By ALANAIR23 in forum Windows Programming
    Replies: 9
    Last Post: 09-01-2001, 01:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21