Help ARRAYS

I have an assignment due that has been racking my brain for a week and I just can't get all the pieces to fall together. Any help would be appreciated.

Write a program that converts the alphabetic characters in a text file into the corresponding words in the International Civil Aviation Organization (ICAO) alphabet. Your program should read the ICAO alphabet from the file called icaoAlphabet.txt. The file contains two lines of text (shown below):

HotelDeltaNovemberUniformOscarAlphaLimaWhiskeyEchoQuebecZuluSierraBravo
JulietMikePapaYankeeCharlieFoxtrotXrayRomeoGolfKiloVictorIndiaTango

The beginning of each word in the ICAO alphabet is indicated by an uppercase letter. For example, the first word in the file is Hotel, the second is Delta, the third is November, etc. Extract each word from the file and insert it into an array corresponding to its position in the ICAO alphabet. For example, the word Hotel is the eighth word in ICAO alphabet, Delta is the fourth, November is the fourteenth, etc. After the words in the ICAO alphabet are loaded into the array, it should contain the words in the order shown below (array index values are shown):

[0]
[1] Alpha
[2] Bravo
[3] Charlie
.
.
.
[24] Xray
[25] Yankee
[26] Zulu

The array can now be used to convert the alphabetic characters in a text file into the corresponding words in the ICAO alphabet. Convert the text in the file called originalText that can be found in the text file called datafiles.txt Write the converted text to a file. For example, the first few words of your output file should appear as shown below:

WhiskeyRomeoIndiaTangoEcho Alpha PapaRomeoOscarGolfRomeoAlphaMike …
What do you have so far?
not a whole lot, just a few example on arrays like
#include <iostream>

using namespace std;

int main() {
int scores[5];
int total = 0;

cout << "Enter 5 scores to compute an average."
<< "One at a time when prompted: " << endl;

for(int i = 0; i <= 4; i++)
{
cout << "Enter score[" << i << "]: ";
cin >> scores[i];
cout << endl;
}

for (i = 0; i <= 4; i++)
total += scores[i];

cout << "The average was: " << total / 5.0 << endl;

return 0;
}
bump
The code you posted has nothing to do with the porblem at hand. You didn't even try to rename any of the variables this is just a copy paste from a previous project.

Did you actually start this one yet or are we taking it from the top?
Taking it from the top, the main problem is I am not sure how to start the process.
Ok, step one you will need to include a certain library. Do you know what lib that is?

Next let's see you Open the file indicated and verify that it is good, if not then print an error indicating so to the screen.
Have they been teaching you this? They only need one for loop, and no array is needed.

Faster and better:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main()
{
    double score = 0;
    double average = 0;
    double incrementScale = 1 / 5.0;

    std::cout << "Enter 5 scores to compute an average.\n One at a time when prompted:\n";

    for (int index = 0; index != 5; ++index)
    {
        std::cout << "Enter score [" << index+1 << "]: ";
        std::cin >> score;
        average += incrementScale * score;
        std::cout << std::endl;
    }

    std::cout << "The average was: " << average << std::endl;

    return 0;
}


Just getting into this kind of optimization habit is good.
Last edited on
computergeek01, I am comfortable writing code, Im trying to put the text file into an array so that I can print the array in alphabetic order and then write them in a sentence per my original post. That is where I am confused, putting the text file into the array so it prints as
[0]
[1] Alpha
[2] Bravo
[3] Charlie
.
.
.
[24] Xray
[25] Yankee
[26] Zulu

based on the text file.
Ah I see. That's because it's "two" seperate steps. First you read the data into the array then you sort it alphabetically.

You can address the first letter of each word as a char in an array. You can actually compare them as if they were integers to, this will tell you if 'A' is larger the 'B' for example.

You'll need two loops to compare each element of the array against everyother one. There may be more efficent ways of doing this but I can't think of them right now.
that makes a lot of sense. So, now getting the text file into a 1d array? I should be able to sort them after that.
First you need to make an array of the alphabet (with capital letters). Then you will need to parse the text file for each capital letter. Stop loading the current capital letter when you find another capital letter, and then add everything currently loaded to the corresponding index of that letter. Then you can use that array to parse input into output.
Last edited on
Sounds like you have the right idea. I'll keep an eye on this thread if you have any more trouble.

Psuedo-Code
1
2
for(int i = 0; i < ArrayMaxSize; i++)
{InputFile >> FileData[i];}


That is if you haven't used Vectors yet. If you have then they are better suited for this type of thing.
I have been working for a few hours now and am still stuck. Any hints?
I have an assignment due that has been racking my brain for a week and I just can't get all the pieces to fall together. Any help would be appreciated.

Write a program that converts the alphabetic characters in a text file into the corresponding words in the International Civil Aviation Organization (ICAO) alphabet. Your program should read the ICAO alphabet from the file called icaoAlphabet.txt. The file contains two lines of text (shown below):

HotelDeltaNovemberUniformOscarAlphaLimaWhiskeyEchoQuebecZuluSierraBravo
JulietMikePapaYankeeCharlieFoxtrotXrayRomeoGolfKiloVictorIndiaTango

The beginning of each word in the ICAO alphabet is indicated by an uppercase letter. For example, the first word in the file is Hotel, the second is Delta, the third is November, etc. Extract each word from the file and insert it into an array corresponding to its position in the ICAO alphabet. For example, the word Hotel is the eighth word in ICAO alphabet, Delta is the fourth, November is the fourteenth, etc. After the words in the ICAO alphabet are loaded into the array, it should contain the words in the order shown below (array index values are shown):

[0]
[1] Alpha
[2] Bravo
[3] Charlie
.
.
.
[24] Xray
[25] Yankee
[26] Zulu

The array can now be used to convert the alphabetic characters in a text file into the corresponding words in the ICAO alphabet. Convert the text in the file called originalText that can be found in the text file called datafiles.txt Write the converted text to a file. For example, the first few words of your output file should appear as shown below:

WhiskeyRomeoIndiaTangoEcho Alpha PapaRomeoOscarGolfRomeoAlphaMike …
here is what I have so far. I need to separate the text file based on the capital letters and put them into the array individually.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main ()
{
     const int length= 27;
	 string  alpha[length];      
	 int  index;
	 ifstream inData;
	 ofstream outData;

	 inData.open("icaoAlphabet.txt");
	 outData.open("outFile.txt");
        


        for (index = 1; index < length; index++)
	{
		getline(inData,alpha[index]);
	}
       
     
       cout << "array alpha is " << endl;
       for (index = 1; index < length; index++)
       {
			cout << alpha[index] << endl;
		   outData << alpha[index] << endl;
       }
	inData.close();
	outData.close();


   return 0;

} 
That won't work at all. Here, let me give this a try.
This took 2 hours:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <string>
#include <fstream>

int main()
{
	char alphabet[27] = {
		'A',
		'B',
		'C',
		'D',
		'E',
		'F',
		'G',
		'H',
		'I',
		'J',
		'K',
		'L',
		'M',
		'N',
		'O',
		'P',
		'Q',
		'R',
		'S',
		'T',
		'U',
		'V',
		'W',
		'X',
		'Y',
		'Z'};

	std::string wordAlphabet[27];

	// Open the file.
	std::ifstream iFile;
	iFile.open("icaoAlphabet.txt");

	// Parse the file.
	char currentLetter = 0;

	unsigned short segmentIndex = 0;
	char segmentHead = 0;
	std::string segmentLoad;

	while(iFile.good())
	{
		// Get the current letter.
		currentLetter = iFile.get();
		if (currentLetter == '\n') continue;

		// Segment handling:
		for (unsigned short index = 0; index != 27; ++index)
		{
			if (alphabet[index] == currentLetter) // Detect segments.
			{
				// Deal with the old segment.
				if (segmentHead)
				{
					// Load the segment into array:
					wordAlphabet[segmentIndex] = segmentLoad;
					segmentLoad.clear();
				}

				// Start the new segment.
				segmentIndex = index;
				segmentHead = currentLetter;

				break;
			}
		}

		if (!segmentHead) return -1; // Error, file is bad.

		// Segment loading.
		segmentLoad += currentLetter;
	}

	std::cout << "Enter some capital letters to convert into words: ";

	std::string input;
	std::cin >> input;

	std::cout << std::endl;

	unsigned int dataLength = input.length();
	char *data = new char [dataLength + 1];
	data = (char*)input.c_str();

	// Now we can try converting the letters into words:
	for (unsigned int dataIndex = 0; dataIndex != dataLength; ++dataIndex)
	{
		for (unsigned short alphabetIndex = 0; alphabetIndex != 27; ++alphabetIndex)
		{
			if (alphabet[alphabetIndex] == data[dataIndex])
			{
				std::cout << wordAlphabet[alphabetIndex] << std::endl;
			}
		}
	}

	// And that's it!
	std::cout << "\nProgram finished, press 'enter' to continue . . .\n";
	std::cin.get();

	return 0;
}
@benjelly.. You don't need to define the alphabet, let alone with an extra character =)
(edit) whoops you're using c-strings forgot about the null termination, my bad =)
//(The modern English alphabet is a Latin-based alphabet consisting of 26 letters) <- thanks wiki

you can use the ascii values.

'a' = 97;
'z' = 122;

'A' = 65;
'Z' = 90;

you can also use isupper rather than making a function. ;)

http://www.cplusplus.com/reference/clibrary/cctype/isupper/
Last edited on
The null termination was not needed actually. I just forgot there were 26, and for some reason my brain told me 27. And thanks for reminding me about ASCII.
Topic archived. No new replies allowed.