我将如何使用 if 语句输出此乘法 table ?
How would I output this multiplication table with an if statement?
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
char response = 'y';
while (response == 'y')
do
{
const int myarray = 144;
int thearray[myarray];
for (int m = 0; m < 12; m++)
{
cout << m + 1 << " ";
}
cout << endl;
for (int rown = 1; rown < 11; rown++)
for (int n = 0; n < 12; n++)
{
thearray[(rown * 12) + n] = thearray[n] * (rown + 1);
}
if ()
cout << "Would you like to run the program again? \n"
<< "Enter y for yes or n for no: ";
cin >> response;
} while (response == 'Y' || response == 'y');
return 0;
}
此代码的要点是使用一维数组创建乘法 table。我相信我已经正确编码了所有内容,但此作业的另一部分是使用 if 语句输出 table,我不完全确定如何输出。有人可以给我一些指导吗?
我不知道 C 的功能,但我认为它会像这样工作(仅思考过程/逻辑,而不是实际代码)
nums = array(1, 2, 3, 4, 5)
output space for left display column of numbers
foreach (nums as header) { // this hopefully builds the top of the table
output header; // column header
}
for each (nums as row) { // this will go through 1,2,3,4,5
if (nums === multiplier) output left row label // i.e "1 | "
for each (nums as col) {
output (row * col) // multiply row * col and display it
}
}
不需要 If 语句...希望对您有所帮助。
对不起,如果代码不完整,我不怎么用 c 编程。
编辑:
为了理智,我写了一些似乎有效的东西。学习新语言总是好的 :) ....让我知道你的想法。
https://ideone.com/Id9Ax9
这是我想出的对我有用的代码
#include <iostream>
using namespace std;
int main() {
// Define variables
int nums [5] = { 1, 2, 3, 4, 5};
// set default values
string headerRow = " ";
string rowLabel = "";
string currentRow = "";
string currentVal = "";
// start the process
int maxArray = (sizeof(nums)/sizeof(nums[0]));
for (int i=1; i<=maxArray; i++) { headerRow = headerRow + to_string(i) + " "; }
cout << headerRow + "\n"; // Display header row
cout << " ---------------\n";
for (int r=1; r<=maxArray; r++) { // this will cycle through rows 1,2,3,4,5
currentRow = "";
for (int c=1; c<=maxArray; c++) {// this will cycle through columns 1,2,3,4,5
if (r == c) {
rowLabel = to_string(r) + std::string(" | ");
}
currentVal = to_string(c * r) + " "; // multiply row * col and display it
currentRow = currentRow + currentVal;
}
cout << rowLabel + currentRow + "\n";
}
}
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
char response = 'y';
while (response == 'y')
do
{
const int myarray = 144;
int thearray[myarray];
for (int m = 0; m < 12; m++)
{
cout << m + 1 << " ";
}
cout << endl;
for (int rown = 1; rown < 11; rown++)
for (int n = 0; n < 12; n++)
{
thearray[(rown * 12) + n] = thearray[n] * (rown + 1);
}
if ()
cout << "Would you like to run the program again? \n"
<< "Enter y for yes or n for no: ";
cin >> response;
} while (response == 'Y' || response == 'y');
return 0;
}
此代码的要点是使用一维数组创建乘法 table。我相信我已经正确编码了所有内容,但此作业的另一部分是使用 if 语句输出 table,我不完全确定如何输出。有人可以给我一些指导吗?
我不知道 C 的功能,但我认为它会像这样工作(仅思考过程/逻辑,而不是实际代码)
nums = array(1, 2, 3, 4, 5)
output space for left display column of numbers
foreach (nums as header) { // this hopefully builds the top of the table
output header; // column header
}
for each (nums as row) { // this will go through 1,2,3,4,5
if (nums === multiplier) output left row label // i.e "1 | "
for each (nums as col) {
output (row * col) // multiply row * col and display it
}
}
不需要 If 语句...希望对您有所帮助。 对不起,如果代码不完整,我不怎么用 c 编程。
编辑:
为了理智,我写了一些似乎有效的东西。学习新语言总是好的 :) ....让我知道你的想法。
https://ideone.com/Id9Ax9
这是我想出的对我有用的代码
#include <iostream>
using namespace std;
int main() {
// Define variables
int nums [5] = { 1, 2, 3, 4, 5};
// set default values
string headerRow = " ";
string rowLabel = "";
string currentRow = "";
string currentVal = "";
// start the process
int maxArray = (sizeof(nums)/sizeof(nums[0]));
for (int i=1; i<=maxArray; i++) { headerRow = headerRow + to_string(i) + " "; }
cout << headerRow + "\n"; // Display header row
cout << " ---------------\n";
for (int r=1; r<=maxArray; r++) { // this will cycle through rows 1,2,3,4,5
currentRow = "";
for (int c=1; c<=maxArray; c++) {// this will cycle through columns 1,2,3,4,5
if (r == c) {
rowLabel = to_string(r) + std::string(" | ");
}
currentVal = to_string(c * r) + " "; // multiply row * col and display it
currentRow = currentRow + currentVal;
}
cout << rowLabel + currentRow + "\n";
}
}