Attached is my code. I posted this twice on accident earlier, and I didnt attach the code which caused the users discomfort.

My problem is that i keep getting a segmentation fault and I can't understand why. The problem occurs during the decryption part of the program. My other question is how can i make my decryption part shorter and less "for" and "if/else if" statements.

Recommended Answers

All 3 Replies

Attached is my code. I posted this twice on accident earlier, and I didnt attach the code which caused the users discomfort.

My problem is that i keep getting a segmentation fault and I can't understand why. The problem occurs during the decryption part of the program. My other question is how can i make my decryption part shorter and less "for" and "if/else if" statements.

The code is way too long and can be shortened considerably by using well crafted arrays.

int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;
int g = 0;
int h = 0;
int i = 0;
int j = 0;
int k = 0;
int l = 0;
int m = 0;
int n = 0;
int o = 0;
int p = 0;
int q = 0;
int r = 0;
int s = 0;
int t = 0;
int u = 0;
int v = 0;
int w = 0;
int x = 0;
int y = 0;
int z = 0;
int place_a = 0;
int place_b = 0;
int place_c = 0;
int place_d = 0;
int place_e = 0;
int place_f = 0;
int place_g = 0;
int place_h = 0;
int place_i = 0;
int place_j = 0;
int place_k = 0;
int place_l = 0;
int place_m = 0;
int place_n = 0;
int place_o = 0;
int place_p = 0;
int place_q = 0;
int place_r = 0;
int place_s = 0;
int place_t = 0;
int place_u = 0;
int place_v = 0;
int place_w = 0;
int place_x = 0;
int place_y = 0;
int place_z = 0;

Variables like these can almost ALWAYS be made into arrays, like this:

int numOccurenceInWord[26];
int place[26];

for (int i = 0; i < 26; i++)
{
    numOccurenceInWord[i] = 0;
    place[i] = 0;
}

Here's your code:

// gets the frequencies and fills each respective array with the place each letter is found
        for(int it = 0; it < length; it++)
        {
            if(word[it] == 'a')
            {
                a++;       
            }
            else if( word[it] == 'b')
            {
                b++;
            }
            else if( word[it] == 'c')
            {
                c++;
            }
            else if( word[it] == 'd')
            {
                d++;               
            }
            else if( word[it] == 'e')
            {
                e++;
            }
            else if( word[it] == 'f')
            {
                f++;
            }
            else if( word[it] == 'g')
            {
                g++;
            }
            else if( word[it] == 'h')
            {
                h++;
            }
            else if( word[it] == 'i')
            {
                i++;
            }
            else if( word[it] == 'j')
            {
                j++;
            }
            else if( word[it] == 'k')
            {
                k++;
            }
            else if( word[it] == 'l')
            {
                l++; 
            }
            else if( word[it] == 'm')
            {
                m++;
            }
            else if( word[it] == 'n')
            {
                n++;
            }
            else if( word[it] == 'o')
            {
                o++;
            }
            else if( word[it] == 'p')
            {
                p++;
            }
            else if( word[it] == 'q')
            {
                q++;
            }
            else if( word[it] == 'r')
            {
                r++;
            }
            else if( word[it] == 's')
            {
                s++;
            }
            else if( word[it] == 't')
            {
                t++;
            }
            else if( word[it] == 'u')
            {
                u++;
            }
            else if( word[it] == 'v')
            {
                v++;
            }
            else if( word[it] == 'w')
            {
                w++;
            }
            else if( word[it] == 'x')
            {
                x++;
            }
            else if( word[it] == 'y')
            {
                y++;
            }    
            else if( word[it] == 'z')
            {
                z++; 
            }
        }// end for

Here's your code using my arrays:

for(int it = 0; it < length; it++)
        {
            char letter = word[it];
            int letterIndex = letter - 'a';  // convert 'a' to 0, 'b' to 1, etc.
            
            numOccurenceInWord[letterIndex]++;
         }

Much shorter, eh? This comes up time and again in your program. Anything with 26 if statements, one for each letter, can almost assuredly be turned into a single statement using an array index.

My advice is this. Rewrite the program using a 3 or 4 letter alphabet ('a' through 'c' or 'd') in any way you feel comfortable (use if's, else if's, whatever), so it works. Then look at your code and see where you can change the if-statements into index arrays. But reducing the number of letters will cut the lines of code WAY down and make things much faster and easier to work with. Then when you've changed it to get rid of all of the unnecessary if statements, make it 26 letters again.

commented: Sounds like a plan to me +35
commented: Hehe, your code looks a lot better :P +10

Much shorter, eh? This comes up time and again in your program. Anything with 26 if statements, one for each letter, can almost assuredly be turned into a single statement using an array index.

That sounds perfect. so does the rest of your improvements. VernonDozier = GOD. haha. Could you show me how to do this single array you mention in the quote above?

That sounds perfect. so does the rest of your improvements. VernonDozier = GOD. haha. Could you show me how to do this single array you mention in the quote above?

I gave an example in my last post. It's a similar idea everywhere else. Glancing at your code, it looks like there may be some logic problems and possibly else if statements that should be if statements, but I'm not sure. The code is too long to get a real sense of the program flow. I would again reduce your code from a 26 letter alphabet to a 3 or 4 letter alphabet. This will drastically reduce the number of lines of code, which is what you want. Then when the program is a manageable size, change to arrays. It'll be much easier to change to arrays when it's smaller, and easier to spot any problems that CAN'T be changed (you may need other redesigns). But it's simply too big to work with now until you reduce the size. As an added bonus, once you reduce the size of the program, you can post it instead of attach it, so more people will look at it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.