Hi, first post...I am writing a program for simpsons rule for numerical estimation of integrals, wiki here , and I finally got the code to execute with no errors. Now I am just getting a blank black box with no results. Any help? Thanks in advance.

// hw2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <math.h>

double f_int(double x);
double I_simpson(double a, double b, int n);

int main()
{
  double d = I_simpson(0, 0.05, 4);

  std::cout << d ;
  _getch();


  return 0;

}

double f_int(double x)
{
  // function
  return exp((-x*x)/2); 
}

double I_simpson(double a, double b, int n)
{
	double h = (b-a)/n;
	double I_simpson;
	int i = 1;
	I_simpson = f_int(a)/6 + f_int(b)/6;
	for ( i ; (n-1) ; i++)
		{
		I_simpson = I_simpson + f_int(a + i * h)/3;
	}
	for (i ; n ; i++)
		{ 
		I_simpson = I_simpson + (2/3)*f_int(a + (i - 1/2)*h);
	}
	I_simpson *= h;
	return I_simpson;
}

Just because it compiles without errors doesn't mean the code works. Your I_simpson function is hanging, which means it doesn't return, which means you don't get any output.

First thing I see is using C-style headers and C input. Get rid of all the C headers ( conio.h, stdio.h ) and stick with iostream . With that, don't use _getch() . Use C++ input commands. math.h should be cmath , the C++ version.

Fix that and if still a problem, describe in greater detail what is happening.

Ok I made the changes to the libraries you said and removed getch. I am still having the same problem though. Its going all the way through to launching the program, so I get black dos prompt but nothing shows up except the flashing _ . So like Narue says though there isnt any syntax errors there still a logic error. Just can't pinpoint but its probably in the simpson function

// hw2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <math.h>

double f_int(double x);
double I_simpson(double a, double b, int n);

int main()
{
  double d = I_simpson(0, 0.05, 4);

  std::cout << d ;
  _getch();


  return 0;

}

double f_int(double x)
{
  // function
  return exp((-x*x)/2); 
}

double I_simpson(double a, double b, int n)
{
	double h = (b-a)/n;
	double I_simpson;
	int i = 1;
	I_simpson = f_int(a)/6 + f_int(b)/6;
	for ( i ; (n-1) ; i++)
		{
		I_simpson = I_simpson + f_int(a + i * h)/3;
	}
	for (i ; n ; i++)
		{ 
		I_simpson = I_simpson + (2/3)*f_int(a + (i - 1/2)*h);
	}
	I_simpson *= h;
	return I_simpson;
}

What do you mean by hanging?

Just because it compiles without errors doesn't mean the code works. Your I_simpson function is hanging, which means it doesn't return, which means you don't get any output.

:icon_lol:

I think the problem is that ur code is entering an infinite loop in the I_simpson function.. because in the for loops u didn't put a condition like (i<n-1) for example . also the same thing for the other for loop u just put n. the value of n is always true so it will not go out of the loop.

I think the problem is that ur code is entering an infinite loop in the I_simpson function.. because in the for loops u didn't put a condition like (i<n-1) for example . also the same thing for the other for loop u just put n. the value of n is always true so it will not go out of the loop.

O i see, so for the first loop it should be i<= n-1
and the second should be i<=n
right?

I will try it when I am home later and let you know. Thanks!

OK it works now. Thank you everyone for your help, was just missing the condition engsara mentioned.

One last question though...I'm not sure if it already is giving the answer to enough decimal places, but is there a way to make the program return more? Or is it automatically giving me the most decimal places? I need an error of 10^-8 .

Use some of the methods from iomanip (#include <iomanip>). Pay particular attention to "fixed" and "setprecision," they'll give you what you need.

Beautiful, thank you very much. Program runs great.

And we're back, I have tested the code to an example and the results don't match up.
Heres the pseudocode

h=(b-a)/n
I_simpson = f_int(a)/6 + f_int(b)/6
for i = 1 : (n-1)
I_simpson = I_simpson + f_int(a + ih)/3
end
for i = 1 : n
I_simpson = I_simpson + 2f_int(a+(i-1/2)h/3
end
I_simpson = h*I_simpson

And heres what i got:

// hw2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <iomanip>

double f_int(double x);
double I_simpson(double a, double b, int n);

int main()
{
  double d = I_simpson(0, 2, 4);
  std::cout << std::setprecision (12) << d << std::endl;
  
system ("PAUSE");
  return 0;

}

double f_int(double x)
{
  return exp(-1*(pow(x, 2))); 
}

double I_simpson(double a, double b, int n)
{
	double h = (b-a)/n;
	double I_simpson = 0;
	int i;
	I_simpson = (f_int(a)/6) + (f_int(b)/6);
	for ( i=1 ; i<=(n-1) ; i++)
		{
		I_simpson = I_simpson + f_int(a + (i * h))/3;
	}
	for (i=1 ; i<=n ; i++)
		{ 
		I_simpson = I_simpson + ((2*f_int(a + (i - 1/2)*h))/3);
	}
	I_simpson *= h;
	return I_simpson;
}

>i - 1/2
This raises some red flags. 1/2 is 0 because both operands are integers. i - 0 is probably not what you wanted. You probably wanted the whole expression to be evaluated as floating-point:

i - 1.0 / 2

Wow, that was it. Awesome. Much love. Im still amazed how a minute change causes big change in the result.

This question has already been answered. Start a new discussion instead.