• Chapter 3 : Data and C


    1.    Find out what your system does with integer overflow, floating-point overflow, and floating-point underflow by using the experimental approach; that is, write programs having these problems.

    #include <stdio.h>
    
    int main(void) {
        int integerNum = 1234567890987654;
        float floatNum = 1234567890987654321.0f;
        float smallNum = 0.00001;
    
        printf("%d
    ",integerNum);
        printf("%f
    ",floatNum);
        printf("%f
    ",smallNum/200);
        return 0;
    }

    The output on my PC is:

    1016588934
    1234567939550609408.000000
    0.000000

    2.    Write a program that asks you to enter an ASCII code value, such as 66, and then prints the character having that ASCII code.  

    #include <stdio.h>
    
    int main(void) {
        char ch;
    
        printf("Enter the character: ");
        scanf("%c", &ch);
        printf("%c's ASCII code is %d.
    ", ch, ch);
    
        return 0;
    }

    4.    Write a program that reads in a floating-point number and prints it first in decimal-point notation, then in exponential notation, and then, if your system supports it, p notation. Have the output use the following format (the actual number of digits displayed for the exponent depends on the system): 

    Enter a floating-point value:  64.25   
    fixed-point notation: 64.250000 
    exponential notation: 6.425000e+01  
    p notation: 0x1.01p+6 
    #include <stdio.h>
    
    int main(void) {
        float number;
    
        printf("Enter a float-pointing number:");
        scanf("%f", &number);
        printf("fixed-point notation: %f
    ", number);
        printf("exponential notation: %e
    ", number);
        printf("p notation: %a
    ", number);
    }

    5.    There are approximately 3.156 × 10 7  seconds in a year. Write a program that requests your age in years and then displays the equivalent number of seconds.  

    #include <stdio.h>
    
    #define seconds_in_a_year 3.156e7
    
    int main(void) {
        int age;
    
        printf("Enter your age in years:");
        scanf("%d", &age);
        printf("Your age in seconds is %lld", age * seconds_in_a_year);
    }

    6.    The mass of a single molecule of water is about 3.0×10 -23  grams. A quart of water is about 950 grams. Write a program that requests an amount of water, in quarts, and displays the number of water molecules in that amount.    

    #include <stdio.h>
    
    #define mass_of_a_single_molecule_of_water 3.0e-23
    
    int main(void) {
        int amount;
    
        printf("Enter the amount of water in quarts:");
        scanf("%d", &amount);
        printf("The number of water molecules: %lld", amount * 950 / mass_of_a_single_molecule_of_water);
    
        return 0;
    }

    7.    There are 2.54 centimeters to the inch. Write a program that asks you to enter your height in inches and then displays your height in centimeters. Or, if you prefer, ask for the height in centimeters and convert that to inches.    

    #include <stdio.h>
    
    int main(void) {
        int inch;
    
        printf("Enter your height in inches:");
        scanf("%d", &inch);
        printf("%d inches is %.2f centimeters.
    ", inch, inch * 2.54);
    
        return 0;
    }

    8.    In the U.S. system of volume measurements, a pint is 2 cups, a cup is 8 ounces, an ounce is 2 tablespoons, and a tablespoon is 3 teaspoons. Write a program that requests a volume in cups and that displays the equivalent volumes in pints, ounces, tablespoons, and teaspoons. Why does a floating-point type make more sense for this application than an integer type?

    #include <stdio.h>
    
    int main(void) {
        float cup;
    
        printf("Enter the volume in cups:");
        scanf("%f", &cup);
    
        float pint = cup / 2;
        float ounce = cup * 8;
        float tablespoon = ounce * 2;
        float teaspoon = tablespoon * 3;
    
        printf("%f cups is %f pints, or %f tablespoons,or %f teaspoons", cup, pint, tablespoon, teaspoon);
    
        return 0;
    }
    苟利国家生死以, 岂因祸福避趋之
  • 相关阅读:
    递归和迭代
    The Rose
    读周国平作品有感
    matlab最小二乘法数据拟合函数详解
    读周国平作品有感
    three.js之创建一条直线
    three.js之创建一个几何体
    Go语言标准库之strconv
    Go语言基础之网络编程
    Go语言基础之并发
  • 原文地址:https://www.cnblogs.com/chintsai/p/10291566.html
Copyright © 2020-2023  润新知