exe screen cutting out too quick -- blank screen

Good evening,

I am developing a project that is much bugger than this small portion from here. This is only the beginning, but the instructions for this part consist of finding the two X value's (roots) where it switches over from a negative value to a positive value and then plug those two values into a specified equation, and then make X more accurate by using the following formula:

x[i]=((x[i-1])-((fx[i-1]*(x[i-2]-x[i-1]))/(fx[i-2]-fx[i-1])));

My output only goes to line 18, then cuts off without the program closing. Why is that?
Any help is apreciated.

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
//Project 2 - ENGR 110-02
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
	//Introduce program to user
	cout<<"Project 2\nThis program determines if a mast will buckle under its own weight using complex \
equations.\n";
	
	//Declare variables
	float l,y,lambda,xrootpls1,xrootmin1;
	int x[10],fx[10],i;
	
	//Get values from user
	cout<<"Using the equation X=(4/9)*g*((lambda*L_3)/(Y*I_2)) to calculate if the mast will buckle, we\
 must find X and L.\n";
 	
 	//Calculate negative root of equation for X
 	for (i=0;i<=5;i+=0.25){
 		fx[0]=(pow(i,0)-(0.375*pow(i,1))+(0.028125*pow(i,2))-(0.0008789*pow(i,3))+(0.000012*pow(i,4)));
 		if (fx[0]<0){
 			fx[2]=(pow(i,0)-(0.375*pow(i,1))+(0.028125*pow(i,2))-(0.0008789*pow(i,3))+(0.000012*pow(i,4)));
 			x[2]=i;
 			break;
 		}
 	}
 	//Calculate posotive root of equation for X
 	for (i=0;i<=5;i+=0.25){
 		fx[0]=(pow(i,0)-(0.375*pow(i,1))+(0.028125*pow(i,2))-(0.0008789*pow(i,3))+(0.000012*pow(i,4)));
 		if (fx[0]<0){
 			i-=0.25;
 			fx[1]=(pow(i,0)-(0.375*pow(i,1))+(0.028125*pow(i,2))-(0.0008789*pow(i,3))+(0.000012*pow(i,4)));
 			x[1]=i;
 			break;
 		}
 	}
	i=3;
	x[i]=((x[i-1])-((fx[i-1]*(x[i-2]-x[i-1]))/(fx[i-2]-fx[i-1])));
	
	//Ask user accuracy for X value
	cout<<"\nTo what accuracy would you like X to be? (Default x4 trials)\nEnter number (+4): ";
	cin>>i;	//Get value stored in 'i' variable
	
	for (i;i<=i+4;i++){
		x[i]=((x[i-1])-((fx[i-1]*(x[i-2]-x[i-1]))/(fx[i-2]-fx[i-1])));
		cout<<"\n"<<x[i];
	}
	cout<<"\n"<<fx[1];
	  
	
}
Last edited on
I'm not exactly sure what's going on yet, so I'm just going to post some tips on things that are probably wrong.

Line 21: for (i=0;i<=5;i+=0.25){

i is an int -- INTEGER.
You cannot add a floating-point number like 0.25 to an integer, it will just be rounded to i += 0.

Lines 22 to 27:
Why are you overwriting the same values (fx[0], fx[1], x[1]), over and over again?
In combination with the faulty for loop, you have an infinite loop here if fx[0] >= 0.

Line 40: x[i]=((x[i-1])-((fx[i-1]*(x[i-2]-x[i-1]))/(fx[i-2]-fx[i-1])));
I don't see where you ever calculated values for the necessary values of x[...].

Line 46: for (i;i<=i+4;i++){
i will always* be less than i + 4? This will be an infinite loop until i reaches 10, at which point you have undefined behavior for going out of the bounds of the array.
*("always" other than after undefined behavior was invoked)
Do you mean i < 4?

In general, I think your program needs some refactoring. You have duplicate, complicated math formulas in your program.

__________________________________________

Enable warnings on your compiler, as well:
 In function 'int main()':
46:8: warning: statement has no effect [-Wunused-value]
46:14: warning: assuming signed overflow does not occur when assuming that (X + c) >= X is always true [-Wstrict-overflow]
13:8: warning: unused variable 'l' [-Wunused-variable]
13:10: warning: unused variable 'y' [-Wunused-variable]
13:12: warning: unused variable 'lambda' [-Wunused-variable]
13:19: warning: unused variable 'xrootpls1' [-Wunused-variable]
13:29: warning: unused variable 'xrootmin1' [-Wunused-variable]
Last edited on
Topic archived. No new replies allowed.