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?

More than 3 years have passed since last update.

お前らに配列と関数を叩き込んでやる。

Last updated at Posted at 2022-06-22

配列
https://c-lang.sevendays-study.com/day5.html

関数
https://c-lang.sevendays-study.com/day6.html

problem1.c
#include <stdio.h>

#define N 5

int main(void)
{
    int i, psum, msum, a[N];
    double pmratio;

    // 入力
    for (i = 0; i < N; i++){
        printf("a[%d] = ", i);
        scanf("%d", &a[i]);
    }
    // 正数の二乗和と負数の二乗和の比の計算

    // 結果の出力
    printf("pmratio = %8.4f\n", pmratio);

    return 0;
}
サンプルコード
answer1.c

#include <stdio.h>

#define N 5

int main(void)
{
    int i, psum, msum, a[N];
    double pmratio;

    // 入力
    for (i = 0; i < N; i++){
        printf("a[%d] = ", i);
        scanf("%d", &a[i]);
    }

    // 正数の二乗和と負数の二乗和の比の計算

    // 変数宣言のときに int psum = 0;のように初期値も同時に宣言してもよい
    psum = 0;
    for (i = 0; i < N; i++){
        if(a[i] > 0){
            // この場合()を付けなくても大丈夫だが、不安なときは付けたほうが良い→演算子優先順位を見る
            psum = psum + (a[i] * a[i]);
        }
    }

    msum = 0;
    for (i = 0; i < N; i++){
        if(a[i] < 0){
            msum = msum + (a[i] * a[i]);
        }
    }

    // キャストしなければならない。intとintの計算では小数が消えてしまう。
    pmratio = (double)psum / (double)msum;

    // 結果の出力
    printf("pmratio = %8.4f\n", pmratio);

    return 0;
}
problem2.c
#include <stdio.h>

#define N 5

// INSERT HERE

int main(void)
{
    int a[N];

    // ここでa[0]~a[N-1] の値を入力する
    printf("oddprod = %2d/n",oddprod(a));

    return 0;
}
サンプルコード
answer2.c

ミスがあったため、模範解答を参照。

problem3.c
#include <stdio.h>

#define N 5

int main(void)
{
    int i, min, a[N];

    // 入力
    for (i = 0; i < N; i++){
        printf("a[%d] = ",i);
        scanf("%d", &a[i]);
    }
    // 奇数の最小値の計算

    // 結果の出力
    printf("min = %d\n", min);

    return 0;
}
サンプルコード
answer3.c
#include <stdio.h>

#define N 5

int main(void)
{
    int i, min, a[N];

    // 入力
    for (i = 0; i < N; i++){
        printf("a[%d] = ",i);
        scanf("%d", &a[i]);
    }
    
    // 奇数の最小値の計算
    min = 10000;
    for (i = 0; i < N; i++){
        if(a[i]%2 == 1){
            if(a[i] < min){
                min = a[i];
            }
        }
    }

    // 結果の出力
    printf("min = %d\n", min);

    return 0;
}
problem4.c
#include <stdio.h>

#define N 5

// INSERT HERE

int main(void)
{
    int a[N];

    //  ここで a[0]~a[N-1] の値を入力する
    printf("minusmax = %d\n", minusmax(a));
    
    return 0;
}
サンプルコード
answer4.c
#include <stdio.h>

#define N 5

int minusmax(int a[N])
{
    int i, ans;

    ans = -10000;
    for (i = 0; i < N; i++)
    {
        // if(a[i]<0&&a[i]>ans) が一番エレガント→条件式 論理演算子 論理積
        if (a[i] < 0)
        {
            if (a[i] > ans)
            {
                ans = a[i];
            }
        }
    }

    // エレガントではない
    if(ans == -10000){
        return 0;
    }else{
        return ans;
    }
}

int main(void)
{
    int a[N];
    int i;

    //  ここで a[0]~a[N-1] の値を入力する
    for (i = 0; i < N; i++)
    {
        printf("a[%d] = ", i);
        scanf("%d", &a[i]);
    }

    printf("minusmax = %d\n", minusmax(a));

    return 0;
}
problem5.c
#include <stdio.h>
#include <math.h>

#define N 5

// INSERT HERE

int main(void)
{
    double x[N], y[N];

    //  ここで x[0]~x[N-1], y[0]~y[N-1] の値を入力する

    printf("distmin = %8.4f\n", distmin(x,y));

    return 0;
}
answer5.c
#include <stdio.h>
#include <math.h>

#define N 5

double distmin(double x[N], double y[N])
{
    double sum_x, sum_y, avg_x, avg_y, d;
    int i;

    sum_x = 0;
    sum_y = 0;
    for (i = 0; i < N; i++)
    {
        sum_x = sum_x + x[i];
        sum_y = sum_y + x[i];
    }

    avg_x = sum_x / N;
    avg_y = sum_y / N;

    d = 1000;

    for (i = 0; i < N; i++)
    {
        d = sqrt((x[i] - avg_x) * (x[i] - avg_x) + (y[i] - avg_y) * (y[i] - avg_y));
    }

    return d;
}

int main(void)
{
    double x[N], y[N];
    int i;

    for (i = 0; i < N; i++)
    {
        printf("x[%d] = ", i);
        scanf("%lf", &x[i]);
        printf("y[%d] = ", i);
        scanf("%lf", &y[i]);
    }

    printf("distmin = %8.4f\n", distmin(x, y));

    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?