45 דק׳

תרגול: printf ו־scanf

התרגילים של טום ל־printf ול־scanf: ניבוי פלט, עיצוב תווית, קליטה והדפסה חוזרת, משימת רישום הכרטיס, ותיקון קוד שבור של scanf ו־printf (שאלות 31-34 בחוברת).

באנגלית בלבד

השיעור הזה כתוב באנגלית בלבד.

Questions 1 to 6 come from the lessons printf and scanf. Questions 31 to 34 keep their numbers from the workbook "Variables, Arithmetic, printf, and scanf".

For every program: use int main(void), match each conversion to its variable's type, and test with the example plus one more input of your own. In Visual Studio, put #define _CRT_SECURE_NO_WARNINGS on line 1 of every program that uses scanf. Predict the result and write your program before you open an answer.

printf

1. Predict the output

Write the exact output of this code. Count the lines, include punctuation and spaces, and apply the precision. Then explain why %% needs no value.

printf("A\nB\n");
printf("Value: %d\n", 12);
printf("Voltage: %.1f V\n", 4.96);
printf("Progress: 75%%\n");
Answer

נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

    2. Bench status label

    Write a program that prints exactly this label:

    Bench 4
    Status: READY
    Accuracy: 99.50%

    Rules: use printf, use at least two calls, use the newline escape, and print the percent sign correctly. Save, build, run, and compare the output character by character.

    Answer

    נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

      scanf

      3. Temperature

      On paper: write one scanf call that reads a double named temperature, then one printf call that displays it with one decimal place. What is printed when the user types 23.46?

      Answer

      נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

        4. Board record (team mission)

        A technician needs to confirm a controller board before it goes to environmental testing. Write a program that:

        • reads the board ID as int,
        • reads the measured voltage as double,
        • reads the channel letter as char,
        • prints a labeled record in exactly this shape (the values come from the input):
        --- BOARD RECORD ---
        ID: 417
        Voltage: 4.98 V
        Channel: B

        Acceptance tests: every input has a prompt; all three values are stored; the voltage shows exactly two decimals; the channel is read correctly after the numbers; the labels and line breaks match; the program builds without errors. Test with 417 4.98 B, with 0 0 a, and with 9001 12.345 Z.

        Work in pairs: the driver types, the navigator checks types, conversions and tests. Switch after the first build.

        Answer

        נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

          5. OperatorBadge (home practice)

          Create a new project named OperatorBadge. Read an integer operator ID and one shift letter. Echo both values with clear labels. Before you run it, write down the output you expect; then test twice with different inputs.

          Answer

          נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

            Debugging

            6. Four clues, four repairs

            This code has four kinds of bugs. Find each one, fix it, then save, build and test.

            int board_id;
            double voltage;
            char channel;
             
            scanf("%d", board_id);
            scanf("%f", &voltage);
            scanf("%c", &channel);
            printf("Board %f | %.2d V | %d\n",
                   board_id, voltage, channel);

            Clues: which destination lacks &? Which double input needs %lf? Which %c may read the Enter? Which output conversions do not match?

            Answer

            נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

              31. Repair the average program

              Find and fix the missing addresses, the wrong order of operations, and the mismatched output conversion. Decimal averages must work: for 7 8 the program must print Average: 7.50.

              int first;
              int second;
              double average;
               
              scanf("%d %d", first, second);
              average = first + second / 2;
              printf("Average: %d\n", average);
              Answer

              נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                32. Repair the rectangle program

                Correct the input conversions, the perimeter formula, and the swapped output variables. For the input 3 4 the program must print the area 12.00 and the perimeter 14.00.

                double width, height;
                double area;
                double perimeter;
                 
                scanf("%d %d", &width, &height);
                area = width * height;
                perimeter = 2 * width + height;
                printf("Area: %.2f\n", perimeter);
                printf("Perimeter: %.2f\n", area);
                Answer

                נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                  33. Repair the time-conversion program

                  Correct the scanf destination and the calculations for minutes and remaining seconds. For 367 the program must print 6 minutes and 7 seconds.

                  int total_seconds;
                  int minutes;
                  int seconds;
                   
                  scanf("%d", total_seconds);
                  minutes = total_seconds % 60;
                  seconds = total_seconds / 60;
                   
                  printf("Minutes: %d\n", minutes);
                  printf("Seconds: %d\n", seconds);
                  Answer

                  נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.

                    34. Repair the temperature program

                    Correct the variable type, the format specifiers and the arithmetic so that decimal Celsius values work. For 36.6 the program must print Fahrenheit: 97.88.

                    int celsius;
                    double fahrenheit;
                     
                    scanf("%lf", &celsius);
                    fahrenheit = celsius * 9 / 5 + 32;
                    printf("Fahrenheit: %d\n", fahrenheit);
                    Answer

                    נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.