# For Loop Problem

• 09-05-2007
xp5
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]); }```
• 09-05-2007
xp5
sorry i figure it out!
• 09-05-2007
dwks
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.)

 Well, never mind if you figured it out. :) [/edit]
• 09-05-2007
xp5
hey thanks, I absolutely forgot about the break command!!
• 09-05-2007
xp5
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]); }```
• 09-05-2007
dwks
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.
• 09-05-2007
matsp
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
• 09-05-2007
xp5
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.
• 09-05-2007
xp5
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]);   }   } }```
• 09-05-2007
matsp
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
• 09-05-2007
xp5
I got it!! thank you matsp!