integral program messing up

Hi so I have a program that is approximating the area under a given function's curve. I'm trying to make it so that it passes through command line statements.

When I try to run the program, it runs very weirdly, for example:


./ass4 -f 1 -m 1 -a -1 -b 0 -n 30
The approximate area under the curve is: 0
The approximate area under the curve is: 0
The approximate area under the curve is: 0
The approximate area under the curve is: -145



#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
using namespace std;

//prototypes for all separate functions
int chooseFunc(int, double, double, double, double);
int shape(int, double, double, double, double);
int numShapes(int, double, double, double, double);
int startPoint(int, double, double, double, double);
int endPoint(int, double, double, double, double);
double chooseMNgo1(int, double, double, double, double);
double chooseMNgo2(int, double, double, double, double);
double chooseMNgo3(int, double, double, double, double);
double chooseMNgo4(int, double, double, double, double);
double chooseMNgo5(int, double, double, double, double);
double func1_r(int, double, double, double, double);
double func2_r(int, double, double, double, double);
double func3_r(int, double, double, double, double);
double func4_r(int, double, double, double, double);
double func5_r(int, double, double, double, double);
double func1_t(int, double, double, double, double);
double func2_t(int, double, double, double, double);
double func3_t(int, double, double, double, double);
double func4_t(int, double, double, double, double);
double func5_t(int, double, double, double, double);

//calls all the functions
int main(int argc, char *argv[]){
double choose, start, end, number;
int choose2;
	if(argc==11){
	for(int i = 1; i < argc; i += 2){
	    if(argv[i][0] == '-' && argv[i][1]=='f')
	   choose = atoi(argv[i+1]);
	    else if(argv[i][0] == '-' && argv[i][1]=='n')
	   number = atoi(argv[i+1]);
	    else if(argv[i][0] == '-' && argv[i][1]=='a')
	   start = atoi(argv[i+1]);
	    else if(argv[i][0] == '-' && argv[i][1]=='b')
	   end = atoi(argv[i+1]);
	    else if(argv[i][0] == '-' && argv[i][1]=='m')
	   choose2 = atoi(argv[i+1]);
			else {
		cout << "ERROR!!!" << endl;
		break;}
	 chooseFunc(choose, number, choose2, start, end);/*
  	start = startPoint(choose, number, choose2, start, end);
       	end = endPoint(choose, number, choose2, start, end);
	choose2 = shape(choose, number, choose2, start, end);
	number = numShapes(choose, number, choose2, start, end);*/	
	}
	
   	return 0;
}

}


//asks user which function they want to use
int chooseFunc(int choose2, double number, double choose, double start, double end){
  //cout<<"Choose a function (1,2,3,4, or 5): "<<endl;
  //cin>>choose;
  /*cout << endl << "Choose" << choose << endl << "Choose2" << choose2 << endl << "Number" << number << endl << "Start" << start << endl << "End" << end << endl <<endl;*/
  if (choose == 1)
     chooseMNgo1(choose, number, choose2, start, end);
  else if (choose == 2)
     chooseMNgo2(choose, number, choose2, start, end);
  else if (choose == 3)
     chooseMNgo3(choose, number, choose2, start, end);
  else if (choose == 4)
     chooseMNgo4(choose, number, choose2, start, end);
  else if (choose == 5){
     chooseMNgo5(choose, number, choose2, start, end);}
}
int shape(int choose2, double end, double choose, double start, double number){
return choose2;
}

int numShapes(int choose2, double number, double choose, double start, double end){
	return number;

}
double chooseMNgo1(int choose2, double end, double choose, double start, double number){
	//cout << "What method would you like to use? Press 1 to use rectangles, 2 to use trapezoids or 3 to use both." << endl;
	//cin >> choose2;
	/*()start = startPoint(start, end, number, choose, choose2);
	end = endPoint(start, end, number, choose, choose2);*/
	if (choose2 == 1){
	   func1_r(start, end, number, choose, choose2);}
	else if (choose2 == 3){
	   func1_r(start, end, number, choose, choose2);
	   func1_t(start, end, number,  choose, choose2);}
	   else if ( choose2 == 2){
	   func1_t(start, end, number, choose, choose2);}
}
double chooseMNgo2(int choose2, double end, double choose, double start, double number){
	//cout << "What method would you like to use? Press 1 to use rectangles, 2 to use trapezoids or 3 to use both." <<endl;
	//cin >> choose2;
	//start = startPoint(start, end, number, choose, choose2);
	//end = endPoint(start, end, number, choose, choose2);
	cout << "here";
   if (choose2 == 1){
	   func2_r(start, end, number, choose, choose2);}
	else if (choose2 == 3){
	   func2_t(start, end, number, choose, choose2);
	   func2_r(start, end, number, choose, choose2);}
	   else if (choose2 == 2){
	   func2_t(start, end, number, choose, choose2);}
}
double chooseMNgo3(int choose2, double end, double choose, double start, double number){
	//cout << "What method would you like to use? Press 1 to use rectangles, 2 to use trapezoids or 3 to use both." <<endl;
	//cin >> choose2;
	//start = startPoint(choose, choose2, start, end, number);
	//end = endPoint(choose, choose2, start, end, number);
	if (choose2 == 1){
	   func3_r(start, end, choose, choose2, number);}
	else if (choose2 == 3){
		func3_r(start, end, choose, choose2, number);
		func3_t(start, end, choose, choose2, number);}
	else if(choose2 == 2){
	   func3_t(start, end, choose, choose2, number);}
}
double chooseMNgo4(int choose2, double end, double choose, double start, double number){
	//cout << "What method would you like to use? Press 1 to use rectangles, 2 to use trapezoids or 3 to use both." <<endl;
	//cin >> choose2;
	//start = startPoint(start, end, choose, choose2, number);
	//end = endPoint(start, end, choose, choose2, number);
	if (choose2 == 1){
	   func4_r(start, end, choose, choose2, number);}
	else if(choose2 == 3){
	   func4_r(start, end, choose, choose2, number);
	   func4_t(start, end, choose, choose2, number);}
	   else if(choose2 == 2){
	   func4_t(start, end, choose, choose2, number);}
}
double chooseMNgo5(int choose2, double end, double choose, double start, double number){
	 //cout << "What method would you like to use? Press 1 to use rectangles, 2 to use trapezoids or 3 to use both." <<endl;
	 //cin >> choose2;
	 //start = startPoint(start, end, choose, choose2, number);
	 //end = endPoint(start, end, choose, choose2, number);
	if (choose2 == 1){
	   func5_r(start, end, choose, choose2, number);}
	else if(choose2 == 3){
	   func5_r(start, end, choose, choose2, number);
	   func5_t(start, end, choose, choose2, number);}
	   else if(choose2 == 2){
	   func5_t(start, end, choose, choose2, number);}
}



**code continued on next post
Last edited on

int startPoint(int choose2, double end, double start, double  number, double choose)
{
	//cout << "Please give a starting value. " << endl;
	//cin >> start;
	return start;
}

int endPoint(int choose2, double start, double end, double number, double choose)
{
	//cout << "Please give an ending value. " << endl;
	//cin >> end;
	return end;
}

double func1_r(int choose2, double  end, double choose, double start, double number){
	float change, total = 0, i;
	//cout << "Finally, indicate how many rectangles you would like to use." <<endl;
	//cin >> rect;
	change = (end-start)/number;
	for(i = start; i < end; i += change)
	   total += change*(2*pow(i,5)+pow(i,3)-10*i+2);
	cout << "The approximate area under the curve is: " << total << endl;
	return total;
}
double func1_t(int choose2, double end, double choose, double start, double number){
   	float change, total = 0, i;
	//cout << "Finally, indicate how many trapezoids you would like to use." <<endl;
	//cin >> trap;
	cout << "here";
	change = (start-end)/number;
	for (i = start; i < end; i+= change)
	   		total += (change * ((2*pow(i,5)+pow(i,3)- 10*i + 2)+(2*pow(i + change, 5)+pow(i + change, 3)-(i + change)*10 +2)))/2;
	cout << "The approximate area under the curve is: " << total << endl;
	return total;
		
}
double func2_r(int choose2, double end, double choose, double start, double number){
	float change, total = 0, i;
	//cout << "Finally, indicate the amount of rectangles you would like to use. " <<endl;
	//cin >> rect;
	change = (start-end)/number;
	for (i = start; i < end; i += change)
	   	total += (6 * pow(i,2) - i + 10) * change;
	cout << "The approximate area under the curve is: " << total << endl;
	return total;
}
double func2_t(int choose2, double end, double choose, double start, double number){
   	float  change, total = 0, i;
	//cout << "Finally, indicate the amount of trapezoids you would like to use. "<< endl;
	//cin >> trap;
	change = (start-end)/number;
	for (i = start; i < end; i += change)
	  total += (change*(( 6 * pow(i,2)- i + 10)+(6*pow(i + change, 2)-(i+ change) +10)))/2;
	cout << "The approximate area under the curve is: " << total << endl;
	return total;
}
double func3_r(int choose2, double end, double choose, double start, double number){
   	float change, total = 0, i;
	//cout << "Finally, indicate the amount of rectangles you would like to use. " << endl;
	//cin >> rect;
	change = (end - start)/number;
	for (i = start; i < end; i += change)
	   total += (change * (5 * i + 3));
	cout << "The approximate area under the curve is: " << total << endl;
	return total;
}
double func3_t(int choose2, double end, double choose, double start, double number){
   	//cout << "Finally, indicate the amount of trapezoids you would like to use. " << endl;
	float  change, total = 0, i;
	//cin >> trap;
	change = (end - start)/number;
	for (i = start; i < end; i += change)
	   total += (((5*i+3)+(5*(i + change)+3))* change)/2;
	cout << "The approximate area under the curve is: " << total << endl;
	return total;
}
double func4_r(int choose2, double end, double choose, double start, double number){
   	//cout << "Finally, indicate the amount of rectangles you would like to use. " << endl;
	float change, i, total = 0;
	//cin >> rect;
	change = (end - start)/number;
	for (i = start; i < end; i += change)
	   total += ((2 * pow(i,3) + 120) * change); 
	cout << "The approximate area under the curve is: " << total << endl;
	return total;
}
double func4_t(int choose2, double end, double choose, double start, double number){
   	//cout << "Finally, indicate the amount of trapezoids you would like to use. " << endl;
	float change, i, total = 0;
	//cin >> trap;
	change = (end - start)/number;
	for (i = start; i < end; i += change)
	   total += (change * ((2*pow(i,3)+ 120) + (2*pow(i+change,3)+ 120)))/2;
	cout << "The approximate area under the curve is: " << total << endl;
	return total;
}
double func5_r(int choose2, double end, double choose, double start, double number){
	float change, i, total = 0;
	//cout << "Finally, indicate the amount of rectangles you would like to use. " << endl;
	//cin >> rect;
	change = (end - start)/number;
	for (i = start; i < end; i += change)
	   total += change * (2*pow(i,2));
	cout << "The approximate area under the curve is: " << total << endl;
	return total;
}
double func5_t(int choose2, double end, double choose, double start, double number){
	float change, i, total = 0;
	//cout << "Finally, indicate the amount of trapezoids you would like to use. " << endl;
	//cin >> trap;
	change = (end - start)/number;
	for (i = start; i < end; i += change)
	   total += (change * ((2 * pow(i,2)) + (2 * pow(i+change,2))))/2;
	cout << "The approximate area under the curve is: " << total << endl;
	return total;
}
This is possibly the worst written piece of code I have ever seen :O

I highly recommend learning the basics of program design, and just starting over from scratch with this.
Yea I commented a lot of unfinished ideas out because I was unsure of whether or not I would need it later but was testing to see if it would run properly.

Also yea I know I'm pretty terrible I'm still learning everything.
Topic archived. No new replies allowed.