Debugging C Code: Unexpected Output

  • Thread starter Quincy
  • Start date
  • Tags
    Output
In summary, debugging C code involves identifying and fixing errors that lead to unexpected output. This process typically involves using tools such as a debugger or print statements to track the flow of the code and pinpoint the source of the issue. Common causes of unexpected output in C code include logical errors, syntax errors, and memory management issues. It is important to thoroughly test and troubleshoot code to ensure it produces the desired output and functions correctly.
  • #1
Quincy
228
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
  • #2
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:
  • #3
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:
  • #4
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.
 
  • #5
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?".
 

Related to Debugging C Code: Unexpected Output

What is debugging?

Debugging is the process of identifying and fixing errors, or bugs, in computer programs. It involves analyzing code, isolating the cause of unexpected output, and making corrections to ensure the program runs as intended.

Why is my C code producing unexpected output?

There are several possible reasons for unexpected output in C code. These can include errors in logic or syntax, memory leaks, incorrect data types, or issues with input/output operations. Debugging techniques can help identify and resolve these issues.

How do I debug C code?

To debug C code, you can use a debugger tool or debug manually by inserting print statements at strategic points in the code. Analyzing the output and stepping through the code can help identify the source of unexpected output.

What are common debugging techniques for C code?

Common debugging techniques for C code include using a debugger, analyzing the code line by line, stepping through the code, and using print statements. It can also be helpful to isolate the code causing the issue and break it down into smaller parts for easier analysis.

How can I prevent unexpected output in my C code?

To prevent unexpected output in your C code, it is important to write clear and well-structured code, use proper data types, and thoroughly test the program before running it. Additionally, regularly reviewing and debugging your code can help catch and fix any errors before they cause unexpected output.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
20
Views
2K
  • Nuclear Engineering
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
950
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
905
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
Back
Top