Maximize area of a triangle given n points [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
The code runs perfectly when I arbitrarily pass arguments to the area function. But gives me a segmentation fault when I try to run the loops. Given n<100
Here's my code.
#include<stdio.h>
#include<math.h>
double area(int x,int y,int x1,int y1,int x2,int y2)
{ //Heron's formula
double a,b,c,s;
double abc;
a=sqrt(((x-x1)*(x-x1))+((y-y1)*(y-y1)));
b=sqrt(((x-x2)*(x-x2))+((y-y2)*(y-y2)));
c=sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
s=(a+b+c)/2;
abc=sqrt(s*(s-a)*(s-b)*(s-c));
return(abc);
}
int main(void)
{
int n,i,j,k;
double max=0,z=0;
scanf("%d",&n);
int x[100]={},y[100]={};
for(i=0;i<n;i++)
{
scanf("%d %d",&x[i],&y[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
for(k=j+1;k<n;j++)
{
z=area(x[i],y[i],x[j],y[j],x[k],y[k]);
printf("%lf\n",z);
if(z>max)
{
max=z;
}
}
}
}
//printf("\n%lfoi\n",area(0,0,1,0,1,2));
printf("%lf",max*2);
}

The inner loop is incrementing j, it should probably increment k:
for(k=j+1;k<n;j++)
^
oops!

there is an error index in inner loop: should be
for(k=j+1;k<n;k++)
instead of
for(k=j+1;k<n;j++)

for(k=j+1;k<n;j++)
I think you meant to increment k in this loop, not j.

You have to increase "k" not "j"
for(k=j+1;k<n;k++)
{ }

Related

Error in a recursive function to print out a series [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
So, I am supposed to print out the following series using a C program:
I decide to use a recursive approach and come up with this code:
#include<stdio.h>
float series(int n, float x)
{
float prod;
if(n==1)
return 1;
else
{
prod = (x*x)/((2*n-2)(2*n-3)); //line 10
return prod*series(n-1,x);
}
}
int main()
{
int n;
float x;
printf("\n Enter the values of n and x : ");
scanf("%d %f",&n,&x);
printf("\n The series is :");
for(int i=1;i<=n;i++)
printf(" %f,",series(i,x));
printf("\n\n");
return 0;
}
But this gives an error on line 10:
error: called object type 'int' is not a function or function pointer
I don't see any syntactical error on the line. It would be great if you could point it out.
Thank You!
prod = (x*x)/((2*n-2)(2*n-3)); //line 10
should be
prod = (x*x)/((2*n-2)*(2*n-3)); //line 10
The compiler sees this as a function call where 2*n-2 is the function pointer and 2*n-3 is the argument.

Spare Matrix Function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
(Write a C function that should check if a matrix (4x5) is sparse or not. Knowing that: sparse matrix is a matrix that has zeros more than the half of its size.)
That's a problem for a sheet in out subject
here is my code:
#include <stdio.h>
#include <stdlib.h>
int Spare(int [4][5]);
int main()
{
int arr[4][5];
int m,n;
for(m=0;m<4;m++){
for(n=0;n<5;n++){
scanf("%d ",&arr[4][5]);
}
}
Spare(arr[4][5]);
return 0;
}
int Spare(int Arr[4][5]){
int i,j;
int zerocount=0;
for(i=0;i<4;i++){
for(j=0;j<5;j++){
if(Arr[i][j]==0){
zerocount++;
}
}
}
if(zerocount>=10) return 1;
else return 0;
}
Its Running but after the user enter inputs of array it stops working!
Any Help Guys?
Change scanf("%d",&arr[4][5]) into scanf("%d",&arr[m][n]). You are just storing the input in arr[4][5] everytime in the loop.
The Spare(arr[4][5]) pass the element of fifth row and sixth column only which doesn't exist.
The Spare function expects an array as parameter and you are passing the single element only.
The function call must be done in this way: Spare(arr)

Program working fine for smaller values but throwing Segmentation Fault for bigger values [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
In the following program when the value of N is less than 100 the program is executing perfectly but for bigger values of N its showing segmentation fault.I sit because of less memory or anything wrong with program??
#include<stdio.h>
int main()
{
int N,iteration,MAX_ITERATONS;
int i,j,k,n,index,boundary1,boundary2;
double h[2][100][100];
int current = 0;
int next = 1;
printf("Enter the number of points\n ");
scanf("%d", &N);
boundary1=0.4*N;
boundary2=(0.6*N);
printf("The boundary is between %d and %d .\n",boundary1,boundary2);
for(k=0;k<2;k++)
{
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if((i==0)&&(j>=boundary1&&j<boundary2))
h[k][i][j]=100;
else
h[k][i][j]=20;
}
}
}
printf("Initial Values\n");
index = N/10;
for(i=0;i<N;)
{
for(j=0;j<N;)
{
printf("%.2f\t",h[0][i][j]);
j=j+index;
}
i=i+index;
printf("\n");
}
}
When N > 100, h is accessed to an index greater than 100, inside the nested for loop
h[k][i][j]=100;
but h is defined as
double h[2][100][100];
You are going out of bounds for h
If you want N as greater than 100 you need to redefine h or malloc it.

printing too much stars [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I type this code on Ubuntu 14.04
#include<stdio.h>
int main() {
int i, j, n;
scanf("%",&n);
for (i=0;i<=n;i++){
for (j=0;j<=i;j++){
printf("*");
}
printf("\n");
}
return 0;
}
but it print too much stars and it Continue until I close it.
Should be:
#include<stdio.h>
int main() {
int i, j, n;
scanf("%d",&n);
for (i=0;i<=n;i++){
for (j=0;j<=i;j++){
printf("*");
}
printf("\n");
}
return 0;
}
You forgot to tell scanf you were reading in an integer by using the %d argument
There is mistake in
scanf("%",&n);
it will be:
scanf("%d",&n);
to tell the complier that the value of n is an integer type.

Why won't a 5x5 multiplication array appear? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I did this and I think it should work:
#include <stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++);
{
for(j=1;j<=5;j++);
{
printf("%d",i*j);
}
}
return 0;
}
But it just prints out 36...
What am I doing wrong?
Remove the two extra ;s:
for(i=1;i<=5;i++);
^
{
for(j=1;j<=5;j++);
^
As others have pointed out, the semicolons before the opening braces of your for loops are preventing the printing.
Additionally, if you want the output to appear as a matrix, you'll want to printf a newline after your first for loop:
#include <stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
printf("%d",i*j);
}
printf("\n");
}
return 0;
}

Resources