Debugging C Code: Unexpected Output

  • Thread starter Thread starter Quincy
  • Start date Start date
  • Tags Tags
    Output
Click For Summary

Discussion Overview

The discussion revolves around debugging a C code snippet that produces unexpected output when calculating values based on user input for left and right endpoints. Participants explore issues related to input handling, potential infinite loops, and the correct usage of format specifiers in the C programming language.

Discussion Character

  • Technical explanation
  • Debugging
  • Exploratory

Main Points Raised

  • One participant reports unexpected output when inputting values for left and right endpoints, specifically mentioning the output format as "1.910014e-3074.243992e-314".
  • Another participant suggests changing the format specifiers in the scanf function from %f to %lf to correctly read double values.
  • A participant acknowledges fixing the initial problem but encounters a new issue where the output does not display, suspecting an infinite loop.
  • Another participant expresses doubt about the absence of an infinite loop and suggests posting the code directly in the forum for better clarity.
  • One participant identifies a problem in the while loop of the trpzd_method function, indicating it may be the reason for the function not returning as expected.
  • A participant humorously reflects on discovering the issue, noting that it was a mistake discussed in class by their professor.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the cause of the output issues, as some suspect an infinite loop while others focus on format specifier corrections. The discussion remains unresolved regarding the final output behavior of the code.

Contextual Notes

There are limitations in the provided code, such as the potential for an infinite loop and the incorrect handling of increment calculations within the trpzd_method function. These aspects are not fully resolved in the discussion.

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?".
 

Similar threads

Replies
20
Views
4K
Replies
7
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
9
Views
2K