Total members 9951 | Gratitudes |It is currently Sat Feb 11, 2012 10:29 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 3 posts ] 
Author Question
 Question subject: Help with selection sort for strings?
PostPosted: Wed Mar 04, 2009 7:17 pm 
Offline
Newbie
User avatar

Joined: Wed Mar 04, 2009 7:13 pm
Posts: 1
Has thanked: 0 time
Have thanks: 0 time

Ok so 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’m losing my mind to my computer.

[code]
#include<stdio.h>

# define LEN 20
# define NUM 10

int readFromFile(char text[NUM][LEN]) {[size=85][/size]

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;
}
[/code]


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Help with selection sort for strings?
PostPosted: Thu Mar 05, 2009 10:08 pm 
Offline
Moderator
User avatar

Joined: Fri Nov 21, 2008 6:18 pm
Posts: 51
Location: thessaloniki
Has thanked: 0 time
Have thanks: 2 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;
}
 

_________________
if you want make an effort yourself no one will make it for you...
best regards


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Help with selection sort for strings?
PostPosted: Thu Mar 05, 2009 10:12 pm 
Offline
Moderator
User avatar

Joined: Fri Nov 21, 2008 6:18 pm
Posts: 51
Location: thessaloniki
Has thanked: 0 time
Have thanks: 2 time
http://www.cs.ubc.ca/spider/harrison/Ja ... -demo.html if it is not you can read a bit about your project there

_________________
if you want make an effort yourself no one will make it for you...
best regards


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 3 posts ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 Library Sort  C++ examples  mileloader  1
 validate age entered as selection box in javascript  JavaScript examples  msi_333  0
 remove space ,trim strings from left side ,right side ,both.  ASP/ASP.net examples  msi_333  0
 fill selection options from list  ASP/ASP.net examples  msi_333  0
 redirect page based on user selection radio button  ASP/ASP.net examples  msi_333  0

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 2 guests



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team