C编程:第三个上反对角方阵的总和,急需帮助

发布于 2024-10-30 13:38:52 字数 1506 浏览 2 评论 0原文

我正在上一门 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

比忠 2024-11-06 13:38:52

无需为您做家庭作业,这里有一个提示:

创建一个从矩阵中抽象存储和检索值的函数。其中一个签名应该看起来很像这样:

int retrieve(int* matrix, int row, int col);

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:

int retrieve(int* matrix, int row, int col);
梦里南柯 2024-11-06 13:38:52

好吧,因为这是作业,你还有几天时间,我不会在这里给你确切的答案。但我会给你一些想法,通过这些想法你应该很容易得到答案。

  1. 一维索引矩阵:这里不允许使用matrix[x][y],而只能使用一维数组。因此,请花一点时间考虑一下如何在一维数组中计算索引 (x,y)。请记住,C 按行存储元素(即元素 matrix[0][0]matrix[0][1]matrix[0][ 2]参考matrix[0]matrix[1]matrix[2])。这是一个关于 X、Y 和 N 的简单公式
  2. 随机填充矩阵很容易,创建随机 int 的函数已经给出,只需沿着矩阵遍历并填充每个元素
  3. 添加第三个反上对角线即可。这确实是一个编程问题。只需在一张纸上画一个小矩阵,然后看看需要添加哪些元素。查看它们的索引,然后将您新获得的知识与我的第 1 点的结果结合起来,您就会知道要添加什么

编辑:由于您不允许使用括号运算符,请记住 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.

  1. Indexing a matrix 1D-way: You are not allowed to use 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 elements matrix[0][0], matrix[0][1], matrix[0][2] refer to matrix[0], matrix[1], matrix[2]). It is a simply forumla in terms of X, Y and N
  2. Filling the matrix randomly is easy, the function to create a random int is already given, just walk the matrix along and fill every element
  3. Adding the third anti-upper diagonal. This isn really a programming question. Just sketch a small matrix on a piece of paper and see what elements you have to add. Look at their indices and than combine your newly gained knowledge with your result from my point 1 and you will know what to add up

Edit: 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 ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文