ページ 11

ヨッシー

Posted: 2012年11月25日(日) 22:24
by 関数の返却 おねがいします 
#include<stdio.h>
#include <math.h>
int main (void)
{int x,y;
printf("2つの数x,yを入力してください");
scanf("%d,%d,&x,&y");
printf("相加=%f\n相乗=%f\n",X,Y");
return 0;
}

int souka_soujou (void)
float X,Y;
X=x+y/2
Y=sqrt(x+y)
return 0;
}

#include<stdio.h>
#include <math.h>
int main (void)
{
printf("長方形の長さa,bを入力してください");
scanf("%d,%d,&a,&b");
printf("対角線の長さ=%f\n面積=%f\n",L,S");
return 0;
}

int calc_rect (void)
float L,S,A,B;
S=a*b
A=pow(a,2)
B=pow(b,2)
L=sqrt(A+B)
return 0;
}
宿題なんですがグローバル変数使用禁止で」計算は、違う関数のところでするとおいうのが条件なんですが
ここまでしかできません誰かおしえてくださいおねがします

Re: ヨッシー

Posted: 2012年11月25日(日) 23:08
by nil
コードタグを使ってください。
まず、
これは何をするプログラムですか?
引数や返り値の使い方はわかりますか?

Re: ヨッシー

Posted: 2012年11月25日(日) 23:26
by box
2個とも、scanf()の引数の書き方が正しくありません。
また、相加相乗平均を求める関数や長方形の対角線・面積を求める関数を
せっかく書いたのに全く呼び出していないことも大きな問題です。

ついでに、相加相乗平均を求める関数や長方形の対角線・面積を求める関数の書き方自体も
正しくないです。

Re: ヨッシー

Posted: 2012年11月25日(日) 23:53
by box
私なら例えばこう書く(相加平均・相乗平均編)

コード:

#include <stdio.h>
#include <math.h>

void souka_soujou(int x, int y, double *souka, double *soujou)
{
    *souka  = ((double) x + (double) y) / 2;
    *soujou = sqrt((double) x * (double) y);
}

int main(void)
{
    int x, y;
    double souka, soujou;

    printf("整数x:"), scanf("%d", &x);
    printf("整数y:"), scanf("%d", &y);
    souka_soujou(x, y, &souka, &soujou);
    printf("相加平均=%f,相乗平均=%f\n", souka, soujou);
    return 0;
}

Re: ヨッシー

Posted: 2012年11月26日(月) 00:02
by box
私なら例えばこう書く(長方形編)

コード:

#include <stdio.h>
#include <math.h>

void rectangle(int x, int y, double *taikakusen, int *menseki)
{
    *taikakusen  = sqrt((double) x * (double) x + (double) y * (double) y);
    *menseki     = x * y;
}

int main(void)
{
    int x, y, menseki;
    double taikakusen;

    printf("長方形の縦(整数):"), scanf("%d", &x);
    printf("長方形の横(整数):"), scanf("%d", &y);
    rectangle(x, y, &taikakusen, &menseki);
    printf("対角線=%f,面積=%d\n", taikakusen, menseki);
    return 0;
}

Re: ヨッシー

Posted: 2012年11月26日(月) 00:35
by box
別解(相加平均・相乗平均編)

コード:

#include <stdio.h>
#include <math.h>

struct souka_soujou {
    double souka;
    double soujou;
};

struct souka_soujou souka_soujou(int x, int y)
{
    struct souka_soujou s;

    s.souka  = ((double) x + (double) y) / 2;
    s.soujou = sqrt((double) x * (double) y);
    return s;
}

int main(void)
{
    int x, y;
    struct souka_soujou s;

    printf("整数x:"), scanf("%d", &x);
    printf("整数y:"), scanf("%d", &y);
    s = souka_soujou(x, y);
    printf("相加平均=%f,相乗平均=%f\n", s.souka, s.soujou);
    return 0;
}

Re: ヨッシー

Posted: 2012年11月26日(月) 00:40
by box
別解(長方形編)

コード:

#include <stdio.h>
#include <math.h>

struct rectangle {
    double taikakusen;
    int menseki;
};

struct rectangle rectangle(int x, int y)
{
    struct rectangle rect;

    rect.taikakusen = sqrt((double) x * (double) x + (double) y * (double) y);
    rect.menseki    = x * y;
    return rect;
}

int main(void)
{
    int x, y;
    struct rectangle rect;

    printf("長方形の縦(整数):"), scanf("%d", &x);
    printf("長方形の横(整数):"), scanf("%d", &y);
    rect = rectangle(x, y);
    printf("対角線=%f,面積=%d\n", rect.taikakusen, rect.menseki);
    return 0;
}

Re: ヨッシー

Posted: 2012年11月26日(月) 13:40
by ヨッシー
みなさんありがとうございます
勉強しなおします