我正在上一门 C 编程短期类(class),最近我一直忙于其他类(class),并帮助我的麻烦准备他的婚礼(因为我是他的伴郎),所以我已经落后了,需要帮助。任何对这个简短作业的帮助将不胜感激,因为我根本不熟悉矩阵及其在几天内到期。
分配给方阵第三个上反对角线的和。
我已获得以下信息:
矩阵应该是大小为 N 的方整数矩阵。在此赋值中,矩阵将被存储 在一维内存块中。您必须在概念性二维矩阵寻址和 使用指针算术进行一维 block 寻址。
关于随机数的注意事项:
rand()
函数返回范围内的伪随机整数序列中的下一个整数
[0,RAND_MAX
]。 RAND_MAX
是一个非常大的数字,并且因系统而异。为了得到一个
[min, max] 范围内的整数:
(int)((min)+rand()/(RAND_MAX+1.0) * ((max)-(min)+1))
srand(SEED)
用于设置 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.
最佳答案
无需为您做家庭作业,这里有一个提示:
创建一个从矩阵中抽象存储和检索值的函数。其中一个签名应该看起来很像这样:
int retrieve(int* matrix, int row, int col);
关于C 编程 : Sum of third upper anti-diagonal a squared matrix , 急需帮助,请,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5568421/