C: Find element in the array

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1092 Views - Last Post: 06 April 2015 - 01:17 PM Rate Topic: -----

#1 gruba  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 86
  • Joined: 13-February 15

C: Find element in the array

Posted 06 April 2015 - 04:43 AM

I need help with this C program:

How to find triangle in an array of n triangles which has the largest area? Thanks for replies.

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

typedef struct
{
    double x,y;
}TRIANGLE;

void read(TRIANGLE *tr)
{
    printf("x1=");scanf("%lf",&tr->x);
    printf("x2=");scanf("%lf",&tr->x);
    printf("x3=");scanf("%lf",&tr->x);
    printf("y1=");scanf("%lf",&tr->y);
    printf("y2=");scanf("%lf",&tr->y);
    printf("y3=");scanf("%lf",&tr->y);
}

int main(int argc,char *argv[])
{
  int i;int n;TRIANGLE *array;
  do
  {
      printf("n=");scanf("%d",&n);
  }
  while(n<1);
  niz=(TRIANGLE *)malloc(n * sizeof(TRIANGLE));
  for(i=0;i<n;i++)
  {
      printf("data about %d. triangle:\n",i+1);read(array+i);
  }
  /*search*/
  
  free(niz);
  return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: C: Find element in the array

#2 ndc85430  Icon User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 703
  • View blog
  • Posts: 2,877
  • Joined: 13-June 14

Re: C: Find element in the array

Posted 06 April 2015 - 05:32 AM

First of all, do you think your struct is correct? You've said that a triangle contains two doubles, x and y. However, in your read() function, you're reading several values for each of x and y and each time you do so, you're going to overwrite the previous one.

As for the question in your post, how do you think you could do that? I'm not talking about how to do it in code, I just want the steps that you would take to find the answer.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6010
  • View blog
  • Posts: 20,662
  • Joined: 05-May 12

Re: C: Find element in the array

Posted 06 April 2015 - 05:34 AM

Does your code even compile? I must be going blind, but I don't seem to see where niz is declared anywhere.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon

Reputation: 5390
  • View blog
  • Posts: 16,810
  • Joined: 25-December 09

Re: C: Find element in the array

Posted 06 April 2015 - 05:42 AM

And stop putting multiple statements on one line, this will make troubleshooting much more difficult when you start using a debugger.

   // Don't do this:
   printf("x1=");scanf("%lf",&tr->x);

   // Keep statements on their own lines.
   printf("x1=");
   scanf("%lf",&tr->x);



Also you shouldn't be casting the return value from malloc(). This can mask several potential problems.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 zakhrov  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 40
  • Joined: 23-October 13

Re: C: Find element in the array

Posted 06 April 2015 - 09:43 AM

Your triangle struct has not been defined correctly.
You will need x and y values for all 3 verticies it should be:
typedef struct
{
   double x1,x2,x3,y1,y2,y3;
}TRIANGLE;
//Then your read function
void read(TRIANGLE *tr)
{
  printf("X1=");
  scanf("%lf",tr->x1);
printf("X2=");
  scanf("%lf",tr->x2);
printf("X3=");
  scanf("%lf",tr->x3);
printf("y1=");
  scanf("%lf",tr->y1);
printf("y2=");
  scanf("%lf",tr->y2);
printf("y3=");
  scanf("%lf",tr->y3);
}


What formula are you using to compute the area of the triangle?
If you are using the 0.5*base*height formula your struct needs only the base and height variables.
If you are using Hero's formula [ sqrt(s*(s-a)*(s-B)*(s-c)) ] its easier to specifiy the length of each side instead of computing it from vertex values.

This post has been edited by zakhrov: 06 April 2015 - 09:48 AM

Was This Post Helpful? 1
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: C: Find element in the array

Posted 06 April 2015 - 09:48 AM

Better still would be to create a Point struct.

struct Point {
    double x;
    double y;
};

struct Triangle {
    struct Point vertices[3];
};

Was This Post Helpful? 1
  • +
  • -

#7 zakhrov  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 40
  • Joined: 23-October 13

Re: C: Find element in the array

Posted 06 April 2015 - 10:41 AM

Actually the OP wants to find the triangle with the largest area from an array of triangles.
Are you doing a graphics program that requires point values?
If it is a simple console application you dont need the inidividual vertices. Just the base and height values.
Something like this:
typdef struct
{
  double base=0;heght=0;//init values. the read() will overwrite them
  double area=0.5*base*height;//calculate the area in the struct itself
}TRIANGLE;
void read(TRIANGLE *tr)
{
  printf("Enter base");
  scanf("%lf",tr->base);
  printf("Enter height");
  scanf("%lf",tr->height);
}
//Then to find the area we just search for the largest area value
int main(char argc, char * argv[])
{
   int i,n,TRIANGLE *array[10];
   double area;
   for(i=0;i<n;i++)
   {
	printf("data about %d. triangle:\n",i+1);
        read(array[i]);
   }
   area=array[0]->area;
   for(i=0;i<n;i++) 
   {
	if(area<array[i]->area)
        {
           area=array[i]->area;
        }
        else
        {
          printf("largest area = %lf",area);
         }
        
   }



Was This Post Helpful? 1
  • +
  • -

#8 zakhrov  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 40
  • Joined: 23-October 13

Re: C: Find element in the array

Posted 06 April 2015 - 11:07 AM

woops my code was wrong. Sorry kinda confused between C and C++
#include <stdio.h>

typedef struct
{
      double base;
      double heght;//init values. the read() will overwrite them

}TRIANGLE;


void read(TRIANGLE *tr)
{
    printf("base= \n");
    scanf("%lf",tr->base);
    printf("height= \n");
    scanf("%lf",tr->heght);
}

int main(void)
{
    int i;
    TRIANGLE * array=(TRIANGLE *)malloc(sizeof(TRIANGLE));

    for(i=0;i<10;i++)
    {
        read(array + i);
    }
    double area=0.5*(array->base*array->heght);
    for(i=0;i<10;i++)
    {
        if(area<0.5*(array->base+i)*(array->heght+i))
        {
            area=0.5*(array->base+i)*(array->heght+i);
        }
        else
        {
            printf("Area = %lf",area);
        }
    }
    free(array);
    
    return 0;
}




Well something like that

This post has been edited by zakhrov: 06 April 2015 - 11:16 AM

Was This Post Helpful? 1
  • +
  • -

#9 gruba  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 86
  • Joined: 13-February 15

Re: C: Find element in the array

Posted 06 April 2015 - 11:45 AM

@zakhrov

Thanks. I only need to manipulate with coordinates of points:

#include <stdio.h>
#include <stdlib.h>


typedef struct
{
    double x1,y1,x2,y2,x3,y3;
}TRIANGLE;

void read(TRIANGLE *);
void read(TRIANGLE *tr)
{
    printf("x1:");scanf("%lf",&tr->x1);
    printf("y1:");scanf("%lf",&tr->y1);
    printf("x2:");scanf("%lf",&tr->x2);
    printf("y2:");scanf("%lf",&tr->y2);
    printf("x3:");scanf("%lf",&tr->x3);
    printf("y3:");scanf("%lf",&tr->y3);
}

int main()
{
    int i,n;TRIANGLE *array;
    do
    {
        printf("n=");scanf("%d",&n);
    }
    while(n<1);
    array=(TRIANGLE *)malloc(n * sizeof(TRIANGLE));
    for(i=0;i<n;i++)
    {
        printf("points of %d. triangle:\n",i+1);read(array+i);
    }
    free(array);
    return 0;
}


I will solve it myself, thanks again.
Was This Post Helpful? 0
  • +
  • -

#10 zakhrov  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 40
  • Joined: 23-October 13

Re: C: Find element in the array

Posted 06 April 2015 - 11:47 AM

How do you plan to calculate the area of the triangle from just the point coordinates?
Is this part of a graphics program?

This post has been edited by zakhrov: 06 April 2015 - 11:48 AM

Was This Post Helpful? 0
  • +
  • -

#11 gruba  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 86
  • Joined: 13-February 15

Re: C: Find element in the array

Posted 06 April 2015 - 12:02 PM

@zakhrov

If (x1,y1),(x2,y2),(x3,y3) are the vertices of a triangle then its area is given by

abs(1/2*(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2)))


@zakhrov

It isn't graphics program.
Was This Post Helpful? 0
  • +
  • -

#12 zakhrov  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 40
  • Joined: 23-October 13

Re: C: Find element in the array

Posted 06 April 2015 - 12:11 PM

Ok then you will need a function to calculate the area for each triangle.
double getarea(TRIANGLE *tr)
{
   return abs(1/2*(tr->x1*(tr->y2-tr->y3)+tr->x2*(tr->y3-tr->y1)+tr->x3*(tr->y1-tr->y2)));
}
//In main()
double area=getarea(array);
for(i=0;i<n;i++)
{
   if(area<getarea(array+i))
   {
     area=getarea(array+i);
    }
    else
    { 
        printf("Area %lf",area);
     }
}


This post has been edited by zakhrov: 06 April 2015 - 12:11 PM

Was This Post Helpful? 0
  • +
  • -

#13 gruba  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 86
  • Joined: 13-February 15

Re: C: Find element in the array

Posted 06 April 2015 - 12:37 PM

@zakhrov

I have a problem with abs() function.
It gives me 0.000000 as output, but I have included math.h library.

@zakhrov

Sorry for abs() it is working...
Was This Post Helpful? 0
  • +
  • -

#14 ndc85430  Icon User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 703
  • View blog
  • Posts: 2,877
  • Joined: 13-June 14

Re: C: Find element in the array

Posted 06 April 2015 - 01:00 PM

You're doing integer division in there, i.e. 1/2. Such divisions will lose anything after the decimal point. You want to make sure one of the values is floating point before you do the division.
Was This Post Helpful? 0
  • +
  • -

#15 gruba  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 86
  • Joined: 13-February 15

Re: C: Find element in the array

Posted 06 April 2015 - 01:11 PM

@zakhrov

How to print vertices of triangle with larges area?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2