Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with array of pointers
    primarykey
    data
    text
    <p>I'm writing a program to alphabetize inputted names and ages. The ages are inputted separately, so I know I need to use an array of pointers to tie the ages to the array of names but I can't quite figure out how to go about doing it. Any ideas?</p> <p>So far, my program only alphabetizes the names.</p> <pre><code>/* program to alphabetize a list of inputted names and ages */ #include &lt;stdio.h&gt; #define MAXPEOPLE 50 #define STRSIZE int alpha_first(char *list[], int min_sub, int max_sub); void sort_str(char *list[], int n); int main(void) { char people[MAXPEOPLE][STRSIZE]; char *alpha[MAXPEOPLE]; int num_people, i; char one_char; printf("Enter number of people (0...%d)\n&gt; ", MAXPEOPLE); scanf("%d", &amp;num_people); do scanf("%c", &amp;one_char); while (one_char != '\n'); printf("Enter name %d (lastname, firstname): ", ); printf("Enter age %d: ", ); for (i = 0; i &lt; num_people; ++i) gets(people[i]); for (i = 0; i &lt; num_people; ++i) alpha[i] = people[i]; sort_str(alpha, num_people); printf("\n\n%-30s5c%-30s\n\n", "Original List", ' ', "Alphabetized List"); for (i = 0; i &lt; num_people; ++i) printf("-30s%5c%-30s\n", people[i], ' ', alpha[i]); return(0); } int alpha_first(char *list[], int min_sub, int max_sub) { int first, i; first = min_sub; for (i = min_sub + 1; i &lt;= max_sub; ++i) if (strcmp(list[i], list[first]) &lt; 0) first = i; return (first); } void sort_str(char *list[], int n) { int fill, index_of_min; char *temp; for (fill = 0; fill &lt; n - 1; ++fill){ index_of_min = alpha_first(list, fill, n - 1); if(index_of_min != fill){ temp = list[index_of_min]; list[index_of_min] = list[fill]; list[fill] = temp; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload