Total members 11893 |It is currently Tue Nov 05, 2024 10:05 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





I have to turn in this C program today that is able to selectively sort a list of names from an input file, but when I compile it and run it, it doesn't sort it correctly. Can anyone please help me here? I am losing my mind to my computer.

cpp code
#include<stdio.h>

# define LEN 20
# define NUM 10

int readFromFile(char text[NUM][LEN]) {

FILE *fp;
int i;


if ((fp = fopen("input.txt", "r")) == NULL) {
printf("Error opening file");
return -1;
}


for (i = 0; i < NUM; i++) {
fscanf(fp, "%s..n", text[i]);
}


fclose(fp);
return 0;
}


void selectionSort(char text[NUM][LEN]) {
int i, j;
int min;
for(i = 0; i < NUM -1; i++)
{
min = i;
for(j = i + 1; j < NUM; j++)
{
CompareStrings(text[i], text[i+1]);
if(CompareStrings(text[i], text[i+1]) == -1);
{min = j;}
}
SwapStrings(text[i], text[i+1]);
}
return;
}

int CompareStrings(char s1[10], char s2[10])
{
int i;
for(i = 0; i < 10; i++)
{
if (s1[i] > s2[i])
{ return (1);}
if (s1[i] < s2[i])
{return (-1);}
if (s1[i] == 0 || s2[i] == 0)
{break;}
}

return 0;
}

void SwapStrings(char s1[10], char s2[10])
{

int i;
char c;

for(i = 0; i <10; i++)
{ c = s1[i];
s1[i] = s2[i];
s2[i] = c;
}
}

void SwapStrings(char s1[10], char s2[10]);
int CompareStrings(char s1[10], char s2[10]);
int readFromFile(char text[NUM][LEN]);
void selectionSort(char text[NUM][LEN]);

int main() {
char input[NUM][LEN];
int i;
if(readFromFile(input) == -1) {
printf("Error reading from file..n");
return -1;
}

readFromFile(input);
printf("UNSORTED ARRAY: ..n");
for(i = 0; i<10; i++)
{printf("%s", input[i]);
printf("..n");}

selectionSort(input);

printf("..nSORTED ARRAY: ..n");
for(i = 0; i<10; i++)
{printf("%s", input[i]);
printf("..n");}

return 0;
}





Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

: well if i understand what you want to do correctly have a look at this example i think it will help you solve your problem ...
Code:
#include<string.h>
int main()
{
  int i,n,j;
  int bSwapped=1;
  char *names[100]; // your actual Array Declaration
  char* pstr;
  char temp[20];   // temperory Char Array to store the Readed Sring
  printf("Enter total no Of Students");
  scanf ("%d",&n);
  for(i=0;i<n;i++)
   {
     printf("Enter the %d StudentName : ",(i+1) );
     scanf("%s",temp);
     name[i] = strdup(temp);
   }
  while (bSwapped)
  {
   bSwapped=0;
   for(i=0;i<n-1;i++)
    for(j=i+1;j<n;j++)
      if(strcmp(name[i],name[j])>0)
        {
          pstr=name[i];
          name[i]=name[j];
          name[j]=pstr;
          bSwapped=1;
        }
  }
   // Print the Sorted List of Student names
  for(i=0;i<n;i++)
   printf("\n %s",name[i]);
  return 0;
}
 



Author:
Moderator
User avatar Posts: 47
Have thanks: 1 time

Try :
c-c/selection-sort-t27.html
c-examples/c-selection-sort-t2916.html


Author:
Moderator
User avatar Posts: 47
Have thanks: 1 time
Post new topic Reply to topic  [ 3 posts ] 

  Related Posts  to : Help with selection sort for strings?
 Sort strings java-Sorting Array of Strings     -  
 C++ Selection Sort     -  
 selection sort     -  
 Good looking selection box with image per option     -  
 selection of checkbox in buttongroup in netbeans     -  
 calculate data from multiple selection     -  
 fill selection options from list     -  
 validate age entered as selection box in javascript     -  
 C++ Strings     -  
 PHP Strings     -  



Topic Tags

C++ Sorting
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com