編輯以添加整個作業和預期輸出。
您受雇協助消防員在大地理區域內定位野火。該區域被劃分為較小的區域。通過衛星掃描每個區域的平均溫度。如果一個區域的平均溫度嚴格高于 1000°F,我們假設該區域發生火災。如果溫度在100度(含)到1000度(含)之間,就得進一步排查,所以就變成了“觀察區”。
您正在觀看的大地理區域是一個具有一定長度和寬度的矩形,每個都以區域的形式給出。例如,如果要掃描的區域的長度為 6,寬度為 9,那么它將被劃分為 6*9 個區域:
因為您的程式將用于各種地理區域(每個區域都有自己的長度和寬度),所以您的程式需要為要處理的區域數量(垂直和水平)動態分配記憶體。
為此,您必須使用以下兩個函式而不更改其中的代碼:
int ** allocateIntStarArray(int num){
int ** ptr = (int **) malloc(num * sizeof(int *));
return ptr;
}
int * allocateIntArray(int num){
int * ptr = (int *) malloc(num * sizeof(int));
return ptr;
}
The function `allocateIntArray()` will be used to allocate the space required to store the average temperatures in one row of zones, that is, an array of integers. The function therefore returns a pointer to such an array of integers.
The function `allocateIntStarArray()` will be used to allocate an array of pointers, each of which will store a pointer to a row of integers (temperatures of zones). That is, the function returns a pointer to an array of pointers. Each cell of this array will point to an array of integers containing the temperature values for the zones.
The inputs of the program are first the length, then the width of an area, then the average temperatures of all zones, row by row.
Please remember to free the memory you have allocated.
The output should pinpoint the possible zones with fires with [X] and the watch zone with a [*], the other zone are displayed with [ ].
Input:
6
9
70 71 70 72 70 69
71 73 68 71 73 72
70 71 70 76 1900 78
69 71 100 800 75 71
70 70 71 79 70 69
70 71 112 1005 75 72
70 71 70 900 70 70
72 70 70 72 70 69
73 74 73 72 70 70
Output:
[ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][X][ ]
[ ][ ][*][*][ ][ ]
[ ][ ][ ][ ][ ][ ]
[ ][ ][*][X][ ][ ]
[ ][ ][ ][*][ ][ ]
[ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ]
----
The code I'm working on is to create a matrix based off user input. I'm having an issue getting my variable **mat back to the first array element so that it will print the rectangle correctly. Could someone enlighten me on how to do this? What I have so far:
#include <stdio.h>
#include <stdlib.h>
int **allocateIntStarArray(int);
int *allocateIntArray(int);
int **allocateMatrix(int, int);
void readValues(int **, int, int);
void print(int **, int, int);
int main(void) {
int rows, cols;
scanf("%d %d", &rows, &cols);
int **mat = allocateMatrix(rows, cols);
readValues(mat, cols, rows);
print(mat, cols, rows);
/* free your memory */
for (int r = 0; r < rows; r ){
free(mat[r]);
}
free(mat);
return 0;
}
void readValues(int **mat, int ncols, int nrows) {
for (int r = 0; r < nrows; r ) {
for (int c = 0; c < ncols; c ) {
scanf("%d", &mat[r][c]);
}
}
}
void print(int **mat, int ncols, int nrows) {
for (int r = 0; r < nrows; r ) {
for (int c = 0; c < ncols; c ) {
int value = mat[r][c];
if (value > 1000){
printf("[X]");
}
else if (value >= 100){
printf("[*]");
}
else{
printf("[ ]");
}
}
printf("\n");
}
}
int **allocateMatrix(int nrows, int ncols) {
int **mat = allocateIntStarArray(nrows);
for (int row = 0; row <= nrows; row) {
mat[row] = allocateIntArray(ncols);
}
return mat;
}
/* Provided functions, do not edit */
int **allocateIntStarArray(int num) {
int **ptr = (int **) malloc(num * sizeof(int *));
return ptr;
}
int *allocateIntArray(int num) {
int *ptr = (int *) malloc(num * sizeof(int));
return ptr;
}
uj5u.com熱心網友回復:
我不會使用雙指標而不是二維陣列。
void *allocateMatrix(size_t nrows, size_t ncols, int (**array)[ncols])
{
int (*arrptr)[ncols] = malloc(nrows * sizeof(*arrptr));
if(array) *array = arrptr;
return arrptr;
}
int main(void)
{
size_t cols = 30, rows = 40;
int (*matrix)[cols] = allocateMatrix(rows, cols, NULL);
if(matrix)
{
matrix[5][4] = 97;
matrix[4][2] = matrix[5][4] * 4;
printf("%d %d\n", matrix[5][4], matrix[4][2]);
}
free(matrix);
}
更簡單,只有一次分配/免費,用法與陣列相同,并且在洗掉一級間接時更有效。
還要對大小和索引使用正確的型別: size_t
uj5u.com熱心網友回復:
在scanf("%d", &**mat);中,第一個*將由其&對應物平衡,因為這兩個運算子具有相反的效果。
結果將是*mat一個型別的值int *。雖然 is 是%d格式說明符的正確型別,但*mat它是矩陣中第一個子陣列的地址。重復讀取該地址的值將繼續覆寫第一個子陣列 ( mat[0][0]) 的第一個值。
由于您從不移動任何指標,因此矩陣的其余部分將保持未初始化狀態。
也就是說,無需手動管理指標,因為您已經在計算索引。只需對索引使用陣列下標表示法,并獲取每個值位置的地址。
scanf("%d", &mat[rows][cols]);
(見會員訪問運營商)
在 print 函式中,mat - num將決議到您不應該嘗試訪問的某個地址,因為它超出了mat物件的范圍。這是未定義行為的經典示例。
然后,您嘗試設定m為通過該地址定位的某個值,但只能設定一次。
同樣,您已經在計算您的索引,因此請利用它們來發揮您的優勢。將內部回圈中的值設定為在 處找到的值m,以便您在每次迭代時更新其值。mat[rows][cols]
這是一個功能程式,帶有釋放您分配的記憶體的示例。
(注意:如前所述,size_t是用于記憶體大小和索引的正確型別。我堅持在int這里,以匹配您的講師的有缺陷的實作。)
#include <stdio.h>
#include <stdlib.h>
int **allocateIntStarArray(int);
int *allocateIntArray(int);
int **allocateMatrix(int, int);
void readValues(int **, int, int);
void print(int **, int, int);
int main(void) {
int rows, cols;
if (2 != scanf("%d %d", &cols, &rows)) {
fprintf(stderr, "Could not read matrix size information.\n");
return EXIT_FAILURE;
}
int **mat = allocateMatrix(rows, cols);
readValues(mat, cols, rows);
print(mat, cols, rows);
/* free your memory */
for (int i = 0; i < rows; i )
free(mat[i]);
free(mat);
}
void readValues(int **mat, int ncols, int nrows) {
for (int i = 0; i < nrows; i ) {
for (int j = 0; j < ncols; j ) {
if (1 != scanf("%d", &mat[i][j])) {
fprintf(stderr, "Invalid value read.\n");
exit(EXIT_FAILURE);
}
}
}
}
void print(int **mat, int ncols, int nrows) {
for (int i = 0; i < nrows; i ) {
for (int j = 0; j < ncols; j ) {
int value = mat[i][j];
if (value > 1000)
printf("[X]");
else if (value >= 100)
printf("[*]");
else
printf("[ ]");
}
/* print a newline after every row */
putchar('\n');
}
}
int **allocateMatrix(int nrows, int ncols) {
int **mat = allocateIntStarArray(nrows);
for (int row = 0; row < nrows; row) {
mat[row] = allocateIntArray(ncols);
}
return mat;
}
/* Provided functions, do not edit */
int **allocateIntStarArray(int num) {
int **ptr = (int **) malloc(num * sizeof(int *));
return ptr;
}
int *allocateIntArray(int num) {
int *ptr = (int *) malloc(num * sizeof(int));
return ptr;
}
這是我用來快速生成測驗資料的程式:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
srand((unsigned) time(NULL));
int rows = (rand() % 20) 1;
int cols = (rand() % 20) 1;
printf("%d %d\n", rows, cols);
for (int i = 0, n = rows * cols; i < n; i )
printf("%d ", rand() % 2000);
}
更新:
正如我所懷疑的,您將行和列的索引弄混了。
矩陣的構建方式,第一個下標值 ( matrix[r]) 對應于row,第二個下標值 ( matrix[r][c]) 對應于該行中的列。
在 read_values
for(i=0;i<ncols;i ){
for(j=0;j<nrows;j ){
scanf("%d", &mat[i][j]);
}
}
并且在 print
for(i=0;i<ncols;i ){
for(j=0;j<nrows;j ){
int value = mat[i][j];
/* ... */
你已經翻轉了那些,訪問mat[COLUMN as i][ROW as j].
您將訪問邊界外的記憶體,wheni >= nrows或j >= ncols。
翻轉嵌套回圈可能也是一個好主意,以便它們讀取"for each row, for each column",這更好地匹配下標語法:
for (int r = 0; r < nrows; r )
for (int c = 0; c < ncols; c )
scanf("%d", &mat[r][c]);
此外,在 中main,您必須先釋放free(mat);所有子陣列
for(int i=0;i<rows;i ){
free(mat[i]);
}
free(mat); /* move this outside, after the loop */
否則mat將是第二次迭代的懸空指標mat[i],并將呼叫Undefined Behavior。
最后一點,為了匹配作業要求,您的中間溫度范圍應該是value >= 100,因為范圍包括在內 ( [100, 1000])。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/415840.html
標籤:
下一篇:使用動態分配的指標初始化成員指標
