需要家庭作业帮助 [英] Homework task help needed
本文介绍了需要家庭作业帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
编写一个程序,创建一个二维数组A [7] [7],其值为
Write a program that creates an two dimensional array A[7][7] with values
1 2 3 4 5 6 7
0 2 3 4 5 6 7
0 0 3 4 5 6 7
0 0 0 4 5 6 7
0 0 0 0 5 6 7
0 0 0 0 0 6 7
0 0 0 0 0 0 7
我需要投入什么条件?循环得到这个表作为输出??????
我尝试过:
What condition do i need to put in for loop to get this table as output??????
What I have tried:
#include <iostream>
using namespace std;
int main(){
int nabeel[7][7];
for(int row=0; row<7; row++){
for (int column=0; column<7; column++){
nabeel[row][column]=row - column;
cout << nabeel[row] [column] << " ";
}
cout << endl;
}
}
output: 0 -1 -2 -3 -4 -5 -6
1 0 -1 -2 -3 -4 -5
2 1 0 -1 -2 -3 -4
3 2 1 0 -1 -2 -3
4 3 2 1 0 -1 -2
5 4 3 2 1 0 -1
6 5 4 3 2 1 0
推荐答案
答案比你的程序复杂一点。
解决这个问题的方法很多,包括不需要数组的方法。使用数组是人为的。
步骤我建议:
- 拆分设置数组并显示数组,以便于阅读。
#include< iostream>
使用命名空间std;
The answer is a little more complicated than your program.
The is many ways to solve this problem, including ways that don't need an array. Using the array is artificial.
Steps I would recommend:
- Split setting the array and display the array, to ease the reading.
#include <iostream>
using namespace std;
int main(){
int nabeel[7][7];
// set the array
for(int row=0; row<7; row++){
for (int column=0; column<7; column++){
nabeel[row][column]=row - column;
}
}
// display the array
for(int row=0; row<7; row++){
for (int column=0; column<7; column++){
cout << nabeel[row] [column] << " ";
}
cout << endl;
}
}
- 然后更改程序以设置此矩阵
- Then change the program to set this matrix
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
- 然后考虑程序和问题之间的区别。
有时将值替换为零。这意味着一个条件,如果你在设置值时使用 if
,则表示条件。
这就是全部今天,你需要填补漏洞。
- Then think about the difference between the program and the problem.
Sometimes the value is replaced with a zero. This means a condition, if mean ti use a if
when you set the value.
That is all for today, you need to fill the holes.
显然,行 r $ c的任何项目都有两个个不同的条件$ c>和列 c
:
- 如果行号
r
小于(或等于)列号 c
,则项值为(r + 1)
。 - 另一方面,如果
r> c
然后项目值 0
。
Apparently, there are two different conditions for any item at rowr
and columnc
:
- if the row number
r
is less than (or equal to) the column numberc
, then item value is(r+1)
.- On the other hand, if
r > c
then the item value is0
.
a[r][c] = r > c ? 0 : (r+1);
有很多方法可以做到这一点,其中一个是:
1.创建一个7x7阵列,每行初始化为1 2 3 4 5 6 7
2.循环每行,每行(由数组索引n)标识,用0替换该行的前n列。(n从0开始)
这就是全部。
There're a number ways to achieve this, one of them is:
1. Create a 7x7 array initialized with 1 2 3 4 5 6 7 in each row
2. loop thru each row, at each row (identified by array index n), replace the first n columns of that row with 0. (n starts from 0)
That's all.
这篇关于需要家庭作业帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文