Sukrit Kapil
Sukrit Kapil

Reputation: 103

Why am I getting 0 as the output? Can you find the mistake?

The problem is about inputting two sides and the included angle for 6 different triangular plots. We need to find the area of each of these plots.

I wrote this code using 2-D and 1-D arrays and passing the 2-D array to a void function which prints the areas of each of the plots. These are the inputs and their respective outputs I got. I am using Ubuntu 18.04 LTS Terminal.

RUN OF THE PROGRAM

Input:

1
137.4
80.9
0.78
2
155.2  
92.62
0.89
3
149.3
97.93
1.35
4
160.0
100.25
9.00
5
155.6
68.95
1.25
6
149.7
120.0
1.75

Output:

Entered Data is:
1.000000 137.399994 80.900002 0.780000 
2.000000 155.199997 92.620003 0.890000 
3.000000 149.300003 97.930000 1.350000 
4.000000 160.000000 100.250000 9.000000 
5.000000 155.600006 68.949997 1.250000 
6.000000 149.699997 120.000000 1.750000 

Area of plot 1 is: 0.000000.
Area of plot 2 is: 0.000000.
Area of plot 3 is: 0.000000.
Area of plot 4 is: 0.000000.
Area of plot 5 is: 0.000000.
Area of plot 6 is: 0.000000.

Program:

#include <stdio.h>
#include <math.h>
void area(float p[6][4],int m,int n);
int main()
{
    float a[6][4];
    int i,j;
    printf("Enter the plot no.,two sides and included angle.\n");
        for(i=0;i<6;i++)
        {
        for(j=0;j<4;j++)
        {   
            scanf("%f",&a[i][j]);
        }
    }
    printf("Entered Data is:\n");
    for(i=0;i<6;i++)
    {
        for(j=0;j<4;j++)
        {
            printf("%f ",a[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    area(a,6,4);
    return 0;    
}

void area(float p[6][4],int m,int n)
{

    float ar[6];
    int i;
    for(i=0;i<6;i++)
    {
        ar[i] = (1/2)*(p[i][1])*(p[i][2])*sin(p[i][3]);
        printf("Area of plot %d is: %f.\n",i+1,ar[i]);
    }
}

Upvotes: 0

Views: 66

Answers (2)

George Mahusay
George Mahusay

Reputation: 51

(1.0/2.0) * (p[i][1]) * (p[i][2]) * sin(p[i][3]);

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 881113

In this expression:

ar[i] = (1/2)*(p[i][1])*(p[i][2])*sin(p[i][3]);

(1/2) evaluates as zero, which means the whole thing comes out as zero (because zero times anything is zero). The reason for it being zero is because it's integer division, which gives a truncated integer as its result. Use 0.5 instead.

Upvotes: 3

Related Questions