二級C程序設計(二)

字號:

試題說明 :
    ===========================================
    函數(shù)fun的功能是:把a數(shù)組中的n個數(shù)和b數(shù)組中逆序的n個數(shù)
    一一對應相加、求平方,結果存放在c數(shù)組中。
    例如:
    若a數(shù)組中的值是:1、3、5、7、8
    b數(shù)組中的值是:2、3、4、5、8
    調(diào)用該函數(shù)后,c中存放的數(shù)據(jù)是81、64、81、100、100
    注意: 部分源程序存在文件PROG1.C中。
    請勿改動主函數(shù)main和其它函數(shù)中的任何內(nèi)容,僅在函數(shù)fun
    的花括號中填入你編寫的若干語句。
    ===========================================
    程序 :
    ===========================================
    #include
    #include
    void fun(int a[], int b[], int c[], int n)
    {
    }
    main()
    { int i, a[100]={1,3,5,7,8}, b[100]={2,3,4,5,8}, c[100];
    clrscr();
    fun(a, b, c, 5);
    printf("The result is: ");
    for (i=0; i<5; i++) printf("%d ", c[i]);
    printf("\n");
    NONO();
    }
    NONO ( )
    {/* 本函數(shù)用于打開文件,輸入數(shù)據(jù),調(diào)用函數(shù),輸出數(shù)據(jù),關閉文件。 */
    FILE *rf, *wf ;
    int a[100], b[100], c[100], i, j ;
    rf = fopen("in.dat", "r") ;
    wf = fopen("bc04.dat","w") ;
    for(i = 0 ; i < 5 ; i++) {
    for(j = 0 ; j < 5 ; j++) fscanf(rf, "%d,", &a[j]) ;
    for(j = 0 ; j < 5 ; j++) fscanf(rf, "%d,", &b[j]) ;
    fun(a, b, c, 5) ;
    for(j = 0 ; j < 5 ; j++) fprintf(wf, "%d ", c[j]) ;
    fprintf(wf, "\n") ;
    }
    fclose(rf) ;
    fclose(wf) ;
    }
    ===========================================
    所需數(shù)據(jù) :
    ===========================================
    @2 IN.DAT 010
    1,2,3,4,5
    6,7,8,9,10
    2,3,5,6,7
    1,4,6,7,9
    11,7,2,4,8
    8,7,2,3,5
    9,10,3,4,7
    8,11,4,6,8
    4,3,1,9,7
    3,4,8,10,9
    #E
    @3 $BC04.DAT 005
    121 121 121 121 121
    121 100 121 100 64
    256 100 16 121 256
    289 256 49 225 225
    169 169 81 169 100
    #E