Alphabetizing names in a file

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1763 Views - Last Post: 18 June 2012 - 04:50 PM Rate Topic: -----

#1 shakenbake1317  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-June 12

Alphabetizing names in a file

Posted 17 June 2012 - 11:24 AM

"A teacher has asked all her students to line up single file according to their first name. For example, in one class Amy will be at the front of the line and Yolanda will be at the end. Write a program that reads the names from a data file. Names should be read in until there is no more data to read.

Once the names have been read in the program will determine which student would be at the front of the line and which one would be at the end of the line. You may assume that no two students have the same name."

I have written the code for opening and reading the file but I am going around in circles looking for a solution to sort the names and display the first and last students in the line. I'm thinking about Bubble sorting but I don't entirely understand it given I am a beginner. Please help!

Is This A Good Question/Topic? 0
  • +

Replies To: Alphabetizing names in a file

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: Alphabetizing names in a file

Posted 17 June 2012 - 11:38 AM

You did not mention whether you are using C or C++. If you are using C, then you compare the string using strcmp. If you are using C++, you can directly compare them using < and > . The bubble sort will be the same as int, but the only difference will be in method of comparison between the strings.

This post has been edited by aresh: 17 June 2012 - 11:39 AM

Was This Post Helpful? 0
  • +
  • -

#3 mccabec123  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 233
  • Joined: 03-March 11

Re: Alphabetizing names in a file

Posted 17 June 2012 - 11:45 AM

I'm pretty sure you could use the 'atoi()' function to convert a letter to it's ascii value and then use a loop to compare the letter to each count, also as strings in C are arrays of characters, so you just need to point the comparison operator to the first element in the array.

Bubble sorts are when the system, will systematically sift through each element comparing the current element with the previous element and swapping them if one is greater or less than, depending on what the condition is. For example
loop

   if(array[i] > array[i-1])
   {
      tempVar = array[i];
      array[i] = array[i-1];
      array[i-1] = tempvar;
   }
   i++;

end loop



Please bear in mind I tried to make this more pseudocode than C code.

This post has been edited by mccabec123: 17 June 2012 - 11:52 AM

Was This Post Helpful? 0
  • +
  • -

#4 shakenbake1317  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-June 12

Re: Alphabetizing names in a file

Posted 17 June 2012 - 12:08 PM

Im using C++ and I know that you can relate letters as a<b but I feel like this would include the value of the entire word instead of only the letters that matter. Bear in mind that I am a beginner.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,421
  • Joined: 25-December 09

Re: Alphabetizing names in a file

Posted 17 June 2012 - 12:10 PM

Post your current code, and ask specific questions about the code provided. Without the code we are just guessing about what you are trying to accomplish. Show us your best try.


Jim
Was This Post Helpful? 0
  • +
  • -

#6 shakenbake1317  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-June 12

Re: Alphabetizing names in a file

Posted 17 June 2012 - 12:18 PM

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
ifstream inFile;

char name[50];

inFile.open("LineUp.dat");

if(inFile.is_open()){

	while(inFile >> name)

		cout << name << endl;
	
}
else cout << "Error Occurred" << endl;

inFile.close();

return 0;
}




Jim, I don't even know where to start when it comes to the alphabetization part of the project. My teacher isn't offering us much help and this is one of the first projects I've ever written.

This post has been edited by jimblumberg: 17 June 2012 - 12:33 PM

Was This Post Helpful? 0
  • +
  • -

#7 mccabec123  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 233
  • Joined: 03-March 11

Re: Alphabetizing names in a file

Posted 17 June 2012 - 12:26 PM

Have a look at what I posted, that's your answer.
Was This Post Helpful? 0
  • +
  • -

#8 shakenbake1317  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-June 12

Re: Alphabetizing names in a file

Posted 17 June 2012 - 12:32 PM

when using atoi(), does it make letters coming in the beginning of the word have a higher value than letters coming later in the word? Like in 137: the 1 has a higher value than the 3 and the 7?
Was This Post Helpful? 0
  • +
  • -

#9 mccabec123  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 233
  • Joined: 03-March 11

Re: Alphabetizing names in a file

Posted 17 June 2012 - 12:37 PM

View Postshakenbake1317, on 17 June 2012 - 12:32 PM, said:

when using atoi(), does it make letters coming in the beginning of the word have a higher value than letters coming later in the word? Like in 137: the 1 has a higher value than the 3 and the 7?


No, you're missing the point. Because strings in C/C++ are arrays of characters, you can specify the first letter of the string like so 'string[0]', arrays usually start off at 0 not 1. So you can then use this to your advantage and convert that single letter using the 'atoi()' function to gain the ascii value. I suggest you sit down and think about how you're going to do this, I've told you how to do it, but I won't give up the code without you making an attempt at it yourself beforehand, part of learning to program is working things out for yourself, it also allows you to understand how these functions and algorithms work.

Hope this helps.

This post has been edited by mccabec123: 17 June 2012 - 12:40 PM

Was This Post Helpful? -1
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,421
  • Joined: 25-December 09

Re: Alphabetizing names in a file

Posted 17 June 2012 - 12:45 PM

First atoi() is not your answer. You will need to use some of the functions in the cstring header file because you are using C-strings. You compare C-strings with strcmp(). This would be much easier if you were using std::strings instead of the C-strings (nul terminated character arrays) because with std::strings you can use the comparison operator==. Next if you want to compare two or more C-strings you need more than one C-string.

Quote

Names should be read in until there is no more data to read.

Normally you will need an array or preferably a vector of your strings that you want to sort. I strongly recommend the vector if you don't know how many students there will be.



View Postmccabec123, on 17 June 2012 - 02:37 PM, said:

No, you're missing the point. Because strings in C/C++ are arrays of characters, you can specify the first letter of the string like so 'string[0]', arrays usually start off at 0 not 1. So you can then use this to your advantage and convert that single letter using the 'atoi()' function to gain the ascii value. I suggest you sit down and think about how you're going to do this, I've told you how to do it, but I won't give up the code without you making an attempt at it yourself beforehand, part of learning to program is working things out for yourself, it also allows you to understand how these functions and algorithms work.

Hope this helps.


First you can't use atoi() on individual characters, atoi() requires a C-string. Second atoi() only converts numbers, never letters. Third a single char is a number as far as the computer is concerned.

Jim

This post has been edited by jimblumberg: 17 June 2012 - 12:46 PM

Was This Post Helpful? 0
  • +
  • -

#11 mccabec123  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 233
  • Joined: 03-March 11

Re: Alphabetizing names in a file

Posted 17 June 2012 - 12:52 PM

Sorry, I don't think atoi actually displays outputs the ascii value. Try assigning the individual character to an integer. My method will work, I can almost guarantee it if it's implemented correctly. Let me have a go at it and I'll post back my results.

This post has been edited by mccabec123: 17 June 2012 - 12:53 PM

Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,421
  • Joined: 25-December 09

Re: Alphabetizing names in a file

Posted 17 June 2012 - 12:56 PM

Why deal with all of the individual characters when you can check the entire string at once? If you only check the first character how will you know where John and James should be? The best answer for C++ is to use std::string and then you can compare the string using the comparison operator==.

Quote

Let me have a go at it and I'll post back my results.

Please don't this is the OP's assignment not yours. If you post a complete working program the OP will not learn anything.

Jim

This post has been edited by jimblumberg: 17 June 2012 - 12:57 PM

Was This Post Helpful? 0
  • +
  • -

#13 mccabec123  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 233
  • Joined: 03-March 11

Re: Alphabetizing names in a file

Posted 17 June 2012 - 01:19 PM

View Postjimblumberg, on 17 June 2012 - 12:56 PM, said:

Why deal with all of the individual characters when you can check the entire string at once? If you only check the first character how will you know where John and James should be? The best answer for C++ is to use std::string and then you can compare the string using the comparison operator==.

Quote

Let me have a go at it and I'll post back my results.

Please don't this is the OP's assignment not yours. If you post a complete working program the OP will not learn anything.

Jim


I didn't say I was going to post the code :P haha! Just curious, and it does work. But there are a few other functions that need to be used using the string header file. Bearing in mind, this is pure C, no C++ whatsoever. It's a rather beautiful thing :P haha! Thank you for voting me down though Jim, I was incorrect about the atoi, but my algorithm was correct.

This post has been edited by mccabec123: 17 June 2012 - 01:26 PM

Was This Post Helpful? 0
  • +
  • -

#14 shakenbake1317  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-June 12

Re: Alphabetizing names in a file

Posted 18 June 2012 - 03:24 PM

I'm going to honest with you gentlemen, I have no idea where to even start on the alphabetization part of this project. You have all given me good ideas but I haven't even learned enough to implement them. I've read everything I can and been through every similar forum on the web. None of my friends in the same class know where to start either so I feel helpless. Sorry for being a noob...
Was This Post Helpful? 0
  • +
  • -

#15 mccabec123  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 233
  • Joined: 03-March 11

Re: Alphabetizing names in a file

Posted 18 June 2012 - 03:34 PM

Okay, here's the one I prepared earlier in pure C, please bare in mind that this is not C++, although C++ is just an advancement of C anyway it will still work in a C++ compiler.

But I strongly suggest that you examine the code and try to rewrite it without looking at the code once you understand it, otherwise you won't be doing yourself any favours.

Here's the code

//
//  main.c
//  bubbleSort
//
//  Created by Conner McCabe on 17/06/2012.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <stdio.h>
#include <string.h>

int main(int argc, const char * argv[])
{

	int i = 0;
	int asciiValue1;
	int asciiValue2;
	char words[26][20];
	char tempWord[20];
		
	strcpy(words[0], "Anne");
	strcpy(words[1], "Dave");
	strcpy(words[2], "Bob");
	strcpy(words[3], "Clive");
	strcpy(words[4], "Ellen");
	strcpy(words[5], "Girtred");
	strcpy(words[6], "Francis");
	strcpy(words[7], "Harry");
	strcpy(words[8], "Killer");
	strcpy(words[9], "Ingrid");
	strcpy(words[10], "Limber");
	strcpy(words[11], "Nondo");
	strcpy(words[12], "Orthopedo");
	strcpy(words[13], "Queen");
	strcpy(words[14], "Penis");
	strcpy(words[15], "Steve");
	strcpy(words[16], "Rena");
	strcpy(words[17], "Tom");
	strcpy(words[18], "Victor");
	strcpy(words[19], "Umbrella");
	strcpy(words[20], "William");
	strcpy(words[21], "Xavior");
	strcpy(words[22], "Yates");
	strcpy(words[23], "Zebra");
	strcpy(words[24], "James");
	strcpy(words[25], "Mono");
	
	int i2 = sizeof(words)/sizeof(words[0]);
	while(i <= i2)
	{
		asciiValue1 = words[i][0];
		asciiValue2 = words[i-1][0];
		if(words[i][0] < words[i-1][0])
		{
			strcpy(tempWord, words[i-1]); 
			strcpy(words[i-1], words[i]);
			strcpy(words[i], tempWord); 
			i = 0;
		}
		i++;
		
	}
	i = 0;
	while(i<=i2)
	{
		printf("%s\n", words[i]);
		i++;
	}
}


Hope this helps.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2