C Language Programming fifth time-cycle structure (1)

Source: Internet
Author: User
Tags integer definition

(a) Correct the wrong question

Output Fahrenheit Celsius temperature conversion table: input two integers lower and upper, output a Zhang Huasi Celsius temperature conversion table, Fahrenheit temperature value 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.
Input and output Example 1:
Enter lower:32
Enter upper:35
Fahr Celsius
32 0.0
34 1.1
Input and output Example 2:
Enter lower:40
Enter upper:30
Fahr Celsius
source program (with the wrong program)

    #Include<stdio.h>IntMain(void) {int Fahr, lower, upper;/* Fahr means Fahrenheit */double Celsius;/* Celsius = Celsius */printf ( "Enter Lower:"); scanf ( "%d", &lower); printf ( "Enter Upper:"); scanf ( "%d", &upper); printf ( "Fahr celsius\n"); /* Display table header *//* temperature conversion */for (Fahr = Lower, Fahr <= Upper, Fahr + +); Celsius = 5/9 * (Fahr-32.0); printf ( "%3.0f%6.1f\n", Fahr, Celsius); return 0;}         

Compile the source program, use the method of modifying the first error and recompile each time, log the error message of each error, parse the cause of the error, and give the correct statement.

Error One

No comma behind for

Correction method: Delete the comma

Error two

The for post-parenthesis statement applies a semicolon-delimited

Correction method change the comma in parentheses to the component number

Error three

The body of the statement behind for is not bracketed

Correction method: Enclose the body of a for-after statement in curly braces

Error Four

Fahr Apply%d Celsius to the shaping variable should be a decimal

Correction method: printf ("%d%.1f\n", Fahr, Celsius);

Error Five

Only 1 of each cycle is added

Correction Method:

Error Six

Calculation Formula error

Correction Method:

(ii) Study summary

1. The following procedures are available:

#include <stdio.h> int main  () {int i,num,odd,even; odd = 0; even = 0; for (i = 1; I <= 10;i++) {scanf ( "%d", &num); if (num% 2 = 0) {even++;} else {odd++;}} printf ( "%d%d", even,odd); return 0;}         

Detailed description of the program's execution, 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.

To perform the steps:
1. Defining integer variables
2. Assigning initial values to odd and even 0
3. Execute a For statement
(1) Execution of I=1;
(2) Enter num
(3) Execute if statement, if true, execute even++; if False, execute odd++
(4) For loop, when i=2, execute (2) (3)
(5) for the For Loop until i=11, the values for the output even and odd are not met for the for loop condition
The function of the program experiment:
Enter 10 integers to determine that there are several even numbers in 10 integers, several odd

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?

#include <stdio.h>int main(){ int i,j; i = 2; j = 2; i++; ++j; printf("i = %d\n",i); printf("j = %d\n",j); return 0; }

When the increment operator is used alone, the i++ executes the other statements first and then the 1;++j is added 1 before the other statements are executed.

(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?

#include <stdio.h>int main(){ int i,j; i = 2; j = 2; printf("%d\n",i++); printf("%d\n",++j); return 0; }

Operation Result:

Execution process:
1. Performing an integer definition
2. Implementation of i=2,j=2;
3. Value of Output I
4. Implementation of i++;
5. Execute the ++J and add the value of J to 1
6. Output J
Description of the issue:
i++ is the assignment, plus the 1;++J is the first plus 1, then the assignment.
After two output statements are executed, the values of I and J are all 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)

#include <stdio.h>int main(){ int i; for(i = 1;i <= 5;i++) printf("*"); printf("\n"); return 0; }

Operation Result:

(2)

#include <stdio.h>int main(){ int i; for(i = 1;i <= 5;i++) printf("*"); printf("\n"); return 0; }

Operation Result:

(3)

#include <stdio.h>int main(){ int i; for(i = 1;i <= 5;i++) { printf("*"); printf("\n"); } return 0; }

Operation Result:

(4)

#include <stdio.h>int main(){ int i; for(i = 1;i <= 5;i++) { printf("*"); } printf("\n"); return 0; }

Operation Result:

(1) (2) The result is the same, so the identification of the loop body is not related to the form of the Indentation, (3) (4) The results of the operation is different, (3) (4) the contents of {} In the program is not the same, and the loop structure executes the contents of {}, so the identification of the loop body
If a loop statement is appended with {}, then the statement loop body is the contents of {}, if the loop statement is not followed by {}, then the loop body statement is the first statement after the loop statement.
(1) (3) (4) is correct

(2) It's wrong.

7-2 average student scores and passing numbers (10 points)

#include <stdio.h>
int main ()
{
int n,x=0,i;
Double a[10001],sum=0;
scanf ("%d", &n);
for (i=0;i<n;i++)
{
scanf ("%lf", &a[i]);
if (a[i]>=60) x + +;
Sum+=a[i];
}
if (n==0) printf ("average = 0.0\n");
else printf ("average =%.1f\n", sum/n);
printf ("Count =%d\n", x);
return 0;
}

7-4 is not too fat (15 points)

#include <stdio.h>
#include <math.h>
struct student{
int Sg,tz;
Double BZ;
}STU[20];
int main () {
int num,i;
Double C;
scanf ("%d", &num);
for (i=0;i<num;i++) {
scanf ("%d%d", &stu[i].sg,&stu[i].tz);
stu[i].bz=0;
}
for (i=0;i<num;i++) {
stu[i].bz= (stu[i].sg-100) *0.9;
c=stu[i].bz-(double) stu[i].tz/2.0;
if (Stu[i].bz*0.1>fabs (c)) {
printf ("You are WAN mei!\n");
}
if (C>0&&stu[i].bz*0.1<=fabs (c)) {
printf ("You are Tai Shou le!\n");
}
if (C<0&&stu[i].bz*0.1<=fabs (c)) {
printf ("You are Tai Pang le!\n");
}
}
return 0;
}

7-6 Statistics Student achievement (10 points)

#include <stdio.h>
int main (void)
{
int N;
int i,x;
int a=0,b=0,c=0,d=0,e=0;
while (scanf ("%d", &n)!=eof) {

for (i=0;i<n;i++) {
scanf ("%d", &x);

if (x>=90) {
a++;
}else if (x>=80) {
b++;
}else if (x>=70) {
C + +;
}else if (x>=60) {
d++;
}else{
e++;
}
}
printf ("%d%d%d%d\n", a,b,c,d,e);

}
return 0;
}

7-8 Find the minimum value (10 points)

#include <stdio.h>
int main (void)
{
int n,i,l,min;
scanf ("%d", &n);
scanf ("%d", &l);
Min=l;
for (i=1;i<n;i++) {
scanf ("%d", &l);
if (min>l)
Min=l;
}
printf ("min =%d", min);
return 0;
}

C Language Programming fifth time-cycle structure (1)

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.