(Solved) : Insert Comments Statements Attached Program Pertain Arrays However Statements Program Also Q32520827 . . .
Insert comments for those statements in the attached programthat pertain to arrays. There are, however, statements in theprogram that, also, pertain to pointers. If you are able to commenton those statements, please do. If not, insert the comment: Commenton pointer omitted. All pre-existing comments, except for a few,have been removed.
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <assert.h>
long offset (int *dimInfo, …) {
long result;
int *indices,
i;
va_list ap;
indices = (int *) malloc (dimInfo[0] * sizeof(short));
va_start (ap,dimInfo);
for (i=0; i < dimInfo[0]; i++) {
indices[i] = va_arg(ap,int);
assert (indices[i] < dimInfo[i+1]);
}
va_end (ap);
result = (indices[0] * dimInfo[2] + indices[1]);
for (i=2; i < dimInfo[0]; i++)
result = result * dimInfo[i+1] +indices[i];
free (indices);
return result;
}
#definesubA2(r,c) *(&A2[0][0] + offset(A2i,r,c))
#define subA3(r,c,l) *(&A3[0][0][0] + offset(A3i,r,c,l))
#define subA4(r,c,l,m) *(&A4[0][0][0][0] + offset(A4i,r,c,l,m))
#define subA5(r,c,l,m,n) *(&A5[0][0][0][0][0] +offset(A5i,r,c,l,m,n))
#define subDynArray(r,c,l) *(dynArray +offset(dynArrayInfo,r,c,l))
int main(void) { // TESTING PROGRAM
int A2[2][3]; intA2i[] = { 2, 2,3 };
intA3[2][3][4]; int A3i[] = { 3, 2,3,4 };
floatA4[2][3][4][5]; int A4i[] = { 4, 2,3,4,5 };
charA5[2][3][4][5][6]; int A5i[] = { 5, 2,3,4,5,6 };
float *dynArray;
int *dynArrayInfo;
int rows,cols,levels,
row,col,level;
float value;
int i,j,k,l;
subA2(1,2) = 5;
printf (“%d %dn”,A2[1][2],subA2(1,2));
/*
// This block can demonstrate that elements in a 3-dimensionalarray in C
// are stored breadth-first (level, then column,then row). It prints
// quite a lot, that’s why it’s commented out fornow.
for (i=0; i < 2*3*4; i++)
*(&A3[0][0][0]+i) = i;
for (i=0; i < 2; i++)
for (j=0; j < 3; j++)
for (k=0; k < 4; k++)
printf(“A3[%d][%d][%d]=%dn”,i,j,k,A3[i][j][k]);
puts (“nn”);
return 0;
*/
/*
// And this block of code demonstrates that elements in a4-dimensional array
// in C are stored still breadth-first (file, thenlevel, then column, then
// row). It also prints quite a lot, that’s why it’scommented out for now.
for (i=0; i < 2*3*4*5; i++)
*(&A4[0][0][0][0]+i) = i;
for (i=0; i < 2; i++)
for (j=0; j < 3; j++)
for (k=0; k < 4; k++)
for (l=0; l < 5;l++)
printf(“A4[%d][%d][%d][%d]=%dn”,i,j,k,l,A4[i][j][k][l]);
*/
puts (“nExamples showing elements assigned using the offset()function”);
subA3(1,1,1) = 99;
printf (“A3[1][1][1]=%dn”,A3[1][1][1]);
subA4(0,1,2,2) = 27.1234F;
printf (“A4[0][1][2][2]= %5.5f%5.5fn”,A4[0][1][2][2],subA4(0,1,2,2));
subA5(1,2,3,4,2) = ‘X’;
printf (“A5[1][2][3][4][2]=’%c’n”,A5[1][2][3][4][2]);
puts (“nExample of dynamically defining a multi-dimensionalarray”);
printf (“Enter number of rows: “); scanf (“%d”,&rows);
printf (“Enter number of cols: “); scanf (“%d”,&cols);
printf (“Enter number of levels: “); scanf(“%d”,&levels);
dynArray = (float *) malloc (rows * cols * levels *sizeof(float));
dynArrayInfo = (int *) malloc ((1+3) * sizeof(int));
dynArrayInfo[0] = 3;
dynArrayInfo[1] = rows;
dynArrayInfo[2] = cols;
dynArrayInfo[3] = levels;
while (true) {
printf (“Specify row: “); scanf (“%d”,&row);
printf (“Specify column: “); scanf(“%d”,&col);
printf (“Specify level: “); scanf(“%d”,&level);
if (row == -1 && col == -1 &&level == -1) break;
printf (“Specify new value: “); scanf(“%f”,&value);
subDynArray(row,col,level) = value;
printf (“The value found in the array was%4.4fn”,
subDynArray(row,col,level));
};
return 0;
}
Expert Answer
Answer to Insert Comments Statements Attached Program Pertain Arrays However Statements Program Also Q32520827 . . .
OR