I am having troubles getting my program to work. It is supposed to take integers from a file and put them into an array. Then it is supposed to sort them by the bubble sort algorithm.

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

#define N 10000

int main(int argc, char *argv[])
{
   int i,j, k;
   int *ptr;
   int *temp;
   FILE *fp;

   fp=fopen(argv[i], "r");
        if(fp==NULL)
          {
            fprintf(stdout, "could not open %s\n", argv[i]);
            exit(EXIT_FAILURE);
          }

    fread(argv[i], sizeof(argv[i]), sizeof(argv[i]), fp);
 
  while (fscanf(fp, "%d", ptr)!=EOF))
         {
            int m=0, *a[N];
            ptr=a[m];
            m++;
         }


      for(k=0; k<N-1; k++)
           {
             if(a[k]>a[k+1])
               {
                  temp=a[k+1];
                   a[k+1]=a[k];
                   a[k]=temp;
                }
            }

        printf("%d", a);

       }
    return 0;
}

I am having troubles getting my program to work. It is supposed to take integers from a file and put them into an array. Then it is supposed to sort them by the bubble sort algorithm.

So are we supposed to figure out what kind of problems you're having? Or would you care to tell us? Explain in detail.

So are we supposed to figure out what kind of problems you're having? Or would you care to tell us? Explain in detail.

Its saying that my variables k, a, and temp are undeclared. And there's a syntax error before '}' on line 40. Sorry. I definitely should've put that in my original post.

Its saying that my variables k, a, and temp are undeclared. And there's a syntax error before '}' on line 40. Sorry. I definitely should've put that in my original post.

Yes you should have.

So where did you declare k, a, and temp?

I don't have a compiler on this machine, so I'm not really sure about your k and temp being undeclared, but you need to declare your 'm' and 'a[]' before your while loop or else it will try and make a new one every time it loops. And it looks like your braces are mismatched. The '}' on 43 is closing main. Try that and see how it changes your error messages.

Yes you should have.

So where did you declare k, a, and temp?

They are declared at the very beginning right under the first {.

Really? Show me the declaration for a?
And temp is a pointer, not an integer.

Do not tell us the errors, post them exactly as the compiler displays them.

In your sort, you only have one loop.

for(k=0; k<N-1; k++)
           {
             if(a[k]>a[k+1])
               {
                  temp=a[k+1];
                   a[k+1]=a[k];
                   a[k]=temp;
                }
            }

Which you'd see in an instant, if it was properly indented:

for(k=0; k<N-1; k++)
      {
         if(a[k]>a[k+1])
         {
            temp=a[k+1];
            a[k+1]=a[k];
            a[k]=temp;
         }
      }

And there is only one sorting algorithm that uses one loop, and that is Gnome Sort.

If you want to do a bubble sort, you have to add the second for loop (you could use a while loop, but don't).

I know I'm probably spitting into the wind here, but I really hope you see how easy it is to spot problems with your code, when you indent it right, and change your indentation style. Right now, the words that come to mind to describe it, are pretty volatile and have a high percentage of 4 letters.

Edited 6 Years Ago by Adak: n/a

He declared a inside the while loop as an array of pointers. The part where temp is assigned a[k+1] threw me off until I found the declaration.

Also, this doesn't directly related to the OP's stated problem, but what happens when you reference argv before giving 'i' a value?

I don't have a compiler on this machine, so I'm not really sure about your k and temp being undeclared, but you need to declare your 'm' and 'a[]' before your while loop or else it will try and make a new one every time it loops. And it looks like your braces are mismatched. The '}' on 43 is closing main. Try that and see how it changes your error messages.

Now it works but its not sorting correctly. It is only taking into account the first number. So for example 7 is after 16. That was because I was using the word sort in my Makefile. Now it is saying Segmentation Fault Core Dumped.

Edited 6 Years Ago by alcx88: n/a

Now I have this as my code and it says Segmentation Fault Core Dumped.

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


int main(int argc, char *argv[])
{
   int i,k,swapped;
   int *ptr;
   int *temp;
   int m,N=10000;
   int *a[N];
   FILE *fp;
 
   fp=fopen(argv[i], "r");
        if(fp==NULL)
          {
            fprintf(stdout, "could not open %s\n", argv[i]);
            exit(EXIT_FAILURE);
          }
  fread(argv[i], sizeof(argv[i]), sizeof(argv[i]), fp);

    while (fscanf(fp,"%d",&ptr )!=EOF)
         {
            ptr=a[m];
            m++;
         }


     do
     {
      swapped=0;

      for(k=0; k<N-2; k++)
       {
         if(a[k]>a[k+1])
         {
            temp=a[k+1];
            a[k+1]=a[k];
            a[k]=temp;
            swapped=1;
         }
       }
    N=N-1;
     } while ((swapped=1)&&(N>1));

        printf("%d", a);

        fclose(fp);
    return 0;
}

You cannot load variable values into pointers. You can load addresses into pointers. So get rid of the pointers.

Think about this loop. Run it by hand. Does it do anything useful? Remember which values are variables and which are pointers and either use them properly of change them to what you really need.

while (fscanf(fp,"%d",&ptr )!=EOF)
{
    ptr=a[m];
    m++;
}

1) Delete the pointers from your program. You don't need them, and they are now an impediment to making your program, run.

2) You can't use a variable, any variable, until you have assigned it a value:

argv[i]

When a variable is global (declared before main()), then OK, the variable will be assigned zero, by the compiler.

All other variables, like this i variable above, just has some left over garbage value, that will vary from one run of the program, to the next. Nothing you can count on here!

Assign all variables to 0, unless you know they will be assigned someplace else, before they're used. You can do it like this, if you want to. All these variables are int's:

var1 = var2 = var3 = var4 = 0;

and all these variables will be assigned a zero value.


I like the bubble sort code - very nice. ;)

Clean up the rest, and you'll have it.

This article has been dead for over six months. Start a new discussion instead.