0

I try to make a function to get numbers from a text file and count numbers that do not overlap and exceed a certain number. And I'm going to put it in the linked list. Calculating a number that doesn't overlap with creating a linked list is a good thing, but there is a problem with counting numbers that exceed a certain number. It comes out less than the original number. In Excel, there are numbers that should come out, but in the case of the C program function that I made, they are less than that number.

#include<stdio.h>
#include<stdlib.h>

typedef struct Node {
    int key;
    struct Node* link;
} listNode;

typedef struct Head {
    struct Node* head;
}headNode;


int NodeCount = 0;
int Data_morethan5000_Count = 0;

headNode* initialize(headNode* rheadnode);
int DeleteList(headNode* rheadnode);
void GetData(headNode* rheadnode);
int InsertNode(headNode* rheadnode, int key);
void PrintResult(headNode* rheadnode);

int main()
{
    int key;
    headNode* headnode = NULL;

    headnode = initialize(headnode);
    GetData(headnode);
    PrintResult(headnode);

    DeleteList(headnode);

    return 0;
}

headNode* initialize(headNode* rheadnode) {
    headNode* temp = (headNode*)malloc(sizeof(headNode));
    temp->head = NULL;
    return temp;
}

int DeleteList(headNode* rheadnode) {
    listNode* p = rheadnode->head;

    listNode* prev = NULL;
    while (p != NULL) {
        prev = p;
        p = p->link;
        free(prev);
    }
    free(rheadnode);
    return 0;
}


void GetData(headNode* rheadnode)
{
    int dataType;
    int newData;
    FILE* fp = NULL;
    fp = fopen("input.txt", "r");
    if (fp != NULL) {

        while (fscanf(fp, "%d", &newData) != EOF)
        {
            dataType = InsertNode(rheadnode, newData);

            if (newData > 5000)
                Data_morethan5000_Count++;

            switch (dataType)
            {
            case 0:
                break;
            case 1:
                NodeCount++;
            }
        }
        fclose(fp);
    }
}

int InsertNode(headNode* rheadnode, int key) {

    listNode* search, * previous;
    listNode* node = (listNode*)malloc(sizeof(listNode));

    node->key = key;

    search = rheadnode->head;
    previous = NULL;
    while (search != NULL)
    {
        if (node->key < search->key)
        {
            previous = search;
            search = search->link;
        }
        else if (node->key == search->key)
            return 0;
        else
            break;
    }
    if (previous == NULL)
    {
        node->link = rheadnode->head;
        rheadnode->head = node;
    }
    else
    {
        node->link = search;
        previous->link = node;
    }
        return 1;
}

void PrintResult(headNode* rheadnode) {
    /*
    The total number of nodes: 10011
    More than 5000 values: 45460
    Execution time: 1.234567 sec
    */
    printf("The total number of nodes: %d\n", NodeCount);
    printf("More than 5000 values: %d\n", Data_morethan5000_Count);
    printf("Execution time:  sec");
}

The code above is my program code. I am using a method to get a number from the input.txt file, put it into the newData variable, and increase the value of Data_morethan5000_Count if it is over 5000. So the result should be 45460. However, the C program outputs a result value of 45432. I want to know where data loss is taking place.

  • 1
    It seems like a good time to learn how to use a debugger to step through your programs statement by statement while monitoring variables and their values. – Some programmer dude Sep 29 '19 at 15:23
  • I think it is difficult because there are 100,000 integer data inside the text file. – student12343 Sep 29 '19 at 15:28
  • 2
    Note: use `while (fscanf(fp, "%d", &newData) == 1)` rather than `while (fscanf(fp, "%d", &newData) != EOF)` to detect non-numeric input attempts. – chux - Reinstate Monica Sep 29 '19 at 15:32
  • 1
    @student12343 Use a smaller file for debugging purposes. – ggorlen Sep 29 '19 at 15:32
  • @ggorlen ok, I will. Thank you for your kind advice. – student12343 Sep 29 '19 at 15:35
  • @chux thanks for your advice. but That couldn't work. – student12343 Sep 29 '19 at 15:38
  • student12343 "That couldn't work" is unclear. Please explain more. – chux - Reinstate Monica Sep 29 '19 at 15:42
  • @student12343 While you're at it, post a small sample of the text file with expected output such that we can run the program on the file and see the discrepancy. See [mcve]. Also see https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc. – ggorlen Sep 29 '19 at 15:42
  • @ggorlen Ok, I want this result.`The total number of nodes: 10011 More than 5000 values: 45460 Execution time: 1.234567 sec` but I got this `The total number of nodes: 10011 More than 5000 values: 45432 Execution time: sec` execution time was not implemented because the Visual Studio failed to read . – student12343 Sep 29 '19 at 15:48
  • @ggorlen and then I understand `listNode* node = (listNode*)malloc(sizeof(listNode));` is not good way for programming. is that right? – student12343 Sep 29 '19 at 15:55
  • @ chux Sorry, I want this result. `The total number of nodes: 10011 More than 5000 values: 45460 Execution time: 1.234567 sec` but I got this `The total number of nodes: 10011 More than 5000 values: 45432 Execution time: sec` execution time was not implemented because the Visual Studio failed to read – student12343 Sep 29 '19 at 15:59

0 Answers0