0

There is a task: there are only integers in the file, the number of these numbers is no more than 30 on one line (that is, the numbers can be on different lines), you need to remove the largest negative value from each line, while if all the negative numbers in the line are the same, there is one negative number in the line, then you don't need to delete anything.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned read_string(int*, FILE*);
void print(int*, unsigned, FILE*);
void remove_max_negative(int*, unsigned*);
int main() {
    int values[30], s;
    unsigned size, n = 1;
    FILE* file = fopen("1.txt", "r");
        if(!file){
        printf("error);
        return 1;
        }
    FILE* out = fopen("r.text", "w");
        if(!out){
        printf("error);
        return 1;
        }
    while (size = read_string(values, file)) {
        remove_max_negative(values, &size);
        print(values, size, out);
    }
    fclose(file);
    fclose(out);
    remove("1.txt");
    rename("r.txt", "1.txt");
    return EXIT_SUCCESS;
}
unsigned read_string(int* values, FILE* stream) {
    char value[1024];
    unsigned result = 0;
    fscanf(stream, "%s", value);
    while (!feof(stream)) {
        values[result] = atoi(value);
        result++;
        if (getc(stream) == '\n') return result;
        fscanf(stream, "%s", value);
    }
    return result;
}
void print(int* values, unsigned size, FILE* output) {
    for (int i = 0; i < size; i++) {
        fprintf(output, "%d ", values[i]);
    }
    fprintf(output, "\n");
}
void remove_max_negative(int* values, unsigned* size) {
    int max_value = -2147483648, flag = 0, index_max_value = -1;
    for (int i = 0; i < *size; i++) {
        if (values[i] < 0) {
            if (values[i] > max_value) {
                flag = 0;
                max_value = values[i];
                index_max_value = i;
            }
            else if (values[i] == max_value) {
                flag = 1;
                index_max_value = i;
            }
            else if (flag) {
                index_max_value = i;
            }
        }
    }if (index_max_value != -1) {
        --* size;
        for (int i = index_max_value; i < *size; i++) {
            values[i] = values[i + 1];
        }
    }
}

If you enter:

1 2 -4 5 2 -4 6 -4 10 5 6 -7 -7 9 -8

Then the program, instead of removing the value I need, removes the most recent negative, regardless of what number is there. I did quite a lot of tests, but the only conclusion I made was that the error occurs not because of the number of numbers, but because of repeated negative values. I can't find any errors in the code myself

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
citn
  • 1
  • 1
  • See [**Why is “while( !feof(file) )” always wrong?**](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) Your loop in `read_string()` to read from the file likely isn't doing what you think it is. – Andrew Henle Jan 17 '23 at 01:32
  • `index_max_value` seems to be set to `i` a little too often... – Fe2O3 Jan 17 '23 at 01:38
  • 1
    In the example data, is `-4` the largest negative value, or is `-8` the largest negative value? A case can be made for either — in casual speech, `-8` has the largest absolute value, but in more formal speech, `-4` is larger (less negative) than `-7` or `-8`. – Jonathan Leffler Jan 17 '23 at 05:41
  • @JonathanLeffler -4, because if you compare them in a mathematical sense, then -4 >-8, but formally, of course, yes, it's a little unclear, I apologize for the flaw – citn Jan 17 '23 at 12:05
  • @Fe2O3 why? there are conditions there, why would he suddenly go through all the conditions and constantly accept a new index? provided that there are no paired negative numbers because everything works – citn Jan 17 '23 at 12:07
  • @AndrewHenle I would never have thought that `feof` is such a problematic function. in all the algorithms that I have looked through at work and reading lines of files, this function has always been used, but if it is incorrect, then what will be its analogue: `while (fgets(line, sizeof(line), file))` or `while ((c=getc(stream))!=EOF)` ? – citn Jan 17 '23 at 12:13
  • As mentioned by @JonathanLeffler, it is unclear what you mean with "_the largest negative number_" And, should such a number appear more than once, do you want to remove the first instance or the last... Regardless, as per my earlier comment, try your code after commenting out the assignment in the `if (flag)` block... This assignment really doesn't make sense... (Hint: Use a sample data set of 4-5 values, then follow the execution using a debugger or even "print" statements... Cast some light into the black box operation...) – Fe2O3 Jan 17 '23 at 21:16

0 Answers0