For Loop Problem

This is a discussion on For Loop Problem within the C Programming forums, part of the General Programming Boards category; Hi, I can't seem to get out of the first for loop below, so that it will pass the user ...

  1. #1
    xp5
    xp5 is offline
    Registered User
    Join Date
    Aug 2007
    Posts
    33

    For Loop Problem

    Hi, I can't seem to get out of the first for loop below, so that it will pass the user inputed array on to the sorting function. Is there something that I'm doing wrong with the first for loop?

    Code:
    #include <stdio.h>
    #define  NUM_INT 1000
    
    void selection_sort(int x[], int lim);
    int get_maxpos(int x[], int lim);
    void print_array(int x[], int lim);
    
    int main()
    {
      int input[NUM_INT] = {0};
      int i;
    
      printf("please input arrays of ints to be sorted ");
      for (i = 0; i < NUM_INT; i++)   //CAN'T GET OUT OF THIS FOR LOOP!!!
      {
        scanf("%d", &input[i]);
        printf("%d ", input[i]);  //TESTING
      }
    
      printf("Original array:\n");
      print_array(input, NUM_INT);
      selection_sort(input, NUM_INT);
      printf("Sorted array:\n");
      print_array(input, NUM_INT);
    
    
    }
    
    void selection_sort(int x[], int lim)
    {
      int eff_size, maxpos, tmp;
    
      for (eff_size = lim; eff_size > 1; eff_size--)
      {
        maxpos = get_maxpos(x, eff_size);
        tmp = x[maxpos];
        x[maxpos] = x[eff_size -1];
        x[eff_size -1] = tmp;
      }
    }
    
    int get_maxpos(int x[], int eff_size)
    {
      int i, maxpos = 0;
    
      for (i = 0; i < eff_size; i++)
        maxpos = x[i] > x[maxpos] ? i: maxpos;
      return maxpos;
    
    }
    
    void print_array(int x[], int lim)
    {
      int i;
    
      for (i = 0; i < lim; i++)
        printf("%d ", x[i]);
    
    }

  2. #2
    xp5
    xp5 is offline
    Registered User
    Join Date
    Aug 2007
    Posts
    33
    sorry i figure it out!

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Code:
    #include <stdio.h>
    #define  NUM_INT 1000
    
    void selection_sort(int x[], int lim);
    int get_maxpos(int x[], int lim);
    void print_array(int x[], int lim);
    
    int main()
    {
      int input[NUM_INT] = {0};
      int i;
    
      printf("please input arrays of ints to be sorted ");
      for (i = 0; i < NUM_INT; i++)   //CAN'T GET OUT OF THIS FOR LOOP!!!
      {
        scanf("&#37;d", &input[i]);
        printf("%d ", input[i]);  //TESTING
      }
    Well, as written, that for loop reads in 1000 numbers. Since you probably don't want to be typing 1000 numbers, perhaps you should keep reading numbers until the user enters -1 or something, break from the loop, and then pass the number of numbers read on to print_array() etc instead of NUM_INT.

    Something like this:
    Code:
    int main()
    {
      int input[NUM_INT] = {0};
      int i;
    
      printf("please input arrays of ints to be sorted ");
      for (i = 0; i < NUM_INT; i++)   //CAN'T GET OUT OF THIS FOR LOOP!!!
      {
        scanf("%d", &input[i]);
        if(input[i] < 0) break;
        printf("%d ", input[i]);  //TESTING
      }
    
      printf("Original array:\n");
      print_array(input, i);
      selection_sort(input, i);
      printf("Sorted array:\n");
      print_array(input, i);
    
      return 0;
    }
    (I also added a return 0, which you should have.)

    [edit] Well, never mind if you figured it out. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    xp5
    xp5 is offline
    Registered User
    Join Date
    Aug 2007
    Posts
    33
    hey thanks, I absolutely forgot about the break command!!

  5. #5
    xp5
    xp5 is offline
    Registered User
    Join Date
    Aug 2007
    Posts
    33

    A different for loop problem

    Hi, this is sorting selection program that i'm writing. The sorting works fine, however, I don't know how to make it so that the program sort a group of values then go to the next group of values to sort again.

    Such as, if user's input: "32 1 92 23 -1 43 19 22 -2". The program will sort the first group (32 1 92 23) but I don't know how to make it so that it will sort the second group (43 19 22). The "-1" indicates next group, the "-2" indicates the end of the array.

    Code:
    #include <stdio.h>
    #define  NUM_INT 1000
    
    void selection_sort(int x[], int size);
    int get_maxpos(int x[], int size);
    void print_array(int x[], int size);
    
    int main()
    {
      int input[NUM_INT] = {0};
      int i;
      int j = 0;
    
      printf("please input arrays of ints to be sorted ");
      for (i = 0; i < NUM_INT; i++)
      {
        scanf("&#37;d", &input[i]);
    
        if(input[i] == -1)        //NOT SURE WHAT TO DO HERE ?????????????????? TROUBLE SPOT!!
        {
          i = 0;
          printf("%d\n ", input[i]);  //TESTING
    
        }
    
        if(input[i] == -2) break;
        if(input[i] < -2)
        {
          printf("One of the integers is less than -2");
        }
        /*
        printf("Original array:\n");
        print_array(input, i);
        selection_sort(input, i);
        printf("Sorted array:\n");
        print_array(input, i);
        */
        // printf("FIRST READ IS: %d\n ", input[i]);  //TESTING
      }
    
      printf("Original array:\n");    
      print_array(input, i);
      selection_sort(input, i);
      printf("\nSorted array:\n");
      print_array(input, i);
    
      //IS THERE A WAY TO GO BACK TO THE FOR LOOP TO READ THE SECOND GROUP AT THIS POINT???
    
      return 0;
    }
    
    void selection_sort(int x[], int size)
    {
      int eff_size, maxpos, tmp;
    
      for (eff_size = size; eff_size > 1; eff_size--)
      {
        maxpos = get_maxpos(x, eff_size);
        tmp = x[maxpos];
        x[maxpos] = x[eff_size -1];
        x[eff_size -1] = tmp;
      }
    }
    
    int get_maxpos(int x[], int size)
    {
      int i, maxpos = 0;
    
      for (i = 0; i < size; i++)
        maxpos = x[i] > x[maxpos] ? i: maxpos;
      return maxpos;
    
    }
    
    void print_array(int x[], int size)
    {
      int i;
    
      for (i = 0; i < size; i++)
        printf("%d ", x[i]);
    
    }

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Hmm. The easiest way that I can think of is to do something like this:
    Code:
    do {
      for (i = 0; i < NUM_INT; i++)
      {
        scanf("%d", &input[i]);
        if(input[i] < 0) break;
      }
    
      printf("Original array:\n");
      print_array(input, i);
      selection_sort(input, i);
      printf("Sorted array:\n");
      print_array(input, i);
    } while(i > -2);
    That lets you enter as many arrays as you like, separated with -1; the last array is terminated with a number less than or equal to -2.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, do you want to sort only the second range of numbers (creating a sequence like this:"1 23 32 92", "19 22 43" , or all of them together (creating a sequence like 1 19 22 23 32 43 92") - I'm ignoring the negative numbers.

    If you want the former, you'll need to fix up your sorting method to take a "first and last" position, rather than size. If you went the second form, just sort the numbers you've got so far while you are still in the loop [in your "if input == -1" part], then continue until you get the -2 value, at which point you sort all again.

    Edit: beaten by dwks (again - must learn to type faster!!).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    xp5
    xp5 is offline
    Registered User
    Join Date
    Aug 2007
    Posts
    33
    Thank you dwks and matsp !!!

    matsp: Yes, I want the former values (the first group) to be sorted with the second group of values too.

    I'm working on that right now.

  9. #9
    xp5
    xp5 is offline
    Registered User
    Join Date
    Aug 2007
    Posts
    33
    The sorting is working perfectly fine now. I have another question, how do I make it print 10 integers per line? For example the sorted array is: "1 2 3 4 5 6 7 8 9 10 11 12 13". How do i make the program print (under function print_array()):
    "1 2 3 4 5 6 7 8 9 10" new line here
    "11 12 13"

    Code:
    #include <stdio.h>
    #define  NUM_INT 1000
    
    void selection_sort(int x[], int size);
    int get_maxpos(int x[], int size);
    void print_array(int x[], int size);
    
    int main()
    {
      int input[NUM_INT] = {0};
      int i = 0;
      int j = 0;
    
      printf("please input arrays of ints to be sorted ");
    
     do {
      for ( ; i < NUM_INT; i++)
      {
        scanf("&#37;d", &input[i]);
        if(input[i] < 0) break;
      }
    
      printf("Original array:\n");
      print_array(input, i);
      selection_sort(input, i);
      printf("Sorted array:\n");
      print_array(input, i);
    } while(input[i] > -2);
    
      return 0;
    }
    
    void selection_sort(int x[], int size)
    {
      int eff_size, maxpos, tmp;
    
      for (eff_size = size; eff_size > 1; eff_size--)
      {
        maxpos = get_maxpos(x, eff_size);
        tmp = x[maxpos];
        x[maxpos] = x[eff_size -1];
        x[eff_size -1] = tmp;
      }
    }
    
    int get_maxpos(int x[], int size)
    {
      int i, maxpos = 0;
      for (i = 0; i < size; i++)
        maxpos = x[i] > x[maxpos] ? i: maxpos;
      return maxpos;
    
    }
    
    void print_array(int x[], int size)
    {
      int i;
    
      for (i = 0; i < size; i++)
      {
       if (i>9)
       {
         printf("\n");
         printf("%d ", x[i]);
       }
       else
       {
         printf("%d ", x[i]);
       }
    
      }
    }

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You're on the right track. Two things you could do:
    1. Add a second variable, count it up, compare with 9 (or whatever number). Print a newline when it's at that point, then set the variable to zero.

    2. Use modulo (%) to see if it's a "ends with 9" number, e.g. if (i % 10 == 9) ...

    Don't duplicate code on both sides of an else if you can avoid it:
    Code:
       if (i>9)
       {
         printf("\n");
       }
       printf("%d ", x[i]);
    does the same thing as your code, but it's shorter, and only one place to change when you want to do a different format for your number (hint: You man find it looking better if you use %6d or some such).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    xp5
    xp5 is offline
    Registered User
    Join Date
    Aug 2007
    Posts
    33
    I got it!! thank you matsp!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21