- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我刚开始学习 C。今天我遇到一个问题,我必须输入 2 个矩阵(行数和列数由用户指定)并添加它们。我很容易地完成了添加和其他部分。但我正在考虑让它看起来更好。所以我的想法是:假设用户输入一个 3x3 大小的矩阵。假设他选择了以下元素->
Matrix 1->
1 2 3
4 5 6
7 8 9
Matrix 2->
9 8 7
6 5 4
3 2 1
我希望它们显示为->
| 1 2 3 + 9 8 7 |
| 4 5 6 + 6 5 4 |
| 7 8 9 + 3 2 1 |
(不,不是实际的加法,只是这种格式,然后在下一行中给出加法的答案)。但问题是,我无法显示右手边的一半。
我的输出如下:
| 1 2 3 + 9 8 7 |
| 4 5 6 + |
| 7 8 9 + |
我已经尝试了很多让剩余的字符以正确的顺序显示,但遇到了一些或其他类似的问题。到目前为止,这是我的代码(它不是最新的,我已经尝试将它弄得更糟,但最新的代码通过引入许多我认为甚至不需要的变量而进一步弄乱了它。所以我会发布我的迄今为止最好的进展)。
printf("| ");
i = 0; //A global int loop variable defined somewhere
width2 = width; //Another width variable in case I need it in second array, width is variable for number of elements in a row of array. In above example, width=3
for (ii = 0; ii < width * height; ii++) { //ii is just like i, another global int loop variable. Height is number of characters in a row (in above example 3)
if (ii != 0) {
if (ii % width == 0) {
printf(" |\n| ");
}
}
printf("%d ", row1[ii]); //For printing out first (left) set of numbers. row1 is where my matrix 1 array values are stored.
if (((ii + 1)*(width - 1)) % (width * 2) == 0) { //I think this condition is where things are going wrong.
printf(" + ");
for (i; i < width2; i++) {
if (i != 0) {
if (i % width2 == 0) {
printf(" |\n| ");
}
}
printf("%d ", row2[i]); //row2 has second matrix (right) array values
}
}
sec++; //Just another integer variable to have some control over loop process, didnt succeed much though
}
printf(" |\n\n");
从 2 天开始就一直在尝试这个,这真的让我很头疼。我不介意是否有更好的更小的代码并且需要替换整个代码(因为我是 C 的新手)。
最佳答案
保持简单,行有一个外循环,列有两个内循环。矩阵的大小是否不同并不重要,只需在每个内部循环之前添加一个简单的检查即可。
类似下面的伪代码:
size_t max_line_count = MAX(matrix1_line_count, matrix2_line_count);
for (size_t line = 0; line < max_line_count; ++line)
{
size_t column;
printf(" | ");
// First matrix
for (column = 0; column < matrix1_column_count; ++column)
{
if (line < matrix1_line_count)
printf("%2d", matrix1[line][column]);
else
printf(" ");
}
printf(" + ");
// The second matrix
for (column = 0; column < matrix2_column_count; ++column)
{
if (line < matrix2_line_count)
printf("%2d", matrix2[line][column]);
else
printf(" ");
}
printf(" |\n");
}
内部循环可以分解成一个单独的函数,以避免代码重复。
如果矩阵不是数组的数组,而是一个大的单个数组,则使用例如 matrix1[line * matrix1_column_count + column]
。
关于c - 一次并排打印两个具有特定字符数的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29694411/
我在 Excel 中有两个图表,但我想将这些图表合并为一个图表。如您所见,我缩小了 graph1 中图表的大小。我想移动 graph2 中的图表进入graph1中的空白区域 最佳答案 我认为在基本的
我正在为我的站点构建一个 HTML 模板,并希望在左侧有一个主要内容 Pane ,在右侧有一个导航 Pane (类似于 Twitter)。 我假设 DIV 不是首选方法,因为默认情况下它们是从上到下列
我有以下 fiddle :http://jsfiddle.net/BFSH4/ 如您所见,有两个问题: h1 和 h2 没有垂直对齐。 导航和内容未水平对齐。 对于 1. 我已经尝试了 margin
在下面的示例中,我试图让“左”和“右”div 并排显示。显然我的理解是有缺陷的,但我犯了什么错误,因为(至少在 Chrome 中)它们没有并排出现。 谢谢
我在 chrome 上使用 Tampermonkey 向网页添加按钮。单击时,脚本会分析页面,然后显示警报。这是显示按钮的代码: function Main(){ GM_addStyle(
我的自定义键盘中有 UICollectionView,它有两行高度相同但宽度不同(大小来自服务器)的单元格,滚动方向是水平的。 我怎样才能并排显示集合项,而不用这种基于先前单元格宽度的奇怪居中? 最佳
任何人都可以帮助将两个位图图像组合成一个位图 在安卓中(并排)。 谢谢,尤瓦拉杰 最佳答案 您可以使用 Canvas - 查看这篇文章: http://www.jondev.net/articles/
如果有的话,设计 RPM 的“正确”方法是什么,以便可以通过 YUM/RPM 并排安装多个版本而不会相互干扰?对于库,正确的答案似乎与 sonames 有关,尽管我找不到任何关于 sonames、符号
从我的 last question 跟进:我执行了 Steve 提供的所有步骤,SxS Parse 日志为空,但我仍然收到“Class Not Registered”错误。 我知道我的 .exe 正在
我想并排显示两个图 block 层,就像并排的传单插件 ( https://github.com/digidem/leaflet-side-by-side )。 但是,我不知道如何用 react 来做
我正在制作一个将 PO 导出为 PDF 的采购订单系统,但我需要在上半部分显示来自买方和卖方的数据。我想并排放置 2 个 DetailView,每个都有 50% 的页面宽度。有可能的?到目前为止,我还
无论屏幕大小如何,我都试图并排 float 两个 div。目前在 IE 7 中,如果我调整窗口大小,一个 div 会下降到另一个下方。我认为这是因为 div2 包含一个表格,一旦窗口边缘碰到表格,它就
我想使用这个数据框的 geom_bar() 创建一个并排的条形图, > dfp1 value percent1 percent 1 (18,29] 0.20909091 0.454545
我正在尝试在 Xcode (Swift) 中创建一个单位转换器,并一直在尝试在 Storyboard 中对 UI 进行排序。我一直试图在屏幕的上半部分并排放置两个表格 View ,以保存两个测量类型的
下面的代码运行良好。 eventDrag 和 Drop 我可以放置在表格内的任何位置。 但是我只想将事件拖放到事件的左侧和右侧。 我想从其他事件的顶部和底部停止拖动事件。只有左侧和右侧的其他事件我想拖
我似乎无法让我的图层列表正常工作。 我需要
我想要类似 unix 中的 paste 命令,它需要两个文件并打印第一行,第一个文件,然后是分隔符,然后是第二个文件的第一行,然后是换行符,然后是第一个文件的第二行第二个文件的文件分隔符第二行,等等。
我想将我的内部应用程序的测试版本部署到我的测试组,我需要它与当前的 LIVE 版本一起安装。 我更改了发布选项中的所有内容,但它仍然会覆盖我的实时安装。我还需要做什么?我是否必须更改“应用程序”选项卡
我想创建一个设置,其中左侧有一个图像,右侧有一个 div。我正在使用 float left 来完成此操作,但我希望右侧的 div 与左侧的图像具有相同的高度,并且文本垂直居中。 这是一个jsfiddl
如何让两张 table 并排放置。问题可能会出现,有时一张 table 可能比另一张 table 大它旁边的表格,所以这可能会导致问题并且下面的表格可能不对齐,我想解决。在我制作的演示中,我有四个表,
我是一名优秀的程序员,十分优秀!