• Chapter2 Introducting C-Programming Exercises


    Reading about C isn’t enough. You should try writing one or two simple programs to see whether writing a program goes as smoothly as it looks in this chapter. A few suggestions follow, but you should also try to think up some problems yourself. You’ll find answers to selected programming exercises on the publisher’s website. 
     


    1.    Write a program that uses one  printf()  call to print your first name and last name on one line, uses a second  printf()  call to print your first and last names on two separate lines, and uses a pair of  printf()  calls to print your first and last names on one line. The output should look like this (but using your name): 

    Gustav Mahler   // First print statement 
    Gustav          // Second print statement
    Mahler          // Still the second print statement  
    Gustav Mahler   // Third and fourth print statements     
    #include "stdio.h"
    
    int main() {
        printf("Chintsai Hwo
    ");
        printf("Chintsai
    Hwo
    ");
        printf("Chintsai");
        printf(" Hwo");
        return 0;
    }

    2.    Write a program to print your name and address.  

    #include "stdio.h"
    
    int main() {
        printf("Chintsai Hwo
    ");
        printf("China");
        return 0;
    }

     
    3.    Write a program that converts your age in years to days and displays both values. At this point, don’t worry about fractional years and leap years. 

    #include "stdio.h"
    
    int main() {
        int age;
        int day;
        age = 23;
        day = age * 365;
        printf("An age of %d years is %d days
    ", age, day);
        return 0;
    }

    4.    Write a program that produces the following output: 

    For he's a jolly good fellow!  

    For he's a jolly good fellow!  

    For he's a jolly good fellow!  

    Which nobody can deny!   

    Have the program use two user-defined functions in addition to  main() : one named 
     jolly()  that prints the “jolly good” message once, and one named  deny()  that prints the final line once.   

    #include "stdio.h"
    
    void jolly(void);
    
    void deny(void);
    
    int main() {
        jolly();
        jolly();
        jolly();
        deny();
    }
    
    void jolly(void) {
        printf("For he is a jolly good fellow!
    ");
    }
    
    void deny(void) {
        printf("Which nobody can deny!
    ");
    }

     5.    Write a program that produces the following output: 

    Brazil, Russia, India, China

    India, China,

    Brazil, Russia   

    Have the program use two user-defined functions in addition to  main() : one named  br()  that prints “Brazil, Russia” once, and one named  ic()  that prints “India, China” once. Let  main()  take care of any additional printing tasks.   

    #include "stdio.h"
    
    void br(void);
    
    void ic(void);
    
    int main() {
        br();
        printf(", ");
        ic();
        printf("
    ");
        ic();
        printf(",
    ");
        br();
    }
    
    void br(void) {
        printf("Brazil, Russia");
    }
    
    void ic(void) {
        printf("India, China");
    }

     6.   Write a program that creates an integer variable called  toes . Have the program set  toes  to  10 . Also have the program calculate what twice  toes  is and what  toes  squared is. The program should print all three values, identifying them. 

    #include "stdio.h"
    
    int main() {
        int toes;
        toes = 10;
        printf("Toes is %d.
    Twice toes is %d.
    Toes squared is %d
    ", toes, toes ^ 2);
        return 0;
    }

    7.    Many studies suggest that smiling has benefits. Write a program that produces the following output: 

    Smile!Smile!Smile!  

    Smile!Smile!  

    Smile!   

    Have the program define a function that displays the string  Smile!  once, and have the program use the function as often as needed. 

    #include "stdio.h"
    
    void smile(void);
    
    int main() {
        smile();
        smile();
        smile();
        printf("
    ");
        smile();
        smile();
        printf("
    ");
        smile();
    }
    
    void smile(void) {
        printf("Smile!");
    }

    8.  In C, one function can call another. Write a program that calls a function named  one_ three() . This function should display the word  one  on one line, call a second function named  two() , and then display the word  three  on one line. The function  two()  should display the word  two  on one line. The  main()  function should display the phrase starting now:  before calling  one_three()  and display  done!  after calling it. Thus, the output should look like the following: 

    starting now:  

    one

    two  

    three

    done!         

    #include "stdio.h"
    
    void one_three(void);
    
    void two(void);
    
    int main() {
        printf("starting now:
    ");
        one_three();
        printf("done!
    ");
        return 0;
    }
    
    void one_three(void) {
        printf("one
    ");
        two();
        printf("three
    ");
    }
    
    void two(void) {
        printf("two
    ");
    }
    
    苟利国家生死以, 岂因祸福避趋之
  • 相关阅读:
    简单查询
    Scott用户表
    记一次Sqoop抽数据异常
    Flink+Druid构建实时OLAP的探索
    客户端埋点实时OLAP指标计算方案
    Kafka服务不可用(宕机)问题踩坑记
    实时计算-多级订单金额,及下级人数
    Apache Druid0.15.0安装方式
    superset安装文档
    Scala的常用小技巧
  • 原文地址:https://www.cnblogs.com/chintsai/p/10291572.html
Copyright © 2020-2023  润新知