powers.cpp

来自「《C/C++程序设计导论(第二版)》一书的程序源文件」· C++ 代码 · 共 30 行

CPP
30
字号
// powers.cpp   A program to prepare and reference a 2-d array of integer powers//  ASSUMPTIONS: the integers 0 through 9 are represented by powers 1 through 4#include <iostream.h>// A function to fill a 2-d table of 10 rows with powers 1 through 4void FillTable (int tab[][5]){	int row, col, product, n;	for (row=0; row<10; row++)		for (col = 0; col<5; col++)		{	product = 1;  		// each cell is the integer value times itself a `col'			for (n=0; n<col; n++)   //  number of times				product *= row;			tab[row][col] = product;		}}void main(){	int table[10][5],	// table of integer powers		intval, power;	// requested integer value and power (from user)	FillTable (table);	cout << "Enter integer (0 to 9) and requested power (1 to 4): ";	cin >> intval >> power;	cout << intval << " raised to a power of " << power << "  is " << table[intval][power];}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?