0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

書籍 ダイテル C言語プログラミング 練習問題 解答 9章

0
Last updated at Posted at 2025-12-21
書籍

image.png

9.7: 1から1000までの乱数を10要素の整数型配列numberに格納するプログラム(この配列の各要素の値をプリントしながら、それまでにプリントした文字数の合計を表示)
source
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int i, j;
    int n;          // %n用
    int count = 0;  // 出力された文字数の合計
    int value[10];
    int space;

  
    for (i = 0; i < 10; i++)
    {
       value[i] = rand() % 1000 + 1;
    }

    printf("%-8s%-8s\n", "値", "合計文字数");

    for (i = 0; i < 10; i++)
    { 
        printf("%-d%n", value[i], &n);

        // 空白を何個分出力するかを計算
        space = 7 - n;
 
        for (j = 0; j < space; j++)
        {
             printf(" ");
        }

        count += n;
        printf(" %-8d\n", count);
    }
    getch();
    return 0;
}
9.8: scanf文における変換仕様%dと%iの違いを調べるプログラム
source
#include <stdio.h>
#include <conio.h>

int main()
{
    unsigned x;
    int y;

    scanf("%i%d", &x, &y);
    printf("%d %d\n", x, y);

    getch();
    return 0;
}
9.9: すべての整数変換指定子と変換仕様%pを使って、ポインタの値をプリントするプログラム
source
#include <stdio.h>
#include <conio.h>

int main()
{
    int a;

    printf("%p\n", &a);
    printf("%d\n", &a);
    printf("%i\n", &a);
    printf("%ld\n", &a);
    printf("%u\n", &a);
    printf("%o\n", &a);
    printf("%x\n", &a);
    getch();
    return 0;
}
9.10: 整数値12345と浮動小数点1.2345をさまざまなフィールド幅でプリントするプログラム
source
#include <stdio.h>
#include <conio.h>

int main()
{
    printf("%4d\n", 12345);
    printf("%5d\n", 12345);
    printf("%6d\n", 12345);

    printf("%5lf\n", 1.2345);
    printf("%6lf\n", 1.2345);
    printf("%7lf\n", 1.2345);

    getch();
    return 0;
}
9.11: 浮動小数点値100.453627をそれぞれ小数点以下1桁、2桁、3桁、4桁、5桁に丸めて(四捨五入して)プリントするプログラム
source
#include <stdio.h>
#include <conio.h>
#include <math.h>

// 四捨五入を行う関数
double sisyagonyu(double, int);

int main()
{
    printf("%lf\n", sisyagonyu(100.453627, 1));
    printf("%lf\n", sisyagonyu(100.453627, 2));
    printf("%lf\n", sisyagonyu(100.453627, 3));
    printf("%lf\n", sisyagonyu(100.453627, 4));
    printf("%lf\n", sisyagonyu(100.453627, 5));
    getch();
    return 0;
}

// 四捨五入を行う
// 引数 四捨五入される値  第何桁に丸め込むか
// 戻り値 四捨五入された値
double sisyagonyu(double value, int power)
{
    return floor(value * pow(10, power) + 0.5) / pow(10, power);
}
9.12: キーボードから読み込んだ文字列の長さを求め、それの2倍の幅フィールドに読み込んだ文字列をプリントするプログラム
source
#include <stdio.h>
#include <conio.h>
#include <string.h>

#define MAX 100

int main()
{
    int length;
    char s[MAX];
    char outPut[MAX];

    printf("文字列を入力してください\n");
    scanf("%s", s);

    length = strlen(s);
  
    //printf("%d\n", length);

    sprintf(outPut, "%%%ds", 2 * length);
    printf(outPut, s);
    printf("\n");
    getch();
    return 0;
}
9.13: 0から212度までのカ氏温度をそれぞれ10桁のフィールドに右詰めでプリントし、セ氏温度に変換するプログラム
source
#include <stdio.h>
#include <conio.h>

double changeCelsiusToFahr(int);

int main()
{
    int fahr;
    double celsius;

    printf("%10s%10s\n", "fahr", "celsius");
    for (fahr = 0 ; fahr <= 212; fahr++)
    {
        printf("%10d%+10.3lf\n", fahr, changeCelsiusToFahr(fahr));
    }
    getch();
    return 0;
}

double changeCelsiusToFahr(int fahr)
{
    return 5.0 / 9.0 * (fahr - 32);
}
9.14: 表9.5のエスケープシーケンスをすべてテストするプログラム
source
#include <stdio.h>
#include <conio.h>

int main()
{
    printf("\'\n");
    printf("\"\n");
    printf("\?\n");
    printf("\\\n");
    printf("ブザー\n");
    printf("\a");
    printf("ブザー終了\n");
    printf("後退");
    printf("\b");

    printf("改勾\n");
    printf("\f");
    printf("改行\n");
    printf("\n");
    printf("*******");
    printf("復帰");
    printf("\r\n");
    printf("タブ");
    printf("\t");
    printf("タブ");
  
    printf("\n");
    printf("垂直タブ");
    printf("\v");
    printf("垂直タブ");
    getch();
    return 0;
}
9.15: printfの書式制御文字列の中に、(エスケープシーケンス?ではなく) リテラル文字として?(疑問符)を書いても正しくプリントされるかどうかをテストするプログラム
source
#include <stdio.h>
#include <conio.h>

#include <stdio.h>

int main(void) {
    printf("Is this printed correctly? Yes, it is.\n");
    printf("Question mark test: ? ? ?\n");
    printf("Format test: %d + %d = %d ?\n", 1, 2, 3);
    getch();
    return 0;
}
9.16: 整数値437をscanfの各整数変換指定子を使って入力するプログラム
source
#include <stdio.h>
#include <conio.h>

int main()
{
    int a;
    
    scanf("%d", &a);
    printf("%d\n", a);

    scanf("%i", &a);
    printf("%i\n", a);

    scanf("%u", &a);
    printf("%u\n", a);

    scanf("%o", &a);
    printf("%o\n", a);

    scanf("%x", &a);
    printf("%x\n", a);
    
    getch();
    return 0;
}
9.17: 3つの変換指定子e, f, gを使って、浮動小数点1.2345を入力するプログラム
source
#include <stdio.h>
#include <conio.h>

int main()
{
    long double e, g, f;

    printf("1.2345を入力してください\n");
    
    scanf("%e", &e);
    fflush(stdin);
    scanf("%g", &g);

    fflush(stdin);
    scanf("%lf", &f);

    printf("%.30lf\n", e);
    printf("%.30lf\n", g);
    printf("%.30lf\n", f);
    getch();
    return 0;
}
9.18: 3つの形式の文字列「suzy」、「"suzy"」、「'suzy'」を読み込むプログラム(シングルクォートやダブルクォートは無視されるか、それとも文字列の一部として読み込まれるか?)
source
#include <stdio.h>
#include <conio.h>

int main()
{
    char s[100];

    scanf("%s", s);

    printf("%s\n", s);

    getch();
    return 0;
}
9.19: printfの書式制御文字列の中で変換指定子%cを書いて、(エスケープシーケンスの文字定数'?'ではなく)文字定数'?'をプリントできるかどうかをテストするプログラム
source
#include <stdio.h>
#include <conio.h>

int main()
{
    printf("%c\n", '?');
    getch();
    return 0;
}
9.20: 変換指定子gを使って浮動小数点9876.12345を表示するプログラム
source
#include <stdio.h>
#include <conio.h>

int main()
{
    char format[100];
    int i;

    for (i = 1; i <= 9; i++)
    {
       sprintf(format, "%%.%dg\n", i);
       printf(format,  9876.12345);
    }

    getch();
    return 0;
}
ポータルサイト
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?