C Language Programming third time job

Source: Internet
Author: User
Tags logical operators

(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>&NBSP; < Span class= "Hljs-keyword" >int main ( void)   < Span class= "hljs-function" >

< Span class= "Hljs-params" > 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 message 1:
Error reason 1: statement ending with a semicolon
Correcting Method 1: Adding a semicolon after a statement
error message 2:
Error cause 2:else cannot be conditional
Fix Method 2: Remove it
error message 3:
Error cause 3:printf statement error, two comma separated
Correction Method 3: Comma separated from XY after ""
error message 4:
Error reason 4:if statement () is a judgment statement rather than an assignment statement, And no semicolon
Correction Method 4: x==10 with a judgment statement, and remove the semicolon
Error message 5:

error cause 5: Output format error
Fix method 5: remove \ nthe
error message 6:
Error cause 6: Output format error
Correction Method 6: Change%.2f to%.1f,%.lf to%.1f
error message 7:
Error reason 7:scanf output when x is the address, and should be%LF type
Correction Method 7: Add & in front of X, "" In the%LF
program run successfully!

(ii) Learning 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. The
meaning is a and B is the time to execute the statement x=x+1, otherwise execute y=y+1, this else with the closest to its nearest if pairing, the correct pairing method is as follows:
if (a&&b
{
    x = x + 1;
}
else
{
    y = y + 1;
}
2.C Expressions ( -10< x <10) can express x in the interval ( -10,10)? Why? If not, how should the correct expression be written?
No, because the merging of logical operators is from left to right, if the number of inputs is greater than 10 is true, satisfies x>-10, at this time X is assigned to 1, and satisfies x<10, and thus is correct, it can not be expressed in the interval ( -10,10) , the correct expression is (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 reason is that AB is an integer, and a is an illegal character, so the input ends

The return value of the

scanf () function is the number of successful read-in 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?
Program (2) #include <STDIO.H>

int main(){ int a, b,n; n = scanf("%d %d", &a, &b); printf("n = %d\n", n); return 0;}
输出结果为
原因是第一中输入为整型符合定义的int型,故录入,而123a中a为非法字符,故只能录取一个,当录取到a是结束
修改程序:

总结:if()内要用判断语句而不是赋值语句!
double型的输入为%lf!!
(三)实验总结
3.(1)题目
判断一个5位数是不是回文数。例如12321是一个回文数,即它的个位与万位相同,十位与千位相同。
(2)流程图

(3)源代码 :


(4)测试数据及运行结果


(5)实验分析
熟悉分支结构的正确形式
5.(1)问题
计算火车运行时间本题要求根据火车的出发时间和达到时间,编写程序计算整个旅途所用的时间。
(2)流程图

(3) Source code

(4) test data and operating results

(5) Experimental analysis

Pay attention to the output format, using%02d!

7. (1) Title
Three days fishing two days, China has a proverb called "Notwith two Days". Suppose someone starts "notwith two days" from one day and asks if the person is "fishing" or "tanning" in the next nth day?

(2) Flowchart

(3) Source code

(4) test data and operating results

(5) Experimental analysis

Consider the problem to be comprehensive, careful!

9. (1) Title
Ladder electricity price, in order to promote the residents to save electricity, a provincial power company to carry out "ladder price", the installation of a table of the residents of the consumer electricity price is divided into two "ladder": the monthly electricity consumption 50-kilowatt hours (including 50-kilowatt hours) within the price of 0.53 yuan/kwh; more than 50-kilowatt, the excess power consumption, Electricity price increases 0.05 yuan/kwh. Please write a program to calculate the electricity bill

(2) Flowchart

(3) Source code

(4) test data and operating results

(5) Experimental analysis

The output format is%.2f and the branching structure if,if else,else is used here.

Four. PTA Submission List

C Language Programming third time job

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.