#Guys help me to fix code ๐Ÿ™

16 messages ยท Page 1 of 1 (latest)

storm lagoon
#

Well, Given a matrix of integers and two natural numbers M and N.
Consider
a subset of the columns of the matrix in which there is at least one element, which, when divided by M, gives the remainder of N. Arrange the columns of the matrix in descending order within the given subset, assuming that the comparison of the columns corresponds to the comparison of their minimum elements. Columns of the matrix that are not included in this subset
must retain their location in the matrix.

I have my code it works . But here I create an array where I put the sorted columns and at the end all the others. AND I NEED to work only with the main a8 array AND SO THAT THE COLUMNS I DON'T NEED REMAIN IN PLACE!! That's exactly what I have a problem with, and sort the ones I need relative to each other. Please redo some of my code! I can't think at all anymore! Help

prime dawnBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

storm lagoon
#

int main() {
FILE *inputFile, *outputFile;
int rows, cols, M, N, i, j;
int **a8;
int *validColumns;
int validColumnCount;
int found;
int min1;
int k;
int min2;
int temp;
int *resC;
int resColCount;
int x;
int y;
int valid;
int originalColumnIndex;

inputFile = fopen("input.txt", "r");
if (inputFile == NULL) {
perror("Error opening input file");
return 1;
}

fscanf(inputFile, "%d %d %d %d", &rows, &cols, &M, &N);

a8 = (int **)malloc(rows * sizeof(int *) + rows * cols * sizeof(int));
if (!a8) {
perror("Memory allocation error");
fclose(inputFile);
return 1;
}
a8[0] = (int *)(a8 + rows);
for (i = 1; i < rows; i++) {
a8[i] = a8[i - 1] + cols;
}

for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
fscanf(inputFile, "%d", &a8[i][j]);
}
}

fclose(inputFile);

validColumns = (int *)malloc(cols * sizeof(int));

validColumnCount = 0;
for (j = 0; j < cols; j++) {
found = 0;
for (i = 0; i < rows; i++) {
if (a8[i][j] % M == N) {
found = 1;
break;
}
}

#

if (found) {
validColumns[validColumnCount++] = j;
}
}

for (i = 0; i < validColumnCount - 1; i++) {
for (j = 0; j < validColumnCount - i - 1; j++) {
min1 = a8[0][validColumns[j]];
for (k = 1; k < rows; k++) {
if (a8[k][validColumns[j]] < min1) {
min1 = a8[k][validColumns[j]];
}
}
min2 = a8[0][validColumns[j + 1]];
for (k = 1; k < rows; k++) {
if (a8[k][validColumns[j + 1]] < min2) {
min2 = a8[k][validColumns[j + 1]];
}
}
if (min1 < min2) {
temp = validColumns[j];
validColumns[j] = validColumns[j + 1];
validColumns[j + 1] = temp;
}
}
}

resC = (int *)malloc(cols * sizeof(int));

resColCount = 0;

for (x = 0; x < validColumnCount; x++) {
resC[resColCount++] = validColumns[x];
}

for (x = 0; x < cols; x++) {
valid = 0;
for (y = 0; y < validColumnCount; y++) {
if (x == validColumns[y]) {
valid = 1;
break;
}
}
if (!valid) {
resC[resColCount++] = x;
}
}

outputFile = fopen("output.txt", "w");

if (outputFile == NULL) {
perror("Error opening output file");
free(a8);
free(validColumns);
free(resC);
return 1;
}

for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
originalColumnIndex = resC[j];
fprintf(outputFile, "%d ", a8[i][originalColumnIndex]);
printf("%d ", a8[i][originalColumnIndex]);
}
fprintf(outputFile, "\n");
printf("\n");
}

fclose(outputFile);
free(a8);
free(validColumns);
free(resC);

return 0;
}

supple hawk
#

Please properly format your code

prime dawnBOT
#
int main() {
  FILE *inputFile, *outputFile;
  int rows, cols, M, N, i, j;
  int** a8;
  int* validColumns;
  int validColumnCount;
  int found;
  int min1;
  int k;
  int min2;
  int temp;
  int* resC;
  int resColCount;
  int x;
  int y;
  int valid;
  int originalColumnIndex;

  inputFile = fopen("input.txt", "r");
  if (inputFile == NULL) {
    perror("Error opening input file");
    return 1;
  }

  fscanf(inputFile, "%d %d %d %d", &rows, &cols, &M, &N);

  a8 = (int**)malloc(rows * sizeof(int*) + rows * cols * sizeof(int));
  if (!a8) {
    perror("Memory allocation error");
    fclose(inputFile);
    return 1;
  }
  a8[0] = (int*)(a8 + rows);
  for (i = 1; i < rows; i++) {
    a8[i] = a8[i - 1] + cols;
  }

  for (i = 0; i < rows; i++) {
    for (j = 0; j < cols; j++) {
      fscanf(inputFile, "%d", &a8[i][j]);
    }
  }

  fclose(inputFile);

  validColumns = (int*)malloc(cols * sizeof(int));

  validColumnCount = 0;
  for (j = 0; j < cols; j++) {
    found = 0;
    for (i = 0; i < rows; i++) {
      if (a8[i][j] % M == N) {
        found = 1;
        break;
      }
    }
ElatedGuy
prime dawnBOT
#
if (found) {
  validColumns[validColumnCount++] = j;
}
}

for (i = 0; i < validColumnCount - 1; i++) {
  for (j = 0; j < validColumnCount - i - 1; j++) {
    min1 = a8[0][validColumns[j]];
    for (k = 1; k < rows; k++) {
      if (a8[k][validColumns[j]] < min1) {
        min1 = a8[k][validColumns[j]];
      }
    }
    min2 = a8[0][validColumns[j + 1]];
    for (k = 1; k < rows; k++) {
      if (a8[k][validColumns[j + 1]] < min2) {
        min2 = a8[k][validColumns[j + 1]];
      }
    }
    if (min1 < min2) {
      temp = validColumns[j];
      validColumns[j] = validColumns[j + 1];
      validColumns[j + 1] = temp;
    }
  }
}

resC = (int*)malloc(cols * sizeof(int));

resColCount = 0;

for (x = 0; x < validColumnCount; x++) {
  resC[resColCount++] = validColumns[x];
}

for (x = 0; x < cols; x++) {
  valid = 0;
  for (y = 0; y < validColumnCount; y++) {
    if (x == validColumns[y]) {
      valid = 1;
      break;
    }
  }
  if (!valid) {
    resC[resColCount++] = x;
  }
}

outputFile = fopen("output.txt", "w");

if (outputFile == NULL) {
  perror("Error opening output file");
  free(a8);
  free(validColumns);
  free(resC);
  return 1;
}

for (i = 0; i < rows; i++) {
  for (j = 0; j < cols; j++) {
    originalColumnIndex = resC[j];
    fprintf(outputFile, "%d ", a8[i][originalColumnIndex]);
    printf("%d ", a8[i][originalColumnIndex]);
  }
  fprintf(outputFile, "\n");
  printf("\n");
}

fclose(outputFile);
free(a8);
free(validColumns);
free(resC);

return 0;
}
ElatedGuy
pastel palm
#

question: is a8 supposed to be a 2D array of int, or a 2D array of int*? i can't really tell. also, just out of curiosity, is C89 a requirement?

#

NVM the first question, i figured it out -- it's meant to be a 2D array of int

#

i would still be interested to know if C89 is a requirement, though

pastel palm
#

question: can you use stdlib.h's qsort()?

pastel palm
#

what i'd do to solve your problem is copy the valid indices out to a secondary array, sort the secondary array, then insert them back into the main array at the appropriate positions. what i would write would be something like thisc fscanf(fp, "%d %d %d %d", &rows, &cols, &M, &N); int (*mat)[cols] = malloc(sizeof(int[rows][cols])); if (mat == NULL) return 2; int* sortIdxs = malloc(sizeof(int[rows])); if (sortIdxs == NULL) return 3; int stkptr = 0; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { fscanf(fp, "%d", &mat[i][j]); if (mat[i][j] % M == N) sortIdxs[stkptr++] = i; } } fclose(fp); int (*sortMat)[cols] = malloc(sizeof(int[stkptr][cols])); if (sortMat == NULL) return 4; for (int i = 0; i < stkptr; i++) { memcpy(sortMat[i], mat[sortIdxs[i]], sizeof(int[cols])); } qsort(sortMat, stkptr, sizeof(int[cols]), cmp); for (int i = 0; i < stkptr; i++) { memcpy(mat[sortIdxs[i]], sortMat[i], sizeof(int[cols])); } but if C89 is a requirement for you, or if you can't use qsort(), YMMV

storm lagoon
#

!solved