Why my C program is keep crashing

Hello Everyone, here is my assigment for my university but I dont get why I am getting my program crashed every single time when I run it?
Input from the user will be 2.1 for radius and 5.6 for length

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  /*------------------------------------------------------------------
 Description: Calculates how the volume changes for different depths
              in a horizontal cylinder.
------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// Define symbolic constant
#define N       50    // number of points to compute
#define H_IX    0   // Index to row with depth, h, values
#define VOL_IX  1   // Index to row with volume values
#define NUM_ROWS 2  // number of rows in 2D array
#define TRUE    1
#define FALSE   0

// Definition of a structure type for user input
typedef struct
{
    double radius;  // in m
    double length;    // in m
} CYLINDER;

// Prototypes
void getDimensions(CYLINDER*cyl);
void computeVolume(CYLINDER *, int n, double [][n]);
/*--------------------------------------------------------------------
Function: main
Description:  Calls getDimensions to get cylinder dimensions from the user,
              computeVolume to fill in the matrix containing depth/volume pairs,
              and displays the contents of the matrix in a table.
----------------------------------------------------------------------*/
void main()
{
    // Variable declarations
    CYLINDER cyl;  // structure variable fo cylinder
    double points[NUM_ROWS][N];  // N points of depth/volume
    int ix;   // for indexing the columns in the 2D array
    double Depth; // Get input from user, the cylinder radius and length
    double Volume;// Get input from user, the cylinder radius and length
    getDimensions(&cyl);
    // Compute depth/volume points
    computeVolume(&cyl, N, points);  // no & for the points array
    // Display results (could have used a function to display the results)
    // Setup the start of the table
    printf("The change in liquid volume of the cylinder with radius %.2f \nand length %.2f as depth changes is as follows.\n",
            cyl.radius, cyl.length);
    printf("%10s    %10s\n",Depth, Volume);
    printf("------------------------\n");
    // Loop to display each column of the table.
    for(ix = 0; ix < N; ix = ix + 1)
       printf("%10.2f    %10.2f\n", points[H_IX][ix], points[VOL_IX][ix]);
}
/*------------------------------------------------------------------------
Function: getDimensions
Parameter
    cylPtr - pointer to a CYLINDER structure variable
Description: Prompts the user for the dimensions of the
             cylinder.  Ensures the values are > 0.
------------------------------------------------------------------------*/
void getDimensions(CYLINDER *cylPtr)
{
    int flag;
    do
    {
         flag = TRUE;
         printf("Please enter the values for the cylinder radius and length: ");
          scanf("%lf %lf",&cylPtr->radius, &cylPtr->length);
          if( cylPtr->radius <= 0.0 || cylPtr->length <= 0.0)
          {
              printf("Both values must be greater than zero.\n");
              flag = FALSE;
          }
    } while(flag == FALSE);
}
/*------------------------------------------------------------------------
Function: computeVolume
Parameter
    cylPtr - pointer to a CYLINDER structure variable
    n      - number of points (note that using this parameter makes
                               the function independent of the symbolic
                               constant and generalizes the function)
    dataPoints - depth/volume points stored in a 2D array
                 Row H_IX contains the depth values
                 Row VOL_IX contains the volume values
Description: Computes a number of depth/volume data points that varies
             the depth of the liquid from 0 to the cylinder diameter.
             n such data points are computed (i.e. the increment in the
                                              value of h is 2r/n).
------------------------------------------------------------------------*/
void computeVolume(CYLINDER *cylPtr, int n, double dataPoints[][n])
{
    // Declaration of variables
    double increment;   // how to increment the depth
    double h;           // depth value
    int ix;             // loop counter and column index in the 2D array
    double term1, term2;  // for computing terms of the equation
    // setup the variables
    increment = cylPtr->radius/n;
    ix = 0;
    h = 0.0;
    // Loop for calculating each of the n depth/volume points.
    while(ix < n)   // Can also use a for loop
    {
       dataPoints[H_IX][ix] = h;
       term1 = pow(cylPtr->radius,2)*acos((cylPtr->radius-h)/cylPtr->radius);
       term2 = (cylPtr->radius - h)*sqrt(2.0*cylPtr->radius*h - pow(h,2));
       dataPoints[VOL_IX][ix] = (term1 - term2)*cylPtr->length;
       // increment variables for next point
       ix = ix +1;

    }
}
Line 26/91: lower case n should be upper case N.
main() should return int.
Line 48: printf(...) uses the wrong format for Depth and Volume.
Depending on your compiler and its settings, there may be warning messages issued.

First, it should be int main() not void.

Next, line 48:
 
    printf("%10s    %10s\n",Depth, Volume);

That line generates three warnings. The first is that %s expects a string, but but the parameters Depth and Volume are type double.
The other two warnings are that Depth and Volume are not initialised, they contain garbage.

I think, trying to understand the intention here, that line should be
 
    printf("%10s    %10s\n", "Depth", "Volume");
in order to print the heading text.

That means lines 39 and 40 can be deleted:
39
40
    double Depth; // Get input from user, the cylinder radius and length
    double Volume;// Get input from user, the cylinder radius and length 


I have to say on reading the comments next to those two lines I was baffled as to why it said radius and length but the variables were named Depth and Volume. Something didn't make sense. It turns out (I think) those lines don't belong at all - and the comment relates to the function call which follows.
Topic archived. No new replies allowed.