-2

I was hoping to get a bit of help, I am implementing an inversion counter algorithm to take in 50,000 intergers and display the inversions and time it took to run the algorithm, I am having a hard time allocating and saving the integers from the file into an array. My code complies and runs but nothing happens here is what I have:

int main(int argc, char** argv)
{
 int n, i;
 int inversions=0;
 int *A;
 FILE *file;
 char filename[100];
 clock_t start, end;
 double totalTime;

 printf("Enter filename: ");
 scanf("%s", filename);

 file = fopen(filename, "r");
 if(file == NULL)
 {
   printf("Error opening file!\n");
   return 0;
 }

 fscanf(file, "%d", &n);
 A = (int*) malloc(n * sizeof(int));
 for(i = 0; i < n; i++) {
 fscanf(file, "%d", &A[i]);
}

 start = clock();
 inversions = countInversionsBruteForce(A, n);
 end = clock();
 totalTime = (double) (end - start) / CLOCKS_PER_SEC;
 printf("Brute Force Algorithm\n");
 printf("Number of inversions: %d\n", inversions);
 printf("Execution time: %f\n", totalTime);

I think I have noth allocated array size and saved it properly

David
  • 21
  • 3
  • You've tagged both `malloc` and `scanf`, but the code shown doesn't bother to check the return codes from those functions... That may be something to investigate... AND, those braces being where they are would not compile... – Fe2O3 Feb 05 '23 at 21:33
  • Your program is incomplete, poorly formatted. In C we don't cast void pointers (it's required in C++). – Allan Wind Feb 05 '23 at 23:11
  • Questions seeking debugging help should generally provide a [mre] of the problem. Your posted code does not fulfill this critieria. The function `main` is incomplete and your program is missing all `#include` directives. – Andreas Wenzel Feb 06 '23 at 07:29
  • Have you tried running your code line-by-line in a debugger while monitoring the control flow and the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Feb 06 '23 at 07:29

1 Answers1

0

Your program is incomplete so I was not able to compile it. Minimized the problem to just loading the data into your array:

  1. Formatted code for readability.
  2. Generated a suitable input file. Most likely this is your problem but you have not shared your input sample with us.
  3. Added missing include files.
  4. Remove argc, argv as you not using them.
  5. Minimize scope of variables. Use size_t instead of int for unsigned values.
  6. Max string size on obtaining file name
  7. Check return value for scanf(), fopen(), fscanf().
  8. Printing out the data read to demonstrate it's working.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    printf("Enter filename: ");
    char filename[100];
    if(scanf("%99s", filename) != 1) {
        printf("scanf failed\n");
        return 1;
    }

    FILE *file = fopen(filename, "r");
    if(!file) {
        printf("Error opening file!\n");
        return 1;
    }

    size_t n;
    fscanf(file, "%zu", &n);
    if(!n) {
        printf("n must be positive");
        return 1;
    }
    int *A = malloc(n * sizeof(*A));
    for(size_t i = 0; i < n; i++)
        if(fscanf(file, "%d", &A[i]) != 1) {
            printf("fscanf() failed\n");
            return 1;
        }

    printf("n = %zu\n", n);
    printf("A = ");
    for(size_t i = 0; i < n; i++)
        printf("%d%s", A[i], i + 1 < n ? ", " : "\n");
}

with 1.txt as:

4
1
2
3
4

a sample session looks like this:

Enter filename: 1.txt 
n = 4
A = 1, 2, 3, 4
Allan Wind
  • 23,068
  • 5
  • 28
  • 38