C Language Programming fifth time job

Source: Internet
Author: User

(a) Correct the wrong question

1. Output Fahrenheit Celsius temperature conversion table: input two integers lower and upper, output a Zhang Huasi Celsius temperature conversion table, Fahrenheit temperature range is {Lower,upper}, each increase of 2 ℉. The calculation formula is as follows:
c = 5x (f-32)/9
Wherein, c is the temperature of Celsius, F is the temperature of Fahrenheit.
Error message 1:

Error cause 1:FOR statement cannot be semicolon-added.

Correction method: Remove the semicolon after the IF

Error message 2:

Error reason 2:for The statement in the loop is greater than one sentence to add {}

Correction method: Enclose the for inside statement in {}

Error message 3:

The error causes the statement within the 3:for to be used; interval

Correction Method 3: Replace the comma in the for;

Compile Error!

After careful review of the program, it is found that the variable input and output format does not match

Error message The 4:FAHR variable is defined as an integer, but the output is actually a floating-point output

Correction Method 4: Change%3.0f to%d

Error message 5:

Error message 5:celsius is double and does not match the result of the calculation

Correction Method 5: Enhanced type conversion before 5/9 (double)

Run correctly!

Two. Learning Summary:

1. Describe the procedure in detail, that is, each step of the program is the execution of which code, the order of execution. The loop needs to describe the detailed execution of the first two loops and the last cycle. Finally, the function of program implementation is described.

A: ① defines four shaping constants I num odd even

② the initial value of odd and even respectively 0

③ Enter for loop, I=1, condition is true

④ output an integer num, determine the parity of NUM, and give the corresponding statistical parity number of values plus one, this is the first cycle

⑤ execution i++, at this time i=2, the condition is true, start the second cycle

⑥ input an integer num, determine the parity and give a count of the number of values plus one, this is the second cycle

⑦ Repeat execution

⑧ when i=11, the condition is false, cannot execute the loop, so the loop ends

Implementation function: Enter any number of 10 integers, respectively, the number of odd numbers and the number of even numbers

2. Increment operator

(1) What is the result of running the following program? When the increment operator is used alone, does the prefix and suffix form differ?

Result is

The increment operator itself is different, ++i refers to let I self-add, and then execute the statement, i++ refers to execute the statement first, and then I self-added, but in this program, the increment operator is used alone, the prefix and suffix does not affect.

(2) What is the result of running the following program? What is the execution process of the two output statements? What's the problem? After the two output statements are executed, what are the values of I and J respectively?

Result is

The first output statement printf ("%d\n", i++), is the first input I, then the value of I plus one, the second output statement printf ("%d\n", ++j), is the first J value plus one, and then output. Description The increment operator has a certain difference when the statement is run. After two output statements have been executed, both I and J have values of 3.

3. Some loop statements are followed by {}, some do not, some statements have indentation, run the following four small programs, the results of the operation is different? Is the identification of the loop body related to the form of the indentation? Is it related to {}? So, how should we correctly judge the loop body in a loop statement? In the following program, which format is the correct indentation format.

1.

2.

3.

4.

In combination with 1, the other analysis, the indentation format is not the same, but the result is the same, so the loop body and the indentation is not related.

Combined with 2, the analysis, is obviously more than 3 {}, the output is different, the combination of 3,4 have {}, but the number of statements contained in the different, the output is different, and therefore with {}.

The second indentation is not correct

4. Summarize the other content that you think you need to summarize.

1, should pay attention to increment character i++ and ++i difference, correct use.

2, whether the judgment condition in the For statement is set up to use "= =" to indicate, "=" for the assignment do not confuse.

Three. Experimental summary
1. (1) Title
Find one of the odd points sequence the first n items and
The subject asks to write the program, calculates the sequence 1 + 1/3 + 1/5 + ... The sum of the first n items.
(2) Flowchart

(3) Source code

#include <stdio.h>

int main ()
{
int i,n,x=1;
Double y,s=0.0;
scanf ("%d", &n);
for (i=1;i<=n;i++)
{
Y= (double) 1/x;
S=s+y;
x=x+2;
}
printf ("sum =%.6f", S);
return 0;
}

(4) Experimental Analysis:

Question: "sum = sum + 1.0/i" is written as "sum = sum + 1/i"

Cause: Inaccurate accuracy, resulting in incorrect results

Workaround: Change "sum = sum + 1/i" to "sum = sum + 1.0/i"

(5) PTA Submission List

3. (1) Title: Odd and even separation
Given n positive integers, count the number of odd and even numbers.

(2) Flowchart

(3) Source code

#include <stdio.h>

int main ()
{
int n,i,x,count1=0,count2=0;
scanf ("%d", &n);
for (i=0;i<n;i++)
{
scanf ("%d", &x);
if (x%2==1)
count1++;
Else
count2++;
}
printf ("%d%d", count1,count2);

}

(4) Experimental analysis

Problem: The judgment within the IF statement only writes an equal sign

Cause: = Assignment, if = = is the judgment

Correction method: Change = =

(5) PTA Submission List

5. (1) Title: Statistical characters
In this case, you need to write a program, enter 10 characters, and count the number of letters, spaces or carriage returns, numeric characters, and other characters.
(2) Flowchart

(3) Source code

  #include <stdio.h>

#include <string.h>
#define N 10
int main ()
{
int letter=0,blank=0,digit=0,other=0,i;
char x;
for (i=0;i<n;i++)
{
scanf ("%c", &x);
if ((x>= ' a ' &&x<= ' z ') | | (x>= ' A ' &&x<= ' Z '))
{
letter++;
}
else if (x== ' | | | x== ' \ n ')
{
blank++;
}
else if (x>= ' 0 ' &&x<= ' 9 ')
{
digit++;
}
Else
{
other++;
}
}
printf ("letter =%d, blank =%d, digit =%d, and other =%d", letter,blank,digit,other);
return 0;
}

(4) Experimental analysis

Question: if ((a >= "a" && a <= "z") | | (A >= "a" && a <= "Z"))

Reason: To judge the character, to use the "

Correction method: if ((a >= ' a ' && a <= ' z ') | | (A >= ' a ' && a <= ' Z '))

(5) PTA Submission List

7. (1) Title: Ask for the first n of the staggered sequence and
The subject asks to write the program, computes the staggered sequence 1-2/3+3/5-4/7+5/9-6/11+ ... The sum of the first n items.
(2) Flowchart

(3) Source code

  #include <stdio.h>

int main ()
{
int i,j=3,n;
Double a=1,b;
scanf ("%d", &n);
if (n!=1)
{
for (i=2;i<=n;i++)
{
b= (double) i/j;
if (i%2==0)
{
B=-b;
}
A=a+b;
j=j+2;
}
}
printf ("%.3f", a);
return 0;
}

(4) Experimental analysis

When I find that the running result I data is inaccurate, I want to examine it carefully, often to use forced type conversion!

(5) PTA Submission List

Four. Blog Peer reviews

Http://www.cnblogs.com/zrx--/p/7814055.html

Http://www.cnblogs.com/ziluo/p/7819483.html

Http://www.cnblogs.com/xiaoqi1/p/7821666.html

C Language Programming fifth time job

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.