プログラミング実習II (2025) 課題
[E11] 第9章 構造体とユーザ定義型
(E11_1) [リスト 6.14]ではもっとも離れた席の組を探すプログラムを扱ったが,[リスト 9.5] はそれを構造体の配列を用いて書いたプログラムである.[リスト9.5]を入力せよ.さらに,以下の変更を施した上でコンパイル・実行せよ.
typedef struct{
...
} position_t;
|
position_t seat[MAX_SEAT] = {
{ 1, 1.1, 5.2 },
{ 5, 3.4, 1.6 },
{ 8, 4.5, 3.4 },
{ 10, 2.3, 2.6 },
{ 15, 6.4, 5.7 },
{ 16, 7.6, 7.8 },
{ 20, 5.2, 4.4 },
{ 22, 1.7, 3.5 },
{ 25, 3.8, 6.3 },
{ 30, 5.8, 6.3 },
};
|
(E11_2) 次の手順に沿って,構造体を用いた複素数の演算を行うプログラムを作成せよ.
3.14000 + 2.43000 iのように表示する関数 complex_print を作成せよ.さらに,x を引数として main 関数から complex_print を呼び出せ.y についても同様.
#include <stdio.h>
void complex_print( complex_t a ){
if( a.im >= 0.0 )
printf(" %f + %f i \n", a.re, a.im);
else
printf(" %f - %f i \n", a.re, -a.im);
}
complex_t complex_add( complex_t a, complex_t b ){
}
complex_t complex_mul( complex_t a, complex_t b ){
}
int main ( void )
{
complex_t x, y;
return 0;
}
|
(E11_3) 教科書 9.4.2 項を読んだ上で,構造体へのポインタに関する以下の問題に解答せよ.
#include <stdio.h>
void complex_print( complex_t *a ){
}
int main ( void )
{
complex_t data;
complex_t *p;
return 0;
}
|
(E11_4) 関数への変数や構造体の受け渡しに関するプログラムについて以下に解答せよ.(プログラム c を提出すること)
(プログラム a)
#include <stdio.h>
void my_func( int x )
{
x += 5;
}
int main( void )
{
int x;
x = 3;
my_func( x );
printf(" x = %d \n", x );
return 0;
}
|
(プログラム b)
#include <stdio.h>
struct point {
int x;
int y;
};
void my_func(struct point p)
{
p.x += 5;
p.y += 5;
}
int main(void)
{
struct point p;
p.x = 1;
p.y = 2;
my_func(p);
printf("x = %d, y = %d\n", p.x, p.y);
return 0;
}
|
(プログラム c)
#include <stdio.h>
struct point {
int x;
int y;
};
void my_func(struct point *p)
{
(*p).x += 5;
p->y += 5;
}
int main(void)
{
struct point p;
p.x = 1;
p.y = 2;
my_func(&p);
printf("x = %d, y = %d\n", p.x, p.y);
return 0;
}
|
(プログラム d)
#include <stdio.h>
struct point {
int x;
int y;
};
void my_func(struct point p[])
{
int i;
for( i = 0; i < 2; i++ )
{
p[i].x += 5;
p[i].y += 5;
}
}
int main(void)
{
struct point p[2];
p[0].x = 1;
p[0].y = 2;
p[1].x = 3;
p[1].y = 4;
my_func( p );
printf("p[0].x = %d, p[0].y = %d\n", p[0].x, p[0].y);
printf("p[1].x = %d, p[1].y = %d\n", p[1].x, p[1].y);
return 0;
}
|
(E11_5*) 以下の通り,構造体の配列について計算を行うプログラムを作成せよ.
#include <stdio.h>
#define N 12
int main( void ){
league_t team[ N ] = {
{ "A", 9, 7, 2, 0.0, -1 },
{ "B", 8, 9, 1, 0.0, -1 },
{ "C", 5, 12, 1, 0.0, -1 },
{ "D", 6, 12, 0, 0.0, -1 },
{ "E", 7, 11, 0, 0.0, -1 },
{ "F", 7, 10, 1, 0.0, -1 },
{ "G", 9, 9, 0, 0.0, -1 },
{ "H", 10, 7, 1, 0.0, -1 },
{ "I", 9, 8, 1, 0.0, -1 },
{ "J", 10, 8, 0, 0.0, -1 },
{ "K", 12, 5, 1, 0.0, -1 },
{ "L", 12, 6, 0, 0.0, -1 },
};
return 0;
}
|