複素数代入解(C言語)

このQ&Aのポイント
  • 複素数に代入するプログラムを作成したがエラーが出てしまう
  • 多項式の係数と複素数を代入して計算するプログラムを作成
  • 複素数の定義と演算を行い、代入計算を行っているがエラーが発生
回答を見る
  • ベストアンサー

複素数代入解(C言語)

n次の実多項式に複素数を代入するプログラムを教えてください。 typedef struct{ double r; double i; }comp; /*複素数の和*/ comp c_add(comp a,comp b){ comp c; c.r = a.r + b.r; c.i = a.i + b.i; return(c); } /*複素数のべき乗*/ comp c_pow(comp a,int n){ comp c; int i; double tpr,tpi; c.r = a.r; c.i = a.i; for(i=1;i<n;i++){ tpr = c.r; tpi = c.i; c.r = tpr*a.r - tpi*a.i; c.i = tpr*a.i + tpi*a.r; } return(c); } を定義してから、 /*a[]は多項式の係数、nは多項式の次数、bは代入する複素数*/ comp c_poly_eval(double a[], int n, comp b){ int i; comp tmp={0.0,0.0}; for(i=n; i>=0; i--){ tmp = c_add(tmp, a[i]*c_pow(b,i)); } return(tmp); } を作ったのですがエラーが出てしまいます。 a[i]*c_pow(b,i)でn次の値を計算して、c_addにいれることで0次~n次までの和を求めようとしたのですが、何が違うのでしょうか?

  • muhmi
  • お礼率100% (2/2)

質問者が選んだベストアンサー

  • ベストアンサー
  • mtaka2
  • ベストアンサー率73% (867/1179)
回答No.2

エラーが出ているのは > a[i]*c_pow(b,i) この部分ですね。 「double型とcomp型とのかけ算」なんて演算はどこにも定義されてませんので、 その2つに「*」演算子を適用することはできません。 comp型同士の足し算用に c_add 関数を作ったように、 double型×comp型を計算する c_mul 関数を作って、 > tmp=c_add(tmp, c_mul(a[i], c_pow(b,i))); のようにしましょう。

muhmi
質問者

お礼

なるほど! 構造体のメンバがdoubleなので、 メンバを各々かけてくれるものだと思ってました(汗 無事、プログラムできました。ありがとうございました。

その他の回答 (1)

  • asuncion
  • ベストアンサー率33% (2126/6286)
回答No.1

> エラーが出てしまいます。 コンパイル時ですか?実行時ですか? エラーメッセージは? もし、当方でコンパイルしてみる必要があるならば、 main関数の定義などを含むコード全体を載せてください。

muhmi
質問者

お礼

コンパイルに失敗です^^; エラーメッセージは書くべきでした。。すいません。 main自体は、複素数代入解を求めるものではなくて、 伝達関数(有理多項式)から周波数応答を求めるプログラムなので、 書いてません。というか、わからなくて載せられません(笑

関連するQ&A

  • C言語:2つの複素数(分数)の四則演算

    下記のプログラムを組んでみて、発展として分数の形で複素数の四則演算のプログラムを作りたいのですが、どうにもややこしく、困っています。 よろしければ御指導よろしくお願いします。 /* 複素数を表す構造体 2つの複素数の四則演算 */ #include <stdio.h> #include <stdlib.h> typedef struct { double real; /* 実数部分 */ double imag; /* 虚数部分 */ }COMPLEX; /* 二つの複素数の和を返す */ COMPLEX comp_add(COMPLEX x, COMPLEX y) { COMPLEX tmp; tmp.real = x.real + y.real; /* 実数部分の和 */ tmp.imag = x.imag + y.imag; /* 虚数部分の和 */ return (tmp); } /* 二つの複素数の差を返す */ COMPLEX comp_sub(COMPLEX x, COMPLEX y) { COMPLEX tmp; tmp.real = x.real - y.real; /* 実数部分の差 */ tmp.imag = x.imag - y.imag; /* 虚数部分の差 */ return (tmp); } /* 二つの複素数の積を返す */ COMPLEX comp_mul(COMPLEX x, COMPLEX y) { COMPLEX tmp; tmp.real = (x.real * y.real) - (x.imag * y.imag); tmp.imag = (x.real * y.imag) + (y.real * x.imag); return (tmp); } /* 二つの複素数の商を返す */ COMPLEX comp_div(COMPLEX x, COMPLEX y) { COMPLEX tmp; tmp.real =(x.real*y.real+x.imag*y.imag)/(y.real*y.real+y.imag*y.imag); tmp.imag = (x.imag*y.real-x.real*y.imag)/(y.real*y.real+y.imag*y.imag); return (tmp); } int main( int argc, char **argv ) { COMPLEX a, b, c; a.real = strtod( argv[1], NULL ); a.imag = strtod( argv[2], NULL ); b.real = strtod( argv[3], NULL ); b.imag = strtod( argv[4], NULL ); c = comp_add(a, b); /* 複素数の和を c に代入 */ printf( "(%f+j%f)*(%f+j%f)=(%3.1lf+j%3.1lf)\n", a.real,a.imag,b.real,b.imag,c.real,c.imag ); c = comp_sub(a, b); /* 複素数の差を c に代入 */ printf( "(%f+j%f)*(%f+j%f)=(%3.1lf+j%3.1lf)\n", a.real,a.imag,b.real,b.imag,c.real,c.imag ); c = comp_mul(a, b); /* 複素数の積を c に代入 */ printf( "(%f+j%f)+(%f+j%f)=(%3.1lf+j%3.1lf)\n", a.real,a.imag,b.real,b.imag,c.real,c.imag ); c = comp_div(a, b); /* 複素数の商を c に代入 */ printf( "(%f+j%f)+(%f+j%f)=(%3.1lf+j%3.1lf)\n", a.real,a.imag,b.real,b.imag,c.real,c.imag ); return( 0 ); }

  • C言語における複素数の四則演算について

    複素数の四則演算(a+biとc+diの四則演算)について、for文を用いて表示するプログラムについて、???の部分に何を入れたらよいかわからず、うまく実行することができません。和・差・積・商の計算種別を入れるみたいなのですが、何を入れたらいいのかわかりません。 #include <stdio.h> void fukuso(double a,double b,double c,double d,double *e,double *f,int keisan); int main(void) { double a=4, b=8, c=4, d=3, e, f; int i; for(i=1;i<5;i++){ fukuso(a,b,c,d,&e,&f,???); if(i==1) printf("和演算\n"); else if(i==2) printf("差演算\n"); else if(i==3) printf("積演算\n"); else printf("商演算\n"); printf("e=%f f=%f i\n",e,f); } return (0); } void fukuso(double a1,double b1,double a2,double b2,double *a3,double *b3,int keisan) { if(keisan==1){ *e=a+c; *f=a+c; } else if(keisan==2){ *e=a-c; *f=b-d; } else if(keisan==3){ *e=a*c-b*d; *f=a*d+c*b; } else{ *e=(a*c+b*d)/(c*c+d*d); *f=(-a*d+c*b)/(c*c+d*d); } }

  • C言語に直して下さい

    VisualC++で円周率を求めるプログラムなのだそうですが、 自分はC言語しか使ったことがないため、よく分かりません。 Cでコンパイルできるように直していただけないでしょうか。 よろしくお願いします。 #include <stdio.h> #include <stdlib.h> #include <string.h> #define N 21 #define K 100000 void add(unsigned long a[],unsigned long b[]) { int c = 0, tmp; for (int i = N - 1; i > -1; i--) { tmp = a[i] + b[i] + c; a[i] = tmp % K; c = tmp / K; } } void sub(unsigned long a[],unsigned long b[]) { int c = 0, x = 0; for (int i = N - 1; i > -1 ; i--) { x = c; if (a[i] < b[i] + x ) c = 1; else c = 0; a[i] = c * K + a[i] - b[i] - x; } } void div(unsigned long a[], unsigned long x) { int c=0, tmp; if (x >= K) { printf("Div Error\n"); getchar(); exit(1); return; } for (int i = 0; i < N ; i++) { tmp = (a[i] + c * K) / x; c = (a[i] + c * K) % x; a[i] = tmp; } } void clear(unsigned long a[]) { memset(a,0x00,sizeof(a)*N); } void set(unsigned long a[], unsigned long b[]) { memcpy(a,b,sizeof(a)*N); } void set(unsigned long a[], unsigned long b) { if (b<K) { clear(a); a[0]=b; } } int _tmain(int argc, _TCHAR* argv[]) { unsigned long pai[N], fn[N], gn[N], tmp1[N], tmp2[N]; int i; unsigned long n; clear(pai); clear(fn); clear(gn); clear(tmp1); clear(tmp2); set(fn, 16*5); set(gn, 4*239); for(n=0;n<40000;n++) { div(fn, 25); div(gn, 239); div(gn, 239); set(tmp1, fn); div(tmp1, 2*n+1); set(tmp2, gn); div(tmp2, 2*n+1); if (n%2==0) { add(pai, tmp1); sub(pai, tmp2); }else{ add(pai, tmp2); sub(pai, tmp1); } } for (i=0;i<N;i++) { printf("%5lu ", pai[i]); } getchar(); return 0; }

  • C言語でまったくわからないところがあります。

    C言語でまったくわからないところがあります。 配列を格納して、その中の最小値をA[9]と入れ替える動作がしたいのですが、 #include<stdio.h> int main(void) { double A[10],min,tmp; int i; for(i=0;i<=9;i++) { printf("A[%d]=",i); scanf("%lf",&A[i]); } printf("\n"); min=A[0]; for(i=0;i<=9;i++) { if(A[i]<min) { min=A[i]; } } tmp=A[i]; A[i]=A[9]; A[9]=tmp; for(i=0;i<=9;i++) { printf("A[%d]=%.0lf\n",i,A[i]); } return 0; } これを実行すると、A[9]=最小値にはなるのですが、A[i]はそのまま入れ替わらないのです。 誰か、どのように直せばよいか、教えていただけないでしょうか。よろしくお願いします。

  • c言語です。

    c言語です。 実行結果 式 3 X1 + 2 X2 + 1 X3 = &g 2 X1 + 5 X2 + 2 X3 = &g 1 X1 + 4 X2 + 1 X3 = &g 解 X1 = 1 X2 = 2 X3 = 3 を 式 3 X1 + 2 X2 + 1 X3 = 10 2 X1 + 5 X2 + 2 X3 = 18 1 X1 + 4 X2 + 1 X3 = 12 解 X1 = 1 X2 = 2 X3 = 3 に直したいのですが&gの所をどのようにしたら10.18.12になりますか? #include <stdio.h> #include <float.h> #define N 3 double A[N][N] = {{3,2,1}, {2,5,2}, {1,4,1}}; double b[N] = { 10, 18, 12 }; void Gauss_J( int, double*, double* ); void main(void) { int i; printf( "%d式\n", N ); for( i = 0; i < N ; i++ ) { printf( "%g X1 + %g X2 + %g X3 = &g \n", A[i][0], A[i][1], A[i][2], b[i] ); } printf("解\n"); Gauss_J(N, (double *)A, (double *)b ); printf("X1 = %g \n", b[0]); printf("X2 = %g \n", b[1]); printf("X3 = %g \n", b[2]); } void Gauss_J(int n, double *a, double *b) { int p, i, j,I ; double pivot, c ; for ( p = 0 ; p < n ; p++ ) { pivot = a[ p*n + p ]; for ( i = p ; i < n ; i++ ) { a[ p*n + i ] /= pivot; } b[ p ] /= pivot; for ( I = 0 ; I < n ; I++) { if (I != p) { c = a[ I*n + p]; for ( j = p ; j < n; j++ ) { a[ I*n + j] -= c * a[ p*n + j ]; } b[ I ] -= c * b[ p ]; } } } return ; }

  • C言語の参照はずしについて

    ソートのプログラムなんですが #include <stdio.h> #include <stdlib.h> int comp(const void *, const void *); int main() { int i; int test[6] = {10, 8, 2, 6, 4, 0}; qsort(test, (size_t)6, sizeof(int), comp); printf("\n"); for (i = 0; i < 6; i++) printf("%d\n", test[i]); return 0; } int comp(const void *a, const void *b) { static int i = 1; printf("%02d--%d,%d\n", i, *(int *)a, *(int *)b); i++; return (*(int *)a - *(int *)b); } 最後のreturnの()の中身がよくわかりません。「参照はずし」という事をしてるらしいんですが「参照はずし」とは何ですか意味も教えてください。

  • C言語について質問です。

    前に質問した訂正です。前の質問に関しては質問の意図が伝わりにくい文章で本当に申し訳ないと思っています。 乱数列の要素数Nの値を変えていきバブルソートの交換回数、比較回数を数えるプログラムを作り、後は処理時間について調べたいのですが、処理時間を出力させることはできたんですが、単位がわかりません。教えてください。 以下に乱数を生成するrand.cとバブルソートを行うbubblesort.cを記載します。 rand.c #include <stdio.h> #include <stdlib.h> #include <time.h> #define N 1000 int num[N]; int makeDataFile ( void ) { int i; FILE *fp; char s[100]; int num[N]; srand ( ( unsigned )time ( NULL ) ); fp = fopen ("rand1.txt", "w" ); if ( fp == NULL ) exit(1); for ( i = 0; i < N; i++ ){ fprintf ( fp, "%d\n", rand()%100 ); } fclose ( fp ); fp = fopen ( "rand1.txt", "r" ); if ( fp == NULL ) exit(1); while( fgets ( s, sizeof (s), fp ) ) { printf ( s ); } fclose ( fp ); return N; } bubblesort.c #include <stdio.h> #include <time.h> extern int makeDataFile ( void ); extern int num[]; void BubbleSort ( int x[] , int n ); void Show ( int x[] , int n ); int comp; int swap; void BubbleSort ( int x[] , int n ) { int i, j, tmp; for ( i = 0; i < n-1; i++ ) { for ( j = n-1; j > i; j-- ){ comp++; if ( x[i] > x[j] ){ swap++; tmp = x[j]; x[j] = x[i]; x[i]= tmp; Show ( x , n ); } } } } void Show ( int x[] , int n ) { while ( n-- ) printf ( "%d " , *x++ ); printf ( "\n" ); } int main(void) { int i, j, n , tmp; FILE *fp; comp = 0; swap = 0; n = makeDataFile(); clock_t start , finish; double duration; start = clock(); fp = fopen ( "rand1.txt", "r" ); if ( fp == NULL ) return 1; for ( i = 0; i < n; i++ ){ fscanf ( fp, "%d", &(num[i] ) ); } fclose ( fp ); printf ( "\nbefore bubblesort\n" ); Show ( num , n ); printf ( "\n" ); printf ( "progress bubblesort\n" ); BubbleSort ( num , n ); printf ( "\n" ); printf ( "after bubblesort\n" ); Show ( num , n ); printf ( "\n" ); finish = clock(); duration = (double)(finish-start) / CLOCKS_PER_SEC; printf ( "count of comparisons : %d\n" , comp ); printf ( "count of swap : %d\n" , swap ); printf ( "%lf\n" , duration ); return 0; } 実行結果: >gcc rand.c bubblesort.c (ソートは省略) count of comparisons : 499500 count of swap : 14848 2.950000 と出力されたのですが読み方?単位が分かりません。教えてください。自分の答えとしては2分55秒だと思うんですが合ってますか?連続質問ですいません。

  • C言語について質問です。

    ソートについて勉強していて、乱数列の要素数Nの値を変えていきバブルソートの交換回数、比較回数を数えるプログラムを作り、後は処理時間について調べたいのですが、処理時間を出力するのはどうやってやるのですか?教えてください。以下に乱数を生成するrand.cとバブルソートを行うbubblesort.cを記載します。これに処理時間を出力するようにしてもらいたいのですが、どうしたらいいですか?解説とソースファイルをよろしくお願いします。 rand.c #include <stdio.h> #include <stdlib.h> #include <time.h> #define N 1000 int num[N]; int makeDataFile ( void ) { int i; FILE *fp; char s[100]; int num[N]; srand ( ( unsigned )time ( NULL ) ); fp = fopen ("rand1.txt", "w" ); if ( fp == NULL ) exit(1); for ( i = 0; i < N; i++ ){ fprintf ( fp, "%d\n", rand()%100 ); } fclose ( fp ); fp = fopen ( "rand1.txt", "r" ); if ( fp == NULL ) exit(1); while( fgets ( s, sizeof (s), fp ) ) { printf ( s ); } fclose ( fp ); return N; } bubblesort.c #include <stdio.h> #include <time.h> extern int makeDataFile ( void ); extern int num[]; void BubbleSort ( int x[] , int n ); void Show ( int x[] , int n ); int comp; int swap; void BubbleSort ( int x[] , int n ) { int i, j, tmp; for ( i = 0; i < n-1; i++ ) { for ( j = n-1; j > i; j-- ){ comp++; if ( x[i] > x[j] ){ swap++; tmp = x[j]; x[j] = x[i]; x[i]= tmp; Show ( x , n ); } } } } void Show ( int x[] , int n ) { while ( n-- ) printf ( "%d " , *x++ ); printf ( "\n" ); } int main(void) { int i, j, n , tmp; FILE *fp; comp = 0; swap = 0; n = makeDataFile(); fp = fopen ( "rand1.txt", "r" ); if ( fp == NULL ) return 1; for ( i = 0; i < n; i++ ){ fscanf ( fp, "%d", &(num[i] ) ); } fclose ( fp ); printf ( "\nbefore bubblesort\n" ); Show ( num , n ); printf ( "\n" ); printf ( "progress bubblesort\n" ); BubbleSort ( num , n ); printf ( "\n" ); printf ( "after bubblesort\n" ); Show ( num , n ); printf ( "\n" ); printf ( "count of comparisons : %d\n" , comp ); printf ( "count of swap : %d\n" , swap ); return 0; }

  • C言語について質問です。

    ソートについて勉強していて、乱数列の要素数Nの値を変えていきバブルソートの交換回数、比較回数を数えるプログラムを作り、後は処理時間について調べたいのですが、処理時間を出力させることはできたんですが、単位がわかりません。教えてください。 以下に乱数を生成するrand.cとバブルソートを行うbubblesort.cを記載します。 rand.c #include <stdio.h> #include <stdlib.h> #include <time.h> #define N 1000 int num[N]; int makeDataFile ( void ) { int i; FILE *fp; char s[100]; int num[N]; srand ( ( unsigned )time ( NULL ) ); fp = fopen ("rand1.txt", "w" ); if ( fp == NULL ) exit(1); for ( i = 0; i < N; i++ ){ fprintf ( fp, "%d\n", rand()%100 ); } fclose ( fp ); fp = fopen ( "rand1.txt", "r" ); if ( fp == NULL ) exit(1); while( fgets ( s, sizeof (s), fp ) ) { printf ( s ); } fclose ( fp ); return N; } bubblesort.c #include <stdio.h> #include <time.h> extern int makeDataFile ( void ); extern int num[]; void BubbleSort ( int x[] , int n ); void Show ( int x[] , int n ); int comp; int swap; void BubbleSort ( int x[] , int n ) { int i, j, tmp; for ( i = 0; i < n-1; i++ ) { for ( j = n-1; j > i; j-- ){ comp++; if ( x[i] > x[j] ){ swap++; tmp = x[j]; x[j] = x[i]; x[i]= tmp; Show ( x , n ); } } } } void Show ( int x[] , int n ) { while ( n-- ) printf ( "%d " , *x++ ); printf ( "\n" ); } int main(void) { int i, j, n , tmp; FILE *fp; comp = 0; swap = 0; n = makeDataFile(); fp = fopen ( "rand1.txt", "r" ); if ( fp == NULL ) return 1; for ( i = 0; i < n; i++ ){ fscanf ( fp, "%d", &(num[i] ) ); } fclose ( fp ); printf ( "\nbefore bubblesort\n" ); Show ( num , n ); printf ( "\n" ); printf ( "progress bubblesort\n" ); BubbleSort ( num , n ); printf ( "\n" ); printf ( "after bubblesort\n" ); Show ( num , n ); printf ( "\n" ); printf ( "count of comparisons : %d\n" , comp ); printf ( "count of swap : %d\n" , swap ); return 0; } 実行結果: (ソートは省略) count of comparisons : 499500 count of swap : 14848 2.950000 と出力されたのですが読み方?単位が分かりません。教えてください。2分ぐらいかかった気がします。

  • C言語

    main() { int a = 5,b=2,e,f,g=3,i; double c,d,h=2.0; c = a/b; printf("c = %f\n",c); d = a/h; printf("d=%f\n",d); e = a++; f = ++b; g+= 4; i= ++a + b; printf(" a = %d\n",a); printf(" b = %d\n"b); printf(" e = %d\n"e); printf(" f = %d\n"f); printf(" g = %d\n"g); printf(" i = %d\n" i); 答え a=7,b=3,c=2.0,d=2.5,e=5,f=3,g=7,i=10 この問題のa,b,iがどうやってこの値になるのかがわかりません解説お願いします。