動態數組(表的構建和填充)

 #include<stdio.h>

#include<stdlib.h>
#include<limits.h>
 
int** buildTsble(void);
void fillTable(int** table);
void processTable(int** table);
int smaller(int first,int second);
int larger(int first,int second);
int rowMinimum(int* rowRtr);
int rowMaximum(int* rowPtr);
float rowAverage(int* rowPtr);
 
//動態數組(main) 
int main() {
int** table;
table=buildTsble();
fillTable(table);
processTable(table);
return 0;
}
 
//動態數組(buildTable) 
int** buildTsble(void) {
int rowNum;
int colNum;
int** table;
int row;
printf("\nEnter the number of rows in the table: ");
scanf("%d",&rowNum);
table=(int**)calloc(rowNum+1,sizeof(int*));
for(row=0;row<rowNum;row++) {
printf("Enter number of integers in row %d: ",row+1);
scanf("%d",&colNum);
table[row]=(int*)calloc(colNum+1,sizeof(int));
table[row][0]=colNum;
}
table[row]=NULL;
return table;
}
 
//動態數組(fillTable)
 void fillTable(int** table) {
  int row=0;
  printf("\n========================");
  printf("\nNow we fill the table.\n");
  printf("\nFor each row enter the data");
  printf("\nand press return: ");
  printf("\n=========================\n");
  while(table[row]!=NULL) {
printf("\nrow%d (%d integers)=====> ",row+1,table[row][0]);
for(int columm=1;columm<=*table[row];columm++)
scanf("%d",table[row]+columm);
  row++;
}
return ;
 }
 
 //動態數組:表處理
 void processTable(int** table) {
  int row=0;
  int rowMin;
  int rowMax;
  float rowAve;
  while(table[row]!=NULL) {
rowMin=rowMinimum(table[row]);
rowMax=rowMaximum(table[row]);
rowAve=rowAverage(table[row]);
printf("\nThe statistics for row %d ",row+1);
printf("\nThe minimum: %5d",rowMin);
printf("\nThe maximum: %5d",rowMax);
printf("\nThe average: %8.2f",rowAve);
row++;
}
return ;
 }
 
 //動態數組:查找最小值
 int rowMinimum(int* rowPtr) {
  int rowMin=INT_MAX;
 
  for(int columm=1;columm<=*rowPtr;columm++)
  rowMin=smaller(rowMin,*(rowPtr+columm));
return rowMin;
 }
 
 //動態數組:查找航最大值
 int rowMaximum(int* rowPtr) {
  int rowMax=INT_MIN;
 
  for(int columm=1;columm<=*rowPtr;columm++)
  rowMax=larger(rowMax,*(rowPtr+columm));
return rowMax;
 } 
 
 //動態數組:查找平均值
 float rowAverage(int* rowPtr) {
  float total=0;
  float rowAve;
 
for(int columm=1;columm<*rowPtr;columm++)
total+=(float)*(rowPtr+columm);
rowAve = total/ *rowPtr;
return rowAve;
 } 
 
//動態數組:查找更小值
int smaller(int first,int second) {
return (first<second?first:second);
 
//動態數組:查找更大值
int larger(int first,int second) {
return (first>second?first:second);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章