1. 程式人生 > >簡單程式設計題

簡單程式設計題

使用函式求兩個複數的積

#include <stdio.h>

double result_real,result_imag;

void complex_prod(double real1,double imag1,double real2,double imag2);

int main(void)

{

    double imag1,imag2,real1,real2;

    printf("Enter 1st complex number(real and imaginary):");

    scanf("%lf%lf",&real1,&imag1);

    printf("Enter 2nd complex number(real and imaginary):");

    scanf("%lf%lf",&real2,&imag2);

    complex_prod(real1,imag1,real2,imag2);

    printf("product of complex is %f+%fi\n",result_real,result_imag);

    return 0;

}

void complex_prod(double x1,double y1,double x2,double y2)

{

    result_real=x1*x2-y1*y2;

    result_imag=x1*y2+x2*y1;

}


求矩形各行元素之和:輸入兩個正整數

mnm>=1,n<=6),然後輸入該mn列二位陣列a中的元素,分別求出各行元素之和並輸出。

#include <stdio.h>

int main()

{

    int a[6][6],i,j,m,n,sum;

    printf("Input m,n:");

    scanf("%d%d",&m,&n);

    printf("Input array:\n");

    for(i=0;i<m;i++)

        for(j=0;j<n;j++)

            scanf("%d",&a[i][j]);

            sum=0;

    for(i=0;i<m;i++){

            sum=0;

        for(j=0;j<n;j++)

            sum=sum+a[i][j];

        printf("sum of row %d is %d\n",i,sum);

    }

    return 0;

}