kExercise 5.4 Table of reciprocals, squares, cubes, and fourth powers.

Exercise 5-4. Define a two-dimensional array, data[11][5] , of type double. Initialize
the elements in the first column with values from 2.0 to 3.0 inclusive in steps of 0.1.
If the first element in a row has value x, populate the remaining elements in each row
with the values 1/x, x2, x3, and x4 . Output the values in the array with each row on a
separate line and with a heading for each column.

 

//Exercise 5.4 Table of reciprocals, squares, cubes, and fourth powers.
#include <stdio.h>

int main(void)
{
  const size_t nrows = 11;              // Number of rows in the array
  const size_t ncols = 5;               // Number of columns in the array
  double data[nrows][ncols];            // Stores data values
  double value = 2.0;
  size_t row = 0 ;                 // Value to be stored in array
  size_t col = 0 ;

  for( row = 0 ; row < nrows ; ++row)
    {
    data[row][0] = value;
    data[row][1] = 1.0/data[row][0];               // 1/x
    data[row][2] = data[row][0]*data[row][0];      // x*x
    data[row][3] = data[row][2]*data[row][0];      // x*x*x
    data[row][4] = data[row][3]*data[row][0];      // x*x*x*x
        value += 0.1;
  }


  printf("            x  ");
  printf("          1/x  ");
  printf("          x*x  ");
  printf("         x*x*x ");
  printf("        x*x*x*x");

    for( row = 0 ; row < nrows ; ++row)
  {
    printf("\n");
    for( col = 0 ; col < ncols ; ++col)
      printf("%15.4lf", data[row][col]);
  }

  printf("\n");
    return 0;
}

 

转载于:https://www.cnblogs.com/xiaomi5320/p/4190246.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值