-1

I am doing an assignment which is: Find k largest elements of a file. Allocate an array of size k and while you read the numbers from the file, store the k largest numbers in the array. When you read the next element from the file, find if the array needs to be modified or not. Assume that next element read is 80. Since 80 is larger than the smallest element, we need to shift elements < 80 to the right by 1 position and create space for 80. In main() use argc and argv to read the filename and k from the user and compute and print the k largest elements. Name your program assign3.c First parameter is filename and second parameter is k. You need to use atoi()in stdlib.h to convert strings to integer.

My problem is that I am getting garbage values for both arr and sorted arr?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
int main(int argc, char *argv[])//
{
    FILE *iFile;//file pointer
    int i = 0, n, temp = 0, count = 0, j;
    int k = atoi(argv[1]);//convert strings into int    
    int *arr = (int *)malloc(k * sizeof(int));////allocate an array of size k
    iFile = fopen("a.txt", "r");//opens file
    if (iFile == NULL)
        return -1;
    while (feof(iFile) <= 0)
    {
        fscanf(iFile, "%d", arr);
        printf("arr= %d\n", arr);
        count = count++;
        for (i = 0; i < count; i++)                     //Loop for descending ordering
        {
            for (j = 1; j <= count; j++)             //Loop for comparing other values
            {
                if (arr[j] < arr[i])                //Comparing other array elements
                {
                    temp = arr[i];         //Using temporary variable for storing last value
                    arr[i] = arr[j];            //replacing value
                    arr[j] = temp;             //storing last value
                }
            }
        }
    }
    for (i = 0; i < k + 1; i++)
        printf("sorted arr is =%d\n", arr[i]);

    fclose(iFile);
    free(arr);
}
Serene
  • 59
  • 2
  • 8
  • `fscanf` return the number of element that have been successfully parsed, use `while(fscanf(iFile, "%d", &arr[i]) != 1)` – dvhh Mar 28 '19 at 04:38
  • 2
    If I understand you correctly, you have to read the largest *k* number of integers from a file containing an arbitrary number *N* integers, and you're asking how to find out the value of *N*? The value of *N* is not germane to the problem. You only need to read through the file once, no matter how large *N* is. – Mark Benningfield Mar 28 '19 at 04:53
  • @MarkBenningfield you summed up my whole problem in only one line! but I need to compare each and every element right? won't reading a file just read the file?? – Serene Mar 28 '19 at 05:01
  • 1
    Don't read directly into the array; use a temp variable. Compare that to the smallest value that you have read in so far. If it is larger, and you haven't read in *k* values yet, stick it in the array at the current location (that you are also keeping track of). Once you have read in *k* values, if you read a value that is larger than your smallest value so far, dump the smallest, add the new value, and sort the array. Pick the new smallest, and continue until you reach the end of the file. – Mark Benningfield Mar 28 '19 at 05:11
  • What exactly is the problem with "When you read the next element from the file, find if the array needs to be modified or not."? When you read a new value it is not yet part of your final array. You need to compare against the content of the array. Only then put it in the array (at the correct position). This requires that your read value musn't go into `arr[i]` directly. – Gerhardh Mar 28 '19 at 07:07
  • @MarkBenningfield I posted my updated my code which is now giving me garbage outputs. I don't know why. – Serene Mar 29 '19 at 05:54
  • @Gerhardh My sorting is not working right?? – Serene Mar 29 '19 at 05:55
  • Yes, you will probably swap each pair twice. Don't do a full sort for each value. Just insert each value in correct position. And shift values to make it fit. No need for swapping pair after pair. – Gerhardh Mar 29 '19 at 07:48
  • 1
    Please don't modify your code. This makes all previous comments and answers useless. No one wants to aim at a moving target. If you do modifications to your code, you can add it as new version but you shouldn't delete the initial code. – Gerhardh Mar 29 '19 at 08:58
  • 1
    @Gerhardh I am sorry about that. From now on I will keep the old code commented at the end of my new code. – Serene Mar 29 '19 at 09:56

1 Answers1

0

Use a while loop condition like this

While(!feof(iFile)) ....

feof() checks whether end of file is reached and returns 0 otherwise

  • 3
    Why is “while (!feof(file))” always wrong? - `https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong' – MayurK Mar 28 '19 at 06:12
  • the loop will break when file pointer(iFile) has reached the end of the file .In that case feof(iFile) returns 1 hence !feof(iFile) will be 0 hence will break the loop . – Ajay T Jose Mar 28 '19 at 07:18
  • 1
    like @MayurK said feof() only checks to see if the end of file marker has been seen - it doesn't do anything for any other kind of end condition (including if the file doesn't have an of file marker) Never use feof as a loop condition - always use something that checks for successful read. – Jerry Jeremiah Mar 29 '19 at 06:19
  • @jerry yes you are correct, but it works for most cases as far as I've seen . – Ajay T Jose Mar 30 '19 at 07:10