Edited to add the entire assignment and expected output.
You have been hired to assist firefighters locate wildfires in a large geographic area. The area is divided into smaller zones. Each zone is scanned via satellite for its average temperature. If a zone has an average temperature strictly greater than 1000°F, we assume there is a fire in that zone. If the temperature is between 100 degrees (included) and 1000 degrees (included), we have to further investigate, so it becomes a "zone to watch."
The large geographic area you are watching is a rectangle with a certain length and width, each given in terms of zones. For example, if the area to be scanned has a length of 6 and width of 9 then it will be divided into 6*9 zones:
Because your program will be used for a variety of geographic areas (each with its own length and width) your program needs to dynamically allocate the memory for the number of zones it is to handle (vertically and horizontally).
To do so, you must use the two following functions without changing the code in them:
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;
}