[英]C Programming : Sum of third upper anti-diagonal a squared matrix , help badly needed please
我在學習C編程方面的短期課程,最近我一直忙於其他課程,並幫助我為他的婚禮做准備(作為我的伴郎),以至於我落伍了,需要幫助。 由於對矩陣及其在幾天之內完全不熟悉,因此對這種短期任務的任何幫助將不勝感激。
分配給第三上反對角線平方矩陣的和。
我已收到此信息:
矩陣應為大小為N的正方形整數矩陣。在此分配中,矩陣將存儲在1d的內存塊中。 您將必須使用指針算法在概念性的2d矩陣尋址和1d塊尋址之間進行轉換。
關於隨機數的注意事項: rand()
函數返回下一個整數和[0, RAND_MAX
]范圍內的偽隨機整數序列。 RAND_MAX
是一個非常大的數字,並且因系統而異。 要獲得[min,max]范圍內的整數:
(int)((min)+rand()/(RAND_MAX+1.0) * ((max)-(min)+1))
srand(SEED)
用於設置rand的種子。 如果以相同的種子值調用srand()
,則將重復偽隨機數序列。 要在每次程序運行time(NULL)
獲得不同的隨機數,請使用time(NULL)
作為種子。 蘭德在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);
好的,因為這是家庭作業,您還有幾天,在這里我不會給您確切的答案。 但是,我會給您一些想法,您可以很容易地得出答案。
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)
。 我認為這很公平;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.