C Language Sixth time assignment

Source: Internet
Author: User

(a) Correct the wrong question
1. Error message:
Error reason: The form of do is wrong
Correction: The while (item < EPS) is followed by a semicolon.
2. Error message:
Error reason: The absolute value of the last item is less than the end of EPs, want to loop should be >=eps
Correct method: Change Item < EPS to Item>=eps.
3. Error message:
Error Reason: Item is defined as double type
Correction method: Change 1 to 1.0
4. Error message:
Error Reason: Item is defined as double type
Correction method: Input to change the%f into%LF.
(ii) Study summary
1.while (1) and for (;;) is an infinite loop, with break.
2. (1) The number of cycles is known, with a for statement.

(2) The number of cycles is unknown, but the loop condition is clear when entering the loop, using the while statement.

(3) The number of cycles is unknown, and the loop condition is unknown when entering the loop, it needs to be clear in the loop body, with a do while statement.

3. (1) For statement

#include <stdio.h> int main(void){ int  n,i,sum; double average; sum=0; i=1; scanf("%d",&n); for(; ;) {    if(n!=-1)    {        sum=sum+n;        i++;    }    else    {        break;    }    scanf("%d",&n); } average=(double)sum/i; printf("%f",average); return 0;}

(2) While statement

#include <stdio.h>int main(void){ int n,i,sum; double average; n=0; sum=0; i=0; while(n!=-1) {    scanf("%d",&n);    sum=sum+n;    i++; }    average=(double)sum/i;    printf("%f",average);    return 0;}

(3) Do While statement

#include <stdio.h>int main(void){ int n,sum,i; double average ; sum=0; i=1; scanf("%d",&n); do {    sum=sum+n;    i++;           scanf("%d",&n);       }while(n!=-1); average=(double)sum/i; printf("%f",average); return 0;}

4. (1)
Break is not eligible to jump directly out of the loop
(2)
Continue is not eligible to end this cycle.
(iii) Experimental summary

    1. The simple staggered sequence part of the given precision and
      (1) Title
      The subject asks to write the program, computes the sequence part and 1-1/4 + 1/7-1/10 + ... The absolute value of the last item is not greater than the given precision EPs.
      (2) Flowchart


      (3) Source code

      Include include

      int main (void)
      {
      int n;
      Double eps,sum,b,c;
      scanf ("%lf", &eps);
      N=1;
      sum=0;
      C=1;
      if (c<=eps)
      {
      Sum=1;
      }
      Else
      {
      while (Fabs (c) >eps)
      {
      b=3.0n-2;
      c=1.0/b
      Pow ( -1,n-1);
      Sum=sum+c;
      n++;

      }
      }

      printf ("sum =%f", sum);
      return 0;

      }

(4) Experimental analysis
Issue 1: Run wrong
Reason: EPS compared with EPS
Workaround: Redefine a quantity versus EPS comparison.
(5) PTA Submission List

    1. Guess number Game
      (1) Title
      Guess the number game is to make game console randomly produce a positive integer within 100, the user enters a number to guess, you need to write a program automatically compare it with the randomly generated guessed number, and the hint is big ("Too big"), or small ("Too small"), equal to guess. If guessed, the program is closed. The program also requires the number of statistical guesses, if 1 times to guess the number, the hint "bingo!" If the number is guessed within 3 times, it prompts "Lucky you!" If the number is more than 3 times but is within N (>3) (including nth), it prompts "good guess!" If you don't guess more than n times, prompt "Game over" and end the program. If the user enters a negative number before reaching n times, it also outputs "Game over" and ends the program.
      (2) Flowchart


      (3) Source code

      #include

        }  else if(i<a)  {      printf("Too big\n");  }  else  {      if(n==1)      {          printf("Bingo!\n");          break;      }      else if(n>1&&n<=3)      {          printf("Lucky You!\n");          break;      }      else if(n>3&&n<=N)      {          printf("Good Guess!\n");          break;      }      else      {          printf("Game Over\n");          break;      }  }

      }
      return 0;
      }

(4) Experimental analysis
Issue 1: Compilation error
Reason: The first else is the meaning of a=b, I in the judgment in the time I wrote again a=b.
Workaround: Remove the B=a
(5) PTA Submission List

    1. Find Odd and
      (1) Title
      The subject requires the calculation of odd numbers in a given series of positive integers and
      (2) Flowchart

      (3) Source code

      Include

      int main (void)
      {
      int a,sum;
      sum=0;
      scanf ("%d", &a);
      Do
      {

       if(a<0) {     break; } else if(a%2!=0) {     sum=sum+a; } scanf("%d",&a);

      }
      while (a>0);
      printf ("%d", sum);
      return 0;
      }
      (4) Experimental analysis
      Issue 1: Compilation error
      Cause: There should be a semicolon behind while (a>0)
      Workaround: while (a>0);
      (5) PTA Submission List

      (iv) Mutual evaluation of blogs
      1, Yaoyuan: http://www.cnblogs.com/yjy751522356/p/7838296.html
      2. Wang Yingdan: http://www.cnblogs.com/windsky-1999/p/7838107.html
      3. Lu Yu: http://www.cnblogs.com/luyu0322-/p/7846404.html

C Language Sixth time assignment

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.