ok so iv been trying loads of combos and i think i got it...
but i cant seem to get the second column of my table right, the values are all wrong. i have checked the maths but cant seem to figure out why. Cheers.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Define Variables
struct parameters
{
double max_moment;
double min_moment;
double no_inc;
double inc_diff;
double i;
double depth;
double breadth;
double moment_area;
double moment;
double stress;
struct parameters *next;
};
int main(void)
{
// Define struct components
struct parameters parametersvariable, *parameters_ptr;
struct parameters *first_ptr, *new_ptr, *previous;
struct parameters *list_ptr;
parameters_ptr = ¶metersvariable;
{
// Prompt user to input depth, breadth, min_moment, max_moment and no_inc
fprintf(stdout, "This program will calculate Max/Min Stress over a range of Applied Moments.\n");
fprintf(stdout, "The Results will be displayed in table-form at the end.\n\nLets Begin...");
fprintf(stdout, "\n\n1. Enter the value for Depth of the Beam: [m]: ");
fscanf(stdin, "%lf", ¶meters_ptr->depth);
fprintf(stdout, "\n\n2. Enter the value for Breadth of the beam [m]: ");
fscanf(stdin, "%lf", ¶meters_ptr->breadth);
fprintf(stdout, "\n\n3. Enter the value of the Minimum Applied Moment [Nm]: ");
fscanf(stdin, "%lf", ¶meters_ptr->min_moment);
fprintf(stdout, "\n\n4. Enter the value of the Maximum Applied Moment [Nm]: ");
fscanf(stdin, "%lf", ¶meters_ptr->max_moment);
fprintf(stdout, "\n\n5. Enter the Number of Increments between the Min and Max Applied Moments: ");
fscanf(stdin, "%lf", ¶meters_ptr->no_inc);
// Calculate the difference between each increment
parameters_ptr->inc_diff = (parameters_ptr->max_moment-parameters_ptr->min_moment)/parameters_ptr->no_inc;
parameters_ptr->moment_area = (parameters_ptr->breadth * (parameters_ptr->depth * parameters_ptr->depth * parameters_ptr->depth)) / 12;
// Work out the values of applied moment using a loop, counter and put in a linked list
first_ptr = NULL;
for(parameters_ptr->i=0; parameters_ptr->i<parameters_ptr->no_inc;parameters_ptr->i++)
{
new_ptr = (struct parameters *)malloc(sizeof(struct parameters));
if (first_ptr != NULL)
previous->next = new_ptr;
else
first_ptr = new_ptr;
new_ptr->next = NULL;
new_ptr->moment = parameters_ptr->min_moment + (parameters_ptr->i * parameters_ptr->inc_diff);
new_ptr->stress = (parameters_ptr->moment * parameters_ptr->depth) / (2 * parameters_ptr->moment_area);
previous = new_ptr;
}
fprintf(stdout,"\n\nMoments [Nm] Max/Min Stress [N/m2]\n");
list_ptr = first_ptr;
while(list_ptr != NULL)
{
fprintf(stdout, "\n%.2lf %.2lf\n", list_ptr->moment, list_ptr->stress);
list_ptr = list_ptr->next;
}
return(0);
}
}