Advertisement
2002ASP Sorting #7567

Sort a List of 20 Names

The user enters 20 names (or whatever) into an array and the program outputs the 20 inputs alphabetically. The program also stops and sorts what the user has entered so far if the user does not enter anything.

AI

Shrnutí AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Zdrojový kód
original-source
/* By Dfuse
http://dfuse.netfirms.com
molsoncanadians@hotmail.com
*/
#include<iostream.h>
#include <string.h>
void getName(char* name)
{ cout<<"Enter a Name: ";
 cin.getline(name, 20, '\n');
 if(strlen(name)==19)
  cin.ignore(100,'\n');
} //getName
readAllNames(char list [] [20])
{ 	int i=0;
	getName(list[i]);
	while((i<19)&&(strlen(list[i])>0))
	{ i++;
	 getName(list[i]);
	} //while 
return i+1;
}//readAllNames
	
void printAll(char list[][20],int c)
{ for (int i=0;i<c;i++)
  cout<<list[i]<<"\n";
} //printAll
void swap(char *s1, char *s2)
{char* temp;
 strcpy(temp,s1);
 strcpy(s1,s2);
 strcpy(s2,temp);
} //swap
sort(char list[][20], int n)
{ int i, smallPos, pass;
  char temp;
  for (pass=0; pass<n; pass++)
  { smallPos = pass; 
		for (i=pass; i<n; i++)
			{ if (stricmp (list[i], list[smallPos])< 0)
			 smallPos = i; 
			} //for
  swap(list[smallPos], list[pass]);
  } //for
return;
}
main()
{ char names[20][20];
 int count=0;
 count=readAllNames(names);
//cout<<"Count= "<<count<<"\n";	
 sort(names,count);
 printAll(names,count);
 return 0;
} //main
Původní komentáře (3)
Obnoveno z Wayback Machine