Debugging C Code: Unexpected Output

  • Thread starter Thread starter Quincy
  • Start date Start date
  • Tags Tags
    Output
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
4 replies · 2K views
Quincy
Messages
228
Reaction score
0
http://pastebin.com/d49e23fad --
I input 0.0 for the left endpoint and 100.0 for the right endpoint, yet it printed "1.910014e-3074.243992e-314"... What is going on??
 
Last edited by a moderator:
Physics news on Phys.org
Quincy said:
http://pastebin.com/d49e23fad --
I input 0.0 for the left endpoint and 100.0 for the right endpoint, yet it printed "1.910014e-3074.243992e-314"... What is going on??

Code:
int main()
{
	int sub_intrvl;
	double left_ep;
	double right_ep;
	double answer;
	printf("Number of Subintervals: ");
	scanf("%d", & sub_intrvl);
	printf("Left endpoint: ");
	scanf("%f", & left_ep);
	printf("Right endpoint: ");
	scanf("%f", & right_ep);
	printf("%e", right_ep);
	printf("%e", left_ep);
}

I would change the %f conversion specifiers in scanf() to %lf, since you're reading in a double value.
 
Last edited by a moderator:
Mark44 said:
Code:
int main()
{
	int sub_intrvl;
	double left_ep;
	double right_ep;
	double answer;
	printf("Number of Subintervals: ");
	scanf("%d", & sub_intrvl);
	printf("Left endpoint: ");
	scanf("%f", & left_ep);
	printf("Right endpoint: ");
	scanf("%f", & right_ep);
	printf("%e", right_ep);
	printf("%e", left_ep);
}

I would change the %f conversion specifiers in scanf() to %lf, since you're reading in a double value.

Thanks, I got that problem fixed. But now, when i input everything, the output doesn't display for some reason. I thought I had an infinite loop, but that doesn't seem to be an issue. http://pastebin.com/m32ac3107
 
Last edited by a moderator:
I think you might have an infinite loop, despite what you are saying.

BTW, why not post your code here? Just put it between a [ code] tag and the beginning and a [ /code] tag at the end -- without the leading spaces.

Code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

double trpzd_method(int, double, double);
double f(double);
int main()
{
	int sub_intrvl;
	double left_ep;
	double right_ep;
	double answer;
	printf("Number of Subintervals: ");
	scanf("%d", & sub_intrvl);
	printf("Left endpoint: ");
	scanf("%lf", & left_ep);
	printf("Right endpoint: ");
	scanf("%lf", & right_ep);
	answer = trpzd_method(sub_intrvl, left_ep, right_ep);
	printf("%lf", answer);
	system("PAUSE");
	
}
double trpzd_method(int sub_intrvl, double left_ep, double right_ep)
{
	double total = 0.0;
	double final_result;
	double first_value = f(left_ep);
	double last_value = f(right_ep);
	double increment = (right_ep - left_ep)/sub_intrvl;
	while((left_ep + increment) <= right_ep);
	{
		total += f(right_ep + increment);
		increment = (increment + ((right_ep - left_ep)/sub_intrvl));
	}
	final_result = increment*((0.5*(first_value + last_value)) + total);
	return final_result;

}
double f(double x)
{
	double e = 2.71828182845904523536;
	double exponent = (-1)*(x)*(x);
	double result = pow(e, exponent);
	return result;
}

From your description, trpzd_method() isn't returning, and I can see exactly why. Take a very close look at your while loop.
 
Mark44 said:
From your description, trpzd_method() isn't returning, and I can see exactly why. Take a very close look at your while loop.

Thanks, I found the problem. The funny thing is, the professor was just going over that today, and I was thinking "lol who would make that mistake?".