C Language Programming third job--selection structure (1)

Source: Internet
Author: User
Tags pow

(a) Correct the wrong question

Calculate the value of f (x): Enter a real number x, calculate and output the value of the following piecewise function f (x), and leave 1 decimal places on the output.

Input and output Example 1:
Enterr x:10.0
F (10.0) = 0.1
Input and output Example 2:
Enter x:234
F (234.0) = 234.0

source program (with the wrong program)

  #include <stdio.h> int main (void) {double X , y; printf ( "Enter x: \ n"); scanf ( "=%f", X); if (x = 10); {y = 1/x} else (x! = 10) {y = x;} 
                      
                       printf (
                        "f (%.2f) =%.lf\n" x y); return 0;} 
Error One

No semicolon before line tenth

Correction method: Add a semicolon after y=1/x

Error two

The eles does not match the IF

Correction method Remove the semicolon after the IF

Error 3

No need to judge after Eles

Method of Correction: Remove the judgment after Eles

Error 4

Commas must be separated from the variable between the quotation mark and the variable.

Error Five

%LF in the output statement

Correction method Change%.LF to%.1f

Error Six

The output shows a decimal%.2f should be changed to%.1f

Error Seven

SCANF statement because it is defined as a double type

Correction method applying LF and removing the =%f =

Error 8

No add-in address characters in scanf statement

Correction method Add the address character before X &

Error Nine

No line breaks in the given output format

Correction method to remove the first one from printf

Error Ten

If after the judgment shall be equal to

Correction Method = = = = =

(ii) Study summary

1.if (a) if (b) X=x+1;else y=y+1; What exactly does it mean? What if this else should be paired with? How to express this pairing method clearly? Write the section code in a normalized format.

If condition A is met, run condition B if the condition B is met x=x+1 run if not satisfied y=y+1

else and if (b) pairing

Else should be paired with the most recent non-paired if

if(a){    if(b)    {        x=x+1;    }
else
{
y=y+1;
}
}
2.C语言的表达式 (-10< x <10)能够表达x在区间(-10,10)吗?为什么?如果不能,正确的表达式应该怎么写?
不能 x>-10&&x<10;

When entering data in the 3.C language, if an illegal character is encountered, the input is considered to be complete. Run the program (1), enter 123a, and see what the output is? Can you explain why? ,
Procedure (1)

#include <stdio.h>int main(){ int a, b; scanf("%d %d", &a, &b); printf("a = %d, b = %d\n", a, b); return 0;}
The output is a=123 b=1
Because it is defined as an integral type, the character type will stop reading, so assigning a value of 1 to B is considered complete.

The return value of the scanf () function is the number of successful read data and returns 0 if no data is read. Run the program (2), enter 123 45 and 123a, respectively, to see what the output is? Why?
Procedure (2)

#include <stdio.h>int main(){ int a, b,n; n = scanf("%d %d", &a, &b); printf("n = %d\n", n); return 0;}
输结果为n=2 n=1因为n为输入的变量的个数 所以输入为123 45是两个都是整型 所以可以读取两个变量
输入123a是 遇到了非法字符 所以停止读取 只能读取123

Modify the program (1) to output "input error!" when illegal data is entered. Examples are as follows:

Modify the following

#include <stdio.h>
int main ()
{
int a,b,n;
N=SCANF ("%d%d", &a, &b);
if (n==1)
{
printf ("Input error!\n");
}
Else
{
printf ("A =%d, B =%d\n", A, b);
}
return 0;
}

7-2 Calculating piecewise functions [2]

#include <stdio.h>
#include <math.h>
int main (void)
{
Double x = 0.0,y = 0.0;
scanf ("%lf", &x);
if (x>=0)
{
y = Pow (x,0.5);
}
Else
{
y = Pow (x+1,2) + 2 * x + 1/x;
}
printf ("f (%.2f) =%.2f", x, y);
return 0;
}

7-4 subtraction of the arithmetic introduction

#include <stdio.h>
int main (void)
{
int a,b,c,d,e, F;
Double g;
scanf ("%d%d", &a,&b);
c = a + B;
D = A-B;
E = a * b;
printf ("%d +%d =%d\n", a,b,c);
printf ("%d-%d =%d\n", a,b,d);
printf ("%d *%d =%d\n", a,b,e);
if (a%b==0)
{
f = A/b;
printf ("%d/%d =%d", a,b,f);
}
Else
{
g = (double) A/b;
printf ("%d/%d =%.2f", a,b,g);
}
return 0;
}

7-6 output Triangle area and perimeter

#include <stdio.h>
#include <math.h>
int main (void)
{
int a = 0,b = 0,c = 0;
Double s = 0.0,area = 0.0,p = 0.0;
scanf ("%d%d%d", &a,&b,&c);
if (A + b>c&&a + c>b&&b + c>a)
{
p = a + b +c;
s = p/2.0;
Area = sqrt (S * (s-a) * (s-b) * (s-c));
printf ("area =%.2f; Perimeter =%.2f ", area,p);
}
Else
{
printf ("These sides does not correspond to a valid triangle");
}
return 0;
}

7-8 Taxi Pricing

#include <stdio.h>
int main (void)
{

Double k = 0.0,f = 0.0,m = 0.0;
int w = 0,t = 0;
scanf ("%lf%d", &k,&t);
if (k<=3)
{
m = 10;
}
else if (k<=10)
{
m = ten + (k-3) * 2;
}
Else
{
m = ten + (k-3) * 2 + (K-10);
}
if (t>=5)
{
w = T/5 * 2;
}
Else
{
w = 0;
}
f = m + W;
printf ("%.0f", f);
return 0;

}

 

C Language Programming third job--selection structure (1)

Related Article

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.