Need Help Sorting Strings From Array By Increasing Length

So for an assignment, I have to mod a selection sort algorithm to sort an array of strings by increasing length. I did some work to the point where I can randomly get a word out of an array of chars but I then get these symbols here. What am I'm doing wrong?

EDIT: Add source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <stdlib.h>

using namespace std;

/**
    Gets the position of the smallest element in an array range.
    @param a the array
    @param from the beginning of the range
    @param to the end of the range
    @return the position of the smallest element in
    the range a[from]...a[to]
*/
int min_position(string a[], int from, int to)
{
   int min_pos = from;
   for (int i = from + 1; i <= to; i++)
   {
      if (a[i] < a[min_pos]) { min_pos = i; }
   }
   return min_pos;
}

/**
   Swaps two integers.
   @param x the first integer to swap
   @param y the second integer to swap
*/
void swap(int& x, int& y)
{
   int temp = x;
   x = y;
   y = temp;
}

/**
   Sorts a array using the selection sort algorithm
   @param a the array to sort
   @param size the number of elements in a
*/
void selection_sort(string a[], int size)
{
   int next; // The next position to be set to the minimum

   for (next = 0; next < size - 1; next++)
   {
      // Find the position of the minimum starting at next
      int min_pos = min_position(a, next, size - 1);
      // Swap the next element and the minimum
      swap(a[next], a[min_pos]);
   }
}

/**
   Prints all elements in an array.
   @param a the array to print
   @param size the number of elements in a
*/
void print(string a[], int size)
{
   for (int i = 0; i < size; i++)
   {
      cout << a[i] << " ";
   }
   cout << endl;
}

int main()
{
  srand(time(0));
    const int  SIZE = 6;
   // changed value to Words
   string Words[SIZE]={"School" , "To", "Sky" ,"Grade" , "A","Amazing"};
   
  

   for(int i = 0; i < SIZE; i++)
   {
      Words[i]= rand()% 100;
     
   }
   print(Words, SIZE);
   selection_sort(Words, SIZE);
   print(Words, SIZE);
   return 0;
}
Last edited on
Please edit for readability
https://www.cplusplus.com/articles/jEywvCM9/

The question is, why doesn't the compiler make remarks about this?
1
2
3
4
5
6
   string Words[SIZE]={"School" , "To", "Sky" ,"Grade" , "A","Amazing"};

   for(int i = 0; i < SIZE; i++)
   {
      Words[i] = rand() % 100;
   }

Why is it "legal" to:
1
2
3
string word;
int answer = 42;
word = answer;

That is what you do; assign integer values to strings. Integer converts to a char and then a string is created from the char. Look at ASCII table to see what prints out for numeric values.

The loop assigns a value to each element of the array. The initial values are overwritten. Result would be same with:
1
2
3
4
5
6
   string Words[SIZE];

   for(int i = 0; i < SIZE; i++)
   {
      Words[i] = rand() % 100;
   }


Use shuffle to change order of words in array Words:
http://www.cplusplus.com/reference/algorithm/shuffle/
Last edited on
Ok so I made changes to the code to where I got the words to come out but I'm having problems trying to put them in order by increasing length. I'm trying to use string.length() but I don't know where to put it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <stdlib.h>
#include <array>

using namespace std;

/**
    Gets the position of the smallest element in an array range.
    @param a the array
    @param from the beginning of the range
    @param to the end of the range
    @return the position of the smallest element in
    the range a[from]...a[to]
*/
int min_position(string a[], int from, int to)
{
   int min_pos = from;
   for (int i = from + 1; i <= to; i++)
   {
      if (a[i] < a[min_pos]) { min_pos = i; }
   }
   return min_pos;
}

/**
   Swaps two integers.
   @param x the first integer to swap
   @param y the second integer to swap
*/
void swap(int& x, int& y)
{
   int temp = x;
   x = y;
   y = temp;
}

/**
   Sorts a array using the selection sort algorithm
   @param a the array to sort
   @param size the number of elements in a
*/
void selection_sort(string a[], int size)
{
   int next; // The next position to be set to the minimum

   for (next = 0; next < size - 1; next++)
   {
      // Find the position of the minimum starting at next
      int min_pos = min_position(a, next, size - 1);
      // Swap the next element and the minimum
      swap(a[next], a[min_pos]);
   }




}

/**
   Prints all elements in an array.
   @param a the array to print
   @param size the number of elements in a
*/
void print(string a[], int size)
{
   for (int i = 0; i < size; i++)
   {
      cout << a[i] << " ";
   }
   cout << endl;
}

int main()
{
  srand(time(0));
    const int  SIZE = 6;
   // changed value to Words
   string Words[SIZE]={"School" , "To", "Sky" ,"Grade" , "A","Amazing"};
    

   for(int i = 0; i < SIZE; i++)
   {
      int index = rand()%SIZE;

      string tempword= Words[i];
      Words[i]= Words[index];
      Words[index]=tempword; 

     
   }
   print(Words, SIZE);
   selection_sort(Words, SIZE);
   print(Words, SIZE);
   return 0;
}
I'm trying to use string.length() but I don't know where to put it.

What decides, whether item is in correct position (or not)?
On line 54 you select the element from array that must be the first element in that array.

How do you do that?
1
2
3
4
5
6
7
8
9
int min_position(string a[], int from, int to)
{
   int min_pos = from;
   for (int i = from + 1; i <= to; i++)
   {
      if (a[i] < a[min_pos]) { min_pos = i; }
   }
   return min_pos;
}

Look at the bolded expression. Is that not the one that should say: This is the shorter of these.
So I put the code at the int min_position function like this but I'm missing something when it comes to comparing the lengths of the strings. I know it has something to do with int min_pos=min and that I think it needs to be changed but I don't know how it should be done because I know that there the B.length() and a[min_pos] are different types that can't be compared together.

1
2
3
4
5
6
7
8
9
10
int min_position(string a[], int from, int to)
{
   int min_pos = from;
   for (int i = from + 1; i <= to; i++)
   {
      string B = a[i];
      if (B.length() < a[min_pos]) { min_pos = i; }
   }
   return min_pos;
}
Last edited on
Integer vs string.

You create string B. A copy. That is fine. Let me create a second string:
1
2
3
4
5
6
7
8
9
10
11
int min_position(string a[], int from, int to)
{
   int min_pos = from;
   for (int i = from + 1; i <= to; i++)
   {
      string B = a[i];
      string C = a[min_pos];
      if ( B.length() < C ) { min_pos = i; }
   }
   return min_pos;
}

Does that make it more clear, why the types are different?

1
2
3
4
5
   int min_pos = from;
   for (int i = from + 1; i <= to; i++)
   {
      if ( a[i].length() < a[min_pos].length() ) { min_pos = i; }
   }
Topic archived. No new replies allowed.