puzzle

Hi There

I have question, while I was going trough one of the exercises assigned by my tutor I came across problem and solution (thank you google).
Majority of the code is self explanatory but there are few parts that I don't understand.
Exercise was to create an array that will hold in char alphabet.

parts that is unclear for me are:

1. I understand that string need to be finished with \0 but why assign this character at the start?
char letter = '\0';

2. in first loop, there are two items. a) int val is set to 1 in for loop parameters and following line where char letter is asinged with val-1

for ( val = 1; val < 26; val++)
{
letter = alphabet[val-1];

I have tried to use in what I tough would correct but this does not provide correct output.

for ( val = 0; val < 27; val++)
{
letter = alphabet[val];

with such changes I am getting characters that are not correct (not sure even what are they as i can't find most of on ascii code table.


if you could walk me trough the first for loop it would be fab :)


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
 /*
Exercise 1.

Write a program that prints out in the console the whole alphabet 'abcdefghijklmnopqrstuvwxyz'.
All of the characters from the alphabet should be stored in a 27 element array of chars and
printing out of each of those elements should be done with the use of the for loop&

eg.
Output:
abcdefghijklmnopqrstuvwxyz
*/
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
    char alphabet[27];
    char letter = '\0';
    int val=0;

    alphabet[0]='a';
    for ( val = 1; val < 26; val++)
    {
        letter = alphabet[val-1];
        letter++;
        alphabet[val] = letter;
    }
    for (val = 0; val < 26; val++)
    {
        cout << alphabet[val];

    }

    return 0;
}
Last edited on
Exercise was to create an array that will hold in char alphabet.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Exercise 1.
// Write a program that prints out in the console the whole
// alphabet 'abcdefghijklmnopqrstuvwxyz'.
// All of the characters from the alphabet should be stored in a 27 element
// array of chars and printing out of each of those elements should
// be done with the use of the for loop&
// eg. output:
// abcdefghijklmnopqrstuvwxyz

#include <iostream>

using namespace std;

int main()
{
   char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
   for (int val = 0; val < 27; val++)
   {
       cout << alphabet[val];
   }
   cout << endl;

   return 0;
}

1. I understand that string need to be finished with \0 but why assign this character at the start?
char letter = '\0';

It’s common not to left any variable uninitialized, even if C++ introduces a better strategy, i.e. declaring the variable just before using it (instead of at the beginning of the function).

2. [...]
I have tried to use in what I tough would correct but this does not provide correct output.
1
2
3
for ( val = 0; val < 27; val++)
{
letter = alphabet[val];

[...] I am getting characters that are not correct [...]

You were going in the right direction, but you needed to initialize the array of char. You were presented with the values that were in the block of memory pointed to by the uninitialized array ;-)

should be done with the use of the for loop&

Sorry, is the ampersand at the end of your phrase a typo? Or do your teacher really want you to use references with chars?
Hi Enoizat

yes & at the end of explanation is typo, I think it was somehow copied as its not in my code in IDE. I am sorry about that.

1. char letter = '\0'; so what you mean that its better to have it with value then as char letter; ?
but would it then be better to use for example lower case a? char letter = 'a'; ???

2. ok, so like in code you have? char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
but then is this not initialization of char array? alphabet[0]='a';

what is puzzling for me why does it have to be -1 in this line letter = alphabet[val-1];
and for loop needs to start from 1 not 0. for ( val = 1; val < 26; val++)

Once again thank you for your help :)
Last edited on
You welcome, xxvms.

its better to have it with value then as char letter; ?

A lot of people say it's advisable to initialize every variable. But the fact is if you declare them immediately before using them, you'll be more unlikely to meet some problem. General rules have often exceptions, so I'm not dogmatic on that (personal point of view).

but then is this not initialization of char array? alphabet[0]='a';

Well, that instruction initializes the 0-position of the array...

what is puzzling for me why does it have to be -1 in this line letter = alphabet[val-1];
and for loop needs to start from 1 not 0. for ( val = 1; val < 26; val++)


Ok, maybe I've been a bit unfair. Your code was fine, it only needed a little tweaking:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
   char alphabet[27];
   alphabet[0]='a';
   char letter = alphabet[0];

   for (int val = 1; val < 26; val++)
   {
      letter++;
      alphabet[val] = letter;
   }
   for (int val = 0; val < 26; val++)
      std::cout << alphabet[val];

   std::cout << std::endl;

   return 0;
}


I'm sorry I have changed it in that major way, but I thought it was simpler...

I like to think that by making mistakes we are creating opportunities to learn.

Enoizat, great advise thank you :)
Topic archived. No new replies allowed.