C编程:第三个上反对角方阵的总和,急需帮助
我正在上一门 C 编程短期课程,最近我一直忙于我的其他课程,并帮助我的麻烦准备他的婚礼(因为我是他的伴郎),所以我已经落后了,需要帮助。任何对这个简短作业的帮助将不胜感激,因为我根本不熟悉矩阵及其在几天内到期。
分配给第三个上反对角方阵的和。
我已得到以下信息:
矩阵应该是大小为 N 的方形整数矩阵。在此作业中,矩阵将被存储 在一维内存块中。您必须在概念性二维矩阵寻址和 使用指针算术进行一维块寻址。
关于随机数的注意事项: rand()
函数返回范围内的伪随机整数序列中的下一个整数 [0,RAND_MAX
]。 RAND_MAX
是一个非常大的数字,并且因系统而异。为了得到一个 [min, max]: srand(SEED)
范围内的整数
(int)((min)+rand()/(RAND_MAX+1.0) * ((max)-(min)+1))
用于设置 rand 的种子。如果使用相同的种子值调用 srand() ,则 重复伪随机数序列。每次获取不同的随机数 程序运行使用time(NULL)
作为种子。 Rand 在 stdlib.h
中,需要包含它。
该程序的结构应如下。
#define N 32 // Matrix size
#define MYSEED 1234 // Last 4 digits of your student number.
int *initialise( ) // Allocate memory for the matrix using malloc
// Initialise the matrix with random integers x, 1≤ x ≤ 9
// Use 'MYSEED' as the seed in the random generator.
// Return a pointer to the matrix
void print_matrix(int *) // Print matrix on screen
int compute_diagonal(int *) // Compute your required calculation using pointer arithmetic.
// (square bracket [ ] array indexes shouldn't be used)
// Return the solution.
void finalise(int *) //free the allocated memory.
int main() // The main function should print the solution to screen.
im doing a short course in c programming and i have been so busy lately with my other classes and and helping my bother prepare for his wedding (as im his best man)that I have fallen behind and need help. any help towards this short assignment would be much appreciated as im not familiar at all with matrixs and its due in a few days.
the assignment is to Sum of third upper anti-diagonal a squared matrix .
i have been given this information:
The matrix should be a square, integer matrix of size N. In this assignment the matrix will be stored
in a 1d block of memory. You will have to convert between the conceptual 2d matrix addressing and
1d block addressing with pointer arithmetic.
Note on random numbers:rand()
function returns the next integer a sequence of pseudo-random integers in the range
[0, RAND_MAX
]. RAND_MAX
is a very large number and varies from system to system. To get an
integer in the range [min, max]:
(int)((min)+rand()/(RAND_MAX+1.0) * ((max)-(min)+1))
srand(SEED)
is used to set the seed for rand. If srand()
is called with the same seed value, the
sequence of pseudo-random numbers is repeated. To get different random numbers each time a
programme runs use time(NULL)
as the seed. Rand is in stdlib.h
, which needs to be included.
The program should be structured as follows.
#define N 32 // Matrix size
#define MYSEED 1234 // Last 4 digits of your student number.
int *initialise( ) // Allocate memory for the matrix using malloc
// Initialise the matrix with random integers x, 1≤ x ≤ 9
// Use 'MYSEED' as the seed in the random generator.
// Return a pointer to the matrix
void print_matrix(int *) // Print matrix on screen
int compute_diagonal(int *) // Compute your required calculation using pointer arithmetic.
// (square bracket [ ] array indexes shouldn't be used)
// Return the solution.
void finalise(int *) //free the allocated memory.
int main() // The main function should print the solution to screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无需为您做家庭作业,这里有一个提示:
创建一个从矩阵中抽象存储和检索值的函数。其中一个签名应该看起来很像这样:
Without doing your homework assignment for you, here's a tip:
Make a functions that abstract storing and retrieving values out of the matrix. One of the signatures should look a lot like this:
好吧,因为这是作业,你还有几天时间,我不会在这里给你确切的答案。但我会给你一些想法,通过这些想法你应该很容易得到答案。
matrix[x][y]
,而只能使用一维数组。因此,请花一点时间考虑一下如何在一维数组中计算索引 (x,y)。请记住,C 按行存储元素(即元素matrix[0][0]
、matrix[0][1]
、matrix[0][ 2]
参考matrix[0]
、matrix[1]
、matrix[2]
)。这是一个关于 X、Y 和 N 的简单公式编辑:由于您不允许使用括号运算符,请记住
matrix[5]
与*(matrix+5)
相同。我认为告诉你这一点是公平的;)
Ok since this is homework and you still have a few days I will not give you an exact answer here. But I will give you some thoughts with which it should be pretty easy to come to your answer.
matrix[x][y]
here, but only a one-dimensional array. So just take a minute and think of how the index (x,y) can be computed within a 1D array. Keep in mind that C stores elements rowwise (i.e. the elementsmatrix[0][0]
,matrix[0][1]
,matrix[0][2]
refer tomatrix[0]
,matrix[1]
,matrix[2]
). It is a simply forumla in terms of X, Y and NEdit: Since you are not allowed to use the bracket operator, keep in mind that
matrix[5]
is the same as*(matrix+5)
.I think it's fair to tell you this ;)