純粹貼代碼,使用VLA創建和計算任意二維數組的值的和

七月底去開個GayHub

/* 通過VLA計算多維數組所有數值的和 */
#define BUFSIZE 500
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
void chartrim(char cache[],int length);
double sumdarray(int rows,int cols,double array[rows][cols]);
int main(void)
{
  char cache[BUFSIZE];
  int rows,cols;
  printf("How many rows does your array have?\n");
  gets(cache);
  rows = atoi(cache);
  chartrim(cache,BUFSIZE);
  printf("How many columns does your array have?\n");
  gets(cache);
  cols = atoi(cache);
  chartrim(cache,BUFSIZE);
  double array[rows][cols];
  for(int i=0;i<rows;++i)
  {
    for(int n=0;n<cols;++n)
    {
      printf("Enter the data at position %d * %d in your array\n",i+1,n+1);
      gets(cache);
      array[i][n] = atoi(cache);
      chartrim(cache,BUFSIZE);
    }
  }
  printf("The total sum of your array is : %lf\n",sumdarray(rows,cols,array));
  return 0;
}
void chartrim(char string[],int length)
{
  for(int n=0;n<length;++n)
  {
    string[n] = 0;
  }
}
double sumdarray(int rows,int cols,double array[rows][cols])
{
  double result;
  result = 0;
  for(int i=0;i<rows;++i)
  {
    for(int n=0;n<cols;++n)
    {
      result += array[i][n];
    }
  }
  return result;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 只要我們活著一天,我們的思想、行動就停不下來。人類的思想是人類痛苦的根源。因為,人總是在逃避現實。人們根據過去的經...
    DrDan閱讀 1,164評論 0 4
  • 天空不會說話,它生氣有雷,難過有雨,煩躁會很熱,冷酷會很冷。所有人都圍著它轉悠,多好。你會說話。生氣,難過的時候有...
    1D_3180閱讀 254評論 0 0
  • 現在的生活特別狼狽,每天有同樣的煩惱,可是現在的我,慢慢被生活打磨著,改變著,每天手忙腳亂的開始,又精疲力盡的結束...
    攸雅閱讀 148評論 1 0
  • 早上九點起床 沒干正事,只把三國志都傳到腦洞文庫上了。 下午看韓劇吳海英,睡覺 晚上看檸檬初上 準備明天的答辯 希...
    Tillyren閱讀 206評論 0 0