# C習題6-22 ###### tags: `C習題` ## 題目: >4個sales,5種產品, 每天每個Sales有0~5筆銷售資料.假設手上有上個月所有銷售資料,用一個二維陣列計算所有Sales對產品的每月銷售額,該Sales當月總銷售額,以及該產品之當月總銷售額.並印出報表. ## code: ```c= #include <stdio.h> #define SALES 4 /* there are 4 sales*/ #define GOODS 5 /* there are 5 products on sale*/ /*Function Prototype*/ void printArray(float array[GOODS][SALES],int col,int row); float readData(float talbe[GOODS][SALES],int g,int s); float initialArray(float array[GOODS][SALES], int col, int row); int main() /*to initialArray, read input data, print 1st row and print the rest array*/ { float t[GOODS][SALES] ; initialArray(t,GOODS,SALES); readData(t,GOODS,SALES); printf("%s%s%s%s%s\n","********","Sales[1]","Sales[2]","Sales[3]","Sales[4]"); printArray(t,GOODS,SALES); return 0; } float readData(float talbe[GOODS][SALES],int g,int s) /*in order to read input data stroke by stroke*/ { int sales,pnber; float amo; float table[GOODS][SALES] ; printf("Enter Sales Notes(To end input, key in '-1'):\n"); printf("Sales ID(1~4):\n"); scanf("%d",&sales); /*printf("%d\n",sales);*/ while(sales != -1) { printf("Product Number(1~5):\n"); scanf("%d",&pnber); /*printf("%d\n",pnber);*/ printf("Sales Amount(USD):\n"); scanf("%f",&amo); /*printf("%f\n",amo); printf("%f\t\n",table[pnber][sales]);*/ printf("Sales ID:\n"); scanf("%d",&sales); /*printf("%d\n",sales);*/ table[pnber][sales] += amo; /*in order to add amount in the coressponding coordinate*/ /* table[GOODS][SALES] = table[pnber][sales]; */ /*printf("%f\t\n",table[pnber][sales]);*/ } return table[GOODS][SALES]; } float initialArray(float array[GOODS][SALES], int col, int row) /*initial array*/ { int x, y; for(x=0;x<GOODS;x++) { for(y=0;y<SALES;y++) { array[x][y] = 0 ; } } return array[x][y]; } void printArray(float array[GOODS][SALES], int col, int row) /*print array*/ { int x, y; for(x=0;x<GOODS;x++) { printf("Good[%d]",x+1); for(y=0;y<SALES;y++) { printf("\t%6.2f",array[x][y]); } printf("\n"); } } ``` ## 心得 >建構中...print功能ok,正在處理輸入資料&分類儲存進二維陣列的部分。 >更新:仔細看了一下&重新查書發現幾個錯誤: >1. 使用函式原型叫用的方式後發現在27行的table有typo, 導致每次輸出結果都是陣列初始化之後的結果(全0); >2. readData()中, 陣列加總在第二次print sales之後執行, 導致加總到錯的格子;調整到之前就可以正常執行; >3. C陣列的第一行第一列是(0,0), 而Sales和goods沒有第0號, 使用這些數字會導致第一行與第一列都不會有值;將加總陣列內值各-1就可以印到正確的格子內.(第59行) >4. 函式原型調整順序與main()函式叫用的順序相同。 > >修改後程式碼如下: ## code:(v2) ```c= #include <stdio.h> #define SALES 4 /* there are 4 sales*/ #define GOODS 5 /* there are 5 products on sale*/ /*Function Prototype*/ float initialArray(float array[GOODS][SALES], int col, int row); float readData(float table[GOODS][SALES],int pnber,int sales); void printArray(float array[GOODS][SALES],int col,int row); int main() /*to initialArray, read input data, print 1st row and print the rest array*/ { float t[GOODS][SALES] ; initialArray(t,GOODS,SALES); readData(t,GOODS,SALES); printf("%s%s%s%s%s\n","********","Sales[1]","Sales[2]","Sales[3]","Sales[4]"); printArray(t,GOODS,SALES); return 0; } float initialArray(float array[GOODS][SALES], int col, int row) /*initial array*/ { int x, y; for(x=0;x<GOODS;x++) { for(y=0;y<SALES;y++) { array[x][y] = 0 ; } } return array[x][y]; } float readData(float table[GOODS][SALES],int pnber,int sales) /*in order to read input data stroke by stroke*/ { /*int sales,pnber;*/ float amo; /*float table[GOODS][SALES] ; */ printf("Enter Sales Notes(To end input, key in '-1'):\n"); printf("Sales ID(1~4):\n"); scanf("%d",&sales); /*printf("%d\n",sales);*/ while(sales != -1) { printf("Product Number(1~5):\n"); scanf("%d",&pnber); /*printf("%d\n",pnber);*/ printf("Sales Amount(USD):\n"); scanf("%f",&amo); /*printf("%f\n",amo); printf("%f\t\n",table[pnber][sales]);*/ table[pnber-1][sales-1] += amo; printf("Sales ID:\n"); scanf("%d",&sales); /*printf("%d\n",sales);*/ /*table[pnber][sales] += amo;*/ /*in order to add amount in the coressponding coordinate*/ /* table[GOODS][SALES] = table[pnber][sales]; */ /*printf("%f\t\n",table[pnber][sales]);*/ } return table[pnber][sales]; } void printArray(float array[GOODS][SALES], int col, int row) /*print array*/ { int x, y; for(x=0;x<GOODS;x++) { printf("Good[%d]",x+1); for(y=0;y<SALES;y++) { printf("\t%6.2f",array[x][y]); } printf("\n"); } } ``` ## 驗證資料儲存正確性: >題目提到每個sales手上每天都有0~5條銷售資料, 且繳到我手上累積一個月的數量 >簡單驗證每個sales每樣商品都有銷售,每個sales的紙條有5張,但銷售金額不同,不考慮哪一天(因對我而言是手上有一桶資料慢慢key) > >紙條: | Sales | 1 | 1 | 1 | 2 | 2 | 2 | 3 | 3 | 3 | 4 | 4 | 4 | 1 | 1 | 1 | | ----- | --- | --- | --- | --- | --- | --- | --- | --- |:--- | --- | --- | --- | --- | --- | --- | | P/N | 1 | 1 | 1 | 2 | 2 | 2 | 3 | 3 | 3 | 4 | 4 | 4 | 5 | 5 | 5 | | $ | 5 | 8 | 7 | 7 | 6 | 3 | 8 | 2 | 5 | 9 | 7 | 6 | 7 | 8 | 9 | >結果應該會是: | Sales | sales1 | Sales2 | Sales3 | Sales4 | |:------ | ------ | ------ | ------ |:------ | | goods1 | 20 | | | | | goods2 | | 16 | | | | goods3 | | | 15 | | | goods4 | | | | 22 | | goods5 | 24 | | | | >實際印出結果: ![Uploading file..._82j5l8tjw]()