我将如何使用 if 语句输出此乘法表
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;
}
此代码的要点是使用一维数组来创建乘法表。我相信我已经正确编码了所有内容,但此作业的另一部分是使用 if 语句输出表,我不完全确定如何。有人可以给我一些指导吗?
我不知道
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
}
}
不需要 如果 声明...希望有帮助。抱歉,如果代码不完整,我不太用 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";
}
}
相关文章:
- 为什么我的 if else 语句不起作用并从数组中输出正确的索引?
- if/else 语句输出由于加号或减号而未显示正确的消息
- 有没有办法我可以在 C++ 中使用 if 语句来说明如果一个字符串不是两个名称的 eqal,它会给出一个输出,ATM i
- 为什么当 while 循环中的 if 条件变为 false 时,我的函数不输出最后一条语句?
- C++:更改 IF 语句中的输出文件
- c++:if 语句无法正常运行,并继续执行无效输出
- C++如果使用 lambda 表达式的语句返回 true,但输出来自 false,为什么
- 输出错误命令的条件语句
- 为什么我得到错误的输出 - If & Else 语句
- C# 读取完全锁定的文本文件或读取 if 语句外部的输出
- 如果/else语句未显示/可变分配问题的输出;初学者程序员
- 从类模块检索信息以在 switch 语句中输出时出现问题
- 切换语句在 C++ 中的输出不清楚
- Cpp 切换语句不会输出条件集
- Switch语句不显示除默认语句之外的任何类型的输出
- 删除"超出矢量误差范围"的打印语句后输出不同的根本原因
- cout 输出乱码消息行,而不是实际用户的 cout 语句
- 最后一行欧普特两个组合输出问题?如果其他语句
- 添加 cout 语句会更正输出
- c++中while循环执行中的打印输出语句