10-15-2013, 03:39 PM
|
#18
|
Digital Dancing!
Join Date: Feb 2006
Location: 80 billion club, NE
Age: 31
Posts: 12,980
|
Re: Requesting help for programming/ CS majors?
I went ahead and did steps 1 and 2 in java. you should be able to figure out how to count the letter frequency and make the histogram. Here's my java code:
Code:
import java.util.Scanner;
public class namesort
{
public static void main(String args[])
{
char[] name = new char[50];
int[] asciiname = new int[50];
int count = 0;
System.out.println("Enter the letters of your name one by one and type * when done.");
Scanner input = new Scanner(System.in);
while(count < 50)
{
char dumbchar = input.next().charAt(0);
if(dumbchar == '*')
break;
else
{
name[count] = dumbchar;
count++;
}
}
//cast char to int(ASCII)
for(int i = 0; i < name.length; i++)
{
asciiname[i] = (int)name[i];
}
//bubble sort
for(int i = 0; i< asciiname.length-1; i++)
{
for(int j = 0; j<asciiname.length-1; j++)
{
if(asciiname[j+1] < asciiname[j])
{
//swap elements
int dummy = asciiname[j];
asciiname[j] = asciiname[j+1];
asciiname[j+1] = dummy;
}
}
}
//cast int(ASCII) to char(ANSI).
for(int i = 0; i < asciiname.length; i++)
{
name[i] = (char)asciiname[i];
}
for(int i=0; i<name.length; i++)
{
System.out.print(name[i]);
}
}
}
|
|
|