piecewise function Evaluation If statement!

Source: Internet
Author: User

On-Machine content: Solving piecewise functions with an if statement

The purpose of the machine: branch structure programming

/*

* Copyright (c) 2012, Computer College, Yantai University

* All rights reserved.

* Author: Xu Benxi

* Date of Completion: October 25, 2012

* Version Number: v1.0

*

* Input Description: X,x is not a negative number

* Problem Description: To find the value of the independent variable x as a piecewise function. Y=x (0<=X<2), y=x^2+2 (2<=x<6)

, y= (x+1) ^ (1/2) (6<=x<10), y=1/(x+1) (x>=10)

* Program output: value of y

* Problem Analysis: First determine the value of x and then do the processing

* Algorithm design: Using the IF statement to deal with

*/

My Code:

#include <iostream>
#include <Cmath>
using namespace std;
int main ()
{
	double x,y;
	cout<< "Please enter the value of x:";
	cin>>x;
	if (x<0)
		cout<< "Input error!" <<endl;
	else
		
	{if (x<2&&x>=0)
	y=x;
	
	else
		if (x<6&&x>=2)
			Y=1+pow (x,2);
		else
			if (x<10&&x>=6)
				y=sqrt (x+1);
			else
				if (x>=10)
					y=1/(x+1);
				cout<< "y=" <<Y<<ENDL;}
	
				return 0;
}


Results screenshot:

My summary is not skilled enough to keep the passion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.