4 Replies - 1020 Views - Last Post: 01 January 2013 - 09:09 PM Rate Topic: -----

#1 biff250  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-January 13

Stress and Strain Program

Posted 01 January 2013 - 08:33 PM

Greetings everyone,

I'm new to the C Language and currently taking a course for it. I've hit an area that I can't seem to figure out though...

Below is my code so far. I don't think the numbers are right for the calculations, but I am able to get the stress and strain to print, but it's only for one calculation. I think my problem lies in the FOR loop since it isn't looping, or appears that way to me.

What I am trying to figure out how to do is get the code to print a list of loads with their associated stress and strain.

If anyone can give me some guidance it will be greatly appreciated.

Thanks.

 
#include <stdio.h>
#include <math.h>
float compute_stress();
float compute_strain(float stress);
void output_results(float stress, float strain);

int main( )
{
	float stress;
	float strain;
	stress = compute_stress();
	strain = compute_strain(stress);
	output_results(stress,strain);
	
	system("PAUSE");
	
	return(0);
}

float compute_stress()
{
	const float pi = 3.141593f;
	float stress;
	float diameter;
	float length;
	float area;
	long int p;
	stress=0;

	printf("\nEnter the diameter of the steel rod in inches: ");
	scanf("%f", &diameter);

	printf("\nEnter the length of the steel rod in inches: ");
	scanf("%f", &length);

	for(p = 10000; p <= 1000000; p += p+100000)
		{
			area = (pi * diameter *diameter) / 4;
			stress = p / area;
		}

	return stress;
}

float compute_strain(float stress)
{
	const float E=30000000;

	float strain;

	strain = stress / E;

	return strain;
}
void output_results (float stress, float strain)
{
printf("\nStress = %f \nStrain = %f\n", stress, strain);
}


This post has been edited by macosxnerd101: 01 January 2013 - 08:34 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Stress and Strain Program

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3846
  • View blog
  • Posts: 11,775
  • Joined: 25-December 09

Re: Stress and Strain Program

Posted 01 January 2013 - 08:52 PM

In your compute_stress() function you may be computing multiple stress values but you are only really using one, the last. You only return that last value to the calling function for use in your printout.

I would recommend restructuring your program somewhat, either create an new function or functions to retrieve the user entered data or place the user entry in main(). Next place your loop in main(), after you retrieve the user entered data and call your compute_stress function with the required parameters. Then in your loop call compute and output functions.

By the way I don't see where you are using length as yet.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 biff250  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-January 13

Re: Stress and Strain Program

Posted 01 January 2013 - 09:01 PM

I think I understand what you are saying. I'm working on the restructuring tips now.

For the functions...is there a way to call/return more than the last computation?

I noticed the part about the length as well just after I posted the original reply. I found my error on that and had inadvertantly left out some calculations that were key.

Thanks again.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3846
  • View blog
  • Posts: 11,775
  • Joined: 25-December 09

Re: Stress and Strain Program

Posted 01 January 2013 - 09:06 PM

Quote

For the functions...is there a way to call/return more than the last computation?

No that's why I suggested the restructuring, let your functions to do a single calculation, call these functions inside a loop in the calling function.

Edit: Also your loop:
for(p = 10000; p <= 1000000; p += p+100000)

that last part probably should be
p += 100000



Jim

This post has been edited by jimblumberg: 01 January 2013 - 09:08 PM

Was This Post Helpful? 0
  • +
  • -

#5 biff250  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-January 13

Re: Stress and Strain Program

Posted 01 January 2013 - 09:09 PM

Sounds good. Thanks for your help. Hopefully I'll be able to get it figured out from here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1