我嘗試使用外部函式(來自另一個 c 檔案)讀取和列印矩陣,問題是我想讀取函式中的矩陣維度并將它們存盤在主函式中,我該怎么做?
我是否需要回傳一個包含矩陣的 m 和 n 維的陣列,或者我可以訪問我在 main 中創建的變數并在外部函式中更改它們的值?(如果有人會解釋第二個,我更喜歡)我實際上不知道如何使用指標和東西。對不起我的英語,我不是母語人士,也感謝您的回復
第二個和第三個函式在一個外部function.c檔案中
int main(){
//----------variables:
int num_of_rows, num_of_columns;
int matrix[10][10];
//-------------------->END
//---------->Read a matrix from user
read_matrix(num_of_rows, num_of_columns, matrix);
//-------------------->END
//---------->Print a matrix
print_matrix(num_of_rows, num_of_columns, matrix);
//-------------------->END
//----------START ==> exit_program:
printf("\n Press any key to exit the program: ");
_getch();
return 0;
//-------------------->END
}//END
//START ==> read a matrix from user
void read_matrix(int num_of_rows, int num_of_columns, int matrix[10][10]){
//---------->variables
int i,j;
//-------------------->END
//---------->reading the matrix dimensions
printf("\nPlease specify the number of rows:");
scanf("%d", &num_of_rows);
printf("\nPlease specify the number of columns: ");
scanf("%d", &num_of_columns);
//-------------------->END
//---------->reading the matrix elements
printf("\nPlease introduce the matrix elements below:\n");
for(i=0; i<num_of_rows; i ){
for(j=0; j<num_of_columns; j ){
printf("matrix[%d][%d]= ", i, j);
scanf("%d", &matrix[i][j]);
}
}
//-------------------->END
}//END
//START ==> Print a matrix
void print_matrix(int num_of_rows, int num_of_columns, int matrix[10][10]){
//---------->variables
int i,j;
//-------------------->END
//---------->
for(i=0; i<num_of_rows; i ){
for(j=0; j<num_of_columns; j ){
printf("matrix[%d][%d]= %d", i, j, matrix[i][j]);
}
}
//-------------------->END
}//END
uj5u.com熱心網友回復:
引數在 C 中按值傳遞。
在read_matrix中,num_of_rows引數是一個區域變數。即使您修改它,呼叫者也不會看到任何變化。對num_of_columns.
你要這個:
void read_matrix(int *num_of_rows, int *num_of_columns, int matrix[10][10]) {
// ^ add * ^ add *
...
scanf("%d", num_of_rows); // << remove the &
printf("\nPlease specify the number of columns: ");
scanf("%d", num_of_columns); // << remove the &
...
for (i = 0; i < *num_of_rows; i ) {
// ^add *
for (j = 0; j < *num_of_columns; j ) {
// ^add *
并在main:
read_matrix(&num_of_rows, &num_of_columns, matrix);
// ^ ^ add the &s
這是您的 C 學習材料中涵蓋的基本知識。最有可能在處理指標的那一章和處理函式呼叫的那一章中。
uj5u.com熱心網友回復:
這就是問題所在。我測驗了你的代碼,它運行良好。問題是,您想要實作的只有使用動態記憶體分配才能實作。看看malloc和free函式。
你可以閱讀更多關于它的資訊:
https://www.tutorialspoint.com/what-is-malloc-in-c-language
https://www.tutorialspoint.com/how-do-malloc-and-free-work-in-c-cplusplus
在 C 語言中,您負責為可變長度資料結構分配記憶體空間。指標只是將地址存盤到分配給所需資料型別的特定記憶體空間。
例如:
int n, *p;
p = (int*) malloc(n * sizeof(int));
在這個例子中,你只是給 p 一個地址給你剛剛分配的n 個整數中的第一個整數。
p在使用for 回圈時將像向量一樣作業,因為在幕后,這正是您遍歷固定長度向量和矩陣時發生的情況,但這次您在運行時控制了它的長度。
uj5u.com熱心網友回復:
#include<stdio.h>
void main()
{
int x[3][3], p, q, max;
printf("Enter the elements of matrix: \n");
for(p=0;p<3;p )
{
for(q=0;q<3;q )
scanf("%d", &x[p][q]);
}
max=x[0][0];
printf("The matrix is as follows: \n");
for(p=0;p<3;p )
{
for(q=0;q<=3;q )
scanf(" %d", &x[p][q]);
}
for(p=0;p<3;p )
{
for(q=0;q<3;q )
{
if(x[p][q]>max)
max=x[p][q];
}
printf("\n");
}
printf("Maximum number in the matrix is: %d", max);
}
輸出:
Enter the elements of matrix:
23 65 12
12 23 56
12 10 32
The matrix is as follows:
23 65 12
12 23 56
12 10 32
Maximum number in the matrix is: 65
矩陣程式
探索更多矩陣程式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/410353.html
標籤:
上一篇:回圈通過鏈表時出現分段錯誤
