Click here to Skip to main content
16,020,343 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
#include<stdio.h>

int main()
{
	int i,r[i];
	double a[20];

	for (i=0;i<20;i++)
	{		
		r[i]=i+1;
		a[20]=area(r);
		printf("%.3e",a[20]);	
	}

	return 0;
}

double area(int r[int i])
	{
		double pi,area;
		
		pi=3.142;
		area=pi*pow(r[i],2);
		return area;
	}	


What I have tried:

C#
#include<stdio.h>

int main()
{
	int i,r[i];
	double a[20];

	for (i=0;i<20;i++)
	{		
		r[i]=i+1;
		a[20]=area(r);
		printf("%.3e",a[20]);	
	}

	return 0;
}

double area(int r[int i])
	{
		double pi,area;
		
		pi=3.142;
		area=pi*pow(r[i],2);
		return area;
	}	
Posted
Updated 6-Dec-16 4:29am
Comments
[no name] 6-Dec-16 11:23am    
And in addition to Solution 1 and 2, you really need to learn how to ask a question. There isn't an actual question anywhere in your posting. This is simply a code dump and we aren't a "debug my homework assignment for me" service.

You must really learn how arrays are used in C programming language. See, for instance Arrays in C[^].

Moreover you have to master functions, see C Functions[^].

Try

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

double area(int r);

int main()
{
  int i, r;
  double a[20];

  for (i = 0; i < 20; i++)
  {
    r = i + 1;
    a[i] = area(r);
    printf("%.3e\n",a[i]);
  }

  return 0;
}

double area(int r)
{
    double pi, result;

    pi = 3.142;
    result = pi * pow (r, 2);
    return result;
}
 
Share this answer
 
Just by reading, I see 4 errors here.
- You need to learn about function prototype
- Array are not declared this way
C++
int i,r[i];

- The function call and the function parameter do nit match
C++
a[20]=area(r);
...
double area(int r[int i])


Solution 1 already gives you corrections. So 2 advices Learn debugger and learn properly C++.
-----
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute.

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.

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.
-----
Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]
 
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