Click here to Skip to main content
16,020,253 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
I want to compute array of r=[1, 2, 3, 4, 5] and then get area of circle, a[5]. What is the bug in my code? Thanks.

What I have tried:

C
#include<stdio.h>
#include<math.h>

int main()
{
	int i,r[i];
	double r,pi,a[5],ar;
	
	double area(r);
	a[5]=area(r);
	
	printf("%.3e",a[5]);

	for (i=0;i<5;i++)
	{
		pi=3.142;		
		r[i]=i+1;
		area=pi*pow(r,2);
		
		return ar
	}

	
	
	return 0;
}
Posted
Updated 6-Dec-16 22:52pm
v4
Comments
StM0n 5-Dec-16 7:42am    
Not quite sure what you expect as output as ar isn't set...
ZurdoDev 5-Dec-16 7:47am    
If you want to know what is wrong, debug it and find out. Simple.
#realJSOP 5-Dec-16 8:32am    
This is NOT a C# question. Pay attention to your tags so you get the appropriate people looking at your question.

Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables after each lines.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
You missed a colon in first return statement.
 
Share this answer
 
There are several bugs in your code. For instance you did't initialize your variables.
Another problem is your area fucntion definition. Try
C
#include <stdio.h>
#include <math.h>


double circle_area(double ray);

int main()
{ 
  double a[5] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
  int i; 
  for (i=0; i<5; ++i)
  {
    printf("ray = %g, area = %g\n", a[i], circle_area(a[i]));
  }
  
  return 0;
}   
    
    
double circle_area(double ray)
{
  return (ray * ray * M_PI); // M_PI is defined in math.h
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900