C Programmers- Help a noob in C

Code:
#include "mpi.h" 
#include <stdio.h> 
#include <math.h> 
int main( int argc, char *argv[] ) 
{ 
    int n, myid, numprocs, i; 
    double PI25DT = 3.141592653589793238462643; 
    double mypi, pi, h, sum, x; 
    MPI_Init(&argc,&argv); 
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs); 
    MPI_Comm_rank(MPI_COMM_WORLD,&myid); 
    while (1) { 
        if (myid == 0) { 
            printf("Enter the number of intervals: (0 quits) "); 
            scanf("%d",&n); 
        } 
        MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); 
        if (n == 0) 
            break; 
        else { 
            h   = 1.0 / (double) n; 
            sum = 0.0; 
            for (i = myid + 1; i <= n; i += numprocs) { 
                x = h * ((double)i - 0.5); 
                sum += (4.0 / (1.0 + x*x)); 
            } 
            mypi = h * sum; 
            MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, 
		       MPI_COMM_WORLD); 
            if (myid == 0)  
                printf("pi is approximately %.16f, Error is %.16f\n", 
                       pi, fabs(pi - PI25DT)); 
        } 
    } 
    MPI_Finalize(); 
    return 0; 
}

http://www-unix.mcs.anl.gov/mpi/usingmpi/examples/simplempi/cpi_c.htm
 
euph said:
sup enrique, I though you were going to medschool bro
one of my last few requirements for my bachelors in chemistry ;)
then medical school which I am in track for in FIU.

Going to the dyno this sat?


oh and Joemomma5000, my professor would know automaticly this is from that site , he is a geek so knowing him he probably knows that website.
 
gah I hate C.

I think I am stupid cause I can't get it to work here is using the above code for the loop.


PHP:
#include<math.h> 
//main 
int main() 


    { 

	int num;
	int x; 
	double sum;
	double term;	
    int i,s;     
                printf("Pi Approximation Program\n"); 
                printf("How Many terms of the series should be included?\n"); 
                printf("(The more terms, the better the approximation.)\n"); 
             	scanf("%lf",&num);

	

	sum = 0.0;
	term = 1.0;
	s = 1;

	for(i=0;i<num;i++) 
		{
   		sum  = sum + s*(1.0/term);
   	s = -s;
   	term = term+2;
	}

                printf("Approximate value of pi: %lf",&term); 
                return 0; 
        }

I used the program above and I get 0.000 for pie.. am I missing something?
 
i don't remember what %lf does exactly, but I'm pretty sure you want to provide printf with the value rather than the address of the variable (as opposed to scanf, where you want the address of the variable)

*edit*
i don't like to correct logic errors (which is why i didn't respond before)... but you probably want to print out sum instead of term.
 
int's and double's are represented as 2's comp integers..
so they will take on +/- integer values..

pi is a float value, you want to do your computations in floats
 
You're reading num in as a float, but it's an integer (both in type and intention). It's most definitely not going to be the right value.
 
On a side note, you should probably learn to statically debug.. I know you probably wont be doing this for too long but it will help you throughout the remainder of your course...

Basically, static debugging consists of using printf's to display variables in specific spots in your program. You are kind of "guessing" where the variables are correct and where they are not.. you are verifying at which point in your control flow they are correct.

For example, a good place to do it here, since your sum comes out to 0.000 is in your for loop, like this:

for(i=0;i<num;i++)
{


sum = sum + s*(1.0/term);
s = -s;
term = term+2;
printf("iteration:%d\tsum:%f\n", i, sum );
}

this way you will have a decent idea of what is going on. What you're really looking at is the printf value, so your problem could be in your for loop, or in your printf, or somewhere else - in this case it happens to be your variable declarations - but you should get accustomed to searching for the problem. The best way I've heard someone describe this is to "verify what you think is correct in your program using a binary search method". If you don't know what a binary search is, then google.
 
ok so on the printf print out &sum

But as far as the logic , I got to change the scanf's floats? huh?


just shoot me this program sux and I wish I never had to take it.
 
no, on printf just print sum, not &sum (& gives the address of sum). for the scanf, you want &num (so scanf knows where to put the input), but you don't want to use %lf, you want to use %d (see this for a small example)
 
it works now I guess the logic before the loop is worng because its definitely not a pie number.


btw thanks for that example that helps a shitload.
 
Okay, you need to change your data types. When you declare your variables, you need to tell the computer what type of data it is. That's why there are chars, ints, doubles, floats, etc.

ints are integers. I.e. whole numbers. I.e. ....-3, -2, -1, 0, 1, 2, 3....
doubles are integers that have a larger capacity. They use more bits, so they can hold larger numbers, but they still just hold integers.

floating point data values are the only number type in C that can represent real numbers. They are approximations of real numbers using a certain number of bits.

All of your data types are just a string of bits interpreted in one way or another. Integers are stored as base 2 numbers, just like a decimal number is base 10 - so if you are storing the value 5 in decimal, it's stored as 0101 in binary ( 0*2^3 + 1*2^2 + 0*2^1 + 1*2^0 ). Floating point numbers aren't represented this way, because you are also including digits past a decimal point. You will probably learn the floating point representation eventually in your course; for now, just know that if you ever want to do a fraction or an irrational number, that you need to use a float.

i.e.

int main(){
float x;
float = 3.245235;
}

or so
 
Back
Top