incorrect incrementing of array elements

Good Morning:
I am new to programming and I have been working on an assignment for two days now. I have written the code for a loop, but when it runs, it does not increment. Here are the instructions for this function:

Write a function named getCharacterFrequency. This function is passed a null terminated string as its first parameter. The second parameter is an uninitialized array of size 26 that will hold the function result.
The declaration looks like this:
void getCharacterFrequency (char inputString [], unsigned int frequency[]);

The function should first set all values in the frequency array to 0. The function should then use a loop to examine each character in the null terminated string. For each alphabetical character a through z or A through Z, the function should increment the corresponding location in the array. For example, if the input string is Hello, the frequency array will be as follows:
0 0 0 0 1 0 0 1 0 0 0 2 0 0 1 0 0 0 0 0 0 0 0 0 0 0
The first parameter is used to pass data in to the function so the first parameter is an IN parameter. The second parameter is used to return the function results so it is an OUT parameter. The result should be returned. Do not display the result. /// I fixed the runaway problem but now the function increments the array elements incorrectly.

Here is my new code:
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
void getCharacterFrequency (char inputString[], unsigned int frequency[])
{
  char ch;
  int index1 = 0;
  int index2 = 0;
  const int SIZE = 26;
     
  while (index1 < SIZE)   
  {
	  frequency[index1] = 0;  // This is to initilize the frequency array values to zero
	  index1++;
  }
  
  ch = inputString[index2];
  while (ch != '\0')
	{
		if (ch >= 65 && ch <= 90)
		{
			frequency[ch - 65]++;
		}
	    else if (ch >= 95 && ch <= 122)
		{
		    frequency[ch - 95]++;
		}
		ch = inputString[++index2];
    }
  
}

This is what I started with:
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
//This program will test the getCharacterFrequency function

#include <iostream>
using namespace std;
void getCharacterFrequency (char inputString[], unsigned int frequency[]);

void main()
{
	char inputString[] = {"Larry had A biG Dog"};
 unsigned int frequency[26];

 getCharacterFrequency (inputString, frequency);
 cout << frequency << endl;

}

//This function will determine how many times a letter appears in a string

#include <iostream>
using namespace std;

void getCharacterFrequency (char inputString[], unsigned int frequency[])
{
  char ch;
  int index = 0;
  const int SIZE = 26;
     
  if (index < SIZE)   
  {
	  frequency[index] = 0;
	  cout <<"In frequency" << endl;
  }index++;
  
  ch = inputString[0];
  while (ch != '\0')
	{
		if (ch >= 65 && ch <= 90)
		{
			cout <<"In loop1" << endl;
			frequency[ch - 65]++;
			cout <<frequency[0]<< endl;
			index++;
		}
	    if (ch <= 95 && ch <= 122)
		{
		    cout <<"In loop 2" << endl;
			frequency[ch - 95]++;
			index++;
		}
		
    }
}


Can anyone tell me what I did wrong? Please.
Last edited on
This code contains a single loop. A while loop. Your while loop examines the value of ch to decide when to stop.

ch is set to the value inputString[0]; before the loop begins.

ch never, ever changes. You have no code that ever changes ch again, so whatever it is at the start is what it will stay forever.

You are incrementing the value index, which is not used anywhere. It's just some number that you're incrementing. If you want ch to be different each time the loop goes round, you're going to have to do something to change the value of ch.
Thank you.
I made the following changes:


char ch;
int index1 = 0;
int index2 = 0;
const int SIZE = 26;

while (index1 < SIZE)
{
frequency[index1] = 0; // This is to initilize the frequency array values to zero
cout <<"In frequency" << endl;
index1++;
}

ch = inputString[index2];
while (ch != '\0')
{
if (ch >= 65 && ch <= 90)
{
cout <<"In loop1" << endl;
frequency[ch - 65]++;
index2++;
}
if (ch <= 95 && ch <= 122)
{
cout <<"In loop 2" << endl;
frequency[ch - 95]++;
index2++;
}

}
}
The frequency array initializes now, but when the second loop starts it becomes a runaway. Any suggestions?
Yes. Exactly the same as I said above.

Your second while loop will end when ch is '\0'. When will that happen?

When in your second while loop do you change the value of ch? Never.
So when will the value of ch become '\0'? Never.
So when will the loop end? Never.
I am not sure I understand. I added (ch = inputstring[index2]) doesn't this increment ch ?
In your code, ch = inputString[index2]; is before the while loop.

Everything inside the while loop gets executed during the loop. So ch = inputString[index2]; happens only once, before the while loop. Changing the value of index2 after this makes no difference. A value of ch has been set and if you want to change it you must set it again.

Let's simplify it. Do you understand why in this simplified while loop, the value of ch does not change?

1
2
3
4
5
ch = inputString[index2]; // happens ONCE
while (ch != '\0')
{
  index2++; //happens many, many times
}

Just got home. I made the change you recommended along with another.
The programs runs now but it doesn't increment the correct array positions.
Here is the corrected code:
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
void getCharacterFrequency (char inputString[], unsigned int frequency[])
{
  char ch;
  int index1 = 0;
  int index2 = 0;
  const int SIZE = 26;
     
  while (index1 < SIZE)   
  {
	  frequency[index1] = 0;  // This is to initilize the frequency array values to zero
	  index1++;
  }
  
  ch = inputString[index2];
  while (ch != '\0')
	{
		if (ch >= 65 && ch <= 90)
		{
			frequency[ch - 65]++;
		}
	    else if (ch >= 95 && ch <= 122)
		{
		    frequency[ch - 95]++;
		}
		ch = inputString[++index2];
    }
  
}
Topic archived. No new replies allowed.