Hello,

I have two arrays that contain different numbers that I need to compare with another array.. e.g.

Array 1:

10
20
40
70

Array 2:

5
3
10
18
2

Basically, I want to write a program that calculates the highest value each array and then swops the values..

So like: 70 is the highest value in array 1, and 18 is the highest value of array 2 so these have to be swopped.. Here's the code:

int max = 0; int max2 = 0;
for(int i=0; (i < size); i++)
    {
        max = counter[0];

        if(counter[i] >= max)
        {
            max = counter[i];
        }
        
        for(int j=0; (j < size); j++)
        {
            max2 = counter[0];
            if(counter[j] >= max)
            {
                max2 = counter[j];
            }
        }
    }

Any suggestions?

Recommended Answers

All 11 Replies

You're overcomplicating things. Just find the index of the largest value in each array (that's two independent loops), then swap them:

int index_of_max(int a[], int size)
{
    int max = 0;

    for (int i = 1; i < size; i++) {
        if (a[i] > a[max])
            max = i;
    }

    return max;
}

...

int max1 = index_of_max(array1, size1);
int max2 = index_of_max(array2, size2);

std::swap(array1[max1], array2[max2]);

Heyy thanks for the reply..

It gives me 13? Which isn't the highest value

Heyy thanks for the reply..

It gives me 13? Which isn't the highest value

While I'm confident your code is wrong, I can't help without seeing it.

Heyy

int analysis[27] = {8, 2, 3, 4, 13, 2, 2, 6, 7, 0, 1, 4, 2, 7, 8, 2, 0, 6, 6, 9, 3, 1, 2,0,2,0};
int size = sizeof(analysis)/sizeof(analysis[0]);
int main()
{
      int max2 = index_of_max(analysis, size);
      cout << max2 << endl;
     return EXIT_SUCCESS;
}

int index_of_max(int a[], int size)
{
    int max = 0;
    
    for (int i = 1; i < size; i++) {
        if (a[i] > a[max])
            max = i;
    }
    
    return max;
}

Thanks

That program prints 4, which is the index of 13, which is the largest number in the array. What's the problem?

Would you forgive me if I told you I was an idiot? Sorry aha!

Once I've swopped the value, do I need to remove it from the array and find the next highest value? Like.. It needs to do this 26 types (to go through each number) so for example:


Array 1:

10
20
40
70 -> found and swopped

Array 2:

5
3
10
18 -> found and swopped
2

and then it would do:

Array 1:

10
20
40 -> found and swopped

Array 2:

5
3
10 -> founded and swopped
2

Until all the elements are found?

Thanks so much!

Like.. It needs to do this 26 types (to go through each number)

*sigh* I have an idea. Instead of you asking questions then invalidating the answer with new requirements, just tell us flat out everything that your program needs to do.

I private messaged you aha! Thanks by the way!

It would have been much better if you said that you're doing frequency analysis on cipher text. Then I could simply say that while frequency analysis is a good idea, your approach is too naive to produce decent results.

Can you do this analysis manually? If so, what's the process you're using? Working toward an algorithm from an already functional set of manual steps is the right approach.

Hey,

I have done this manually and produced the decrypted text. Basically I used (for example):

The frequency of A = 10 (cipher text) and the frequency of E = 7 (alphabet)
But, the problem is that some of the alphabet occurs twice.. So like

C = 2
D = 2
F = 2

So it's hard to get an exact result..

So it's hard to get an exact result..

Exactly. Not to mention that the most frequent cipher text letter isn't guaranteed to be an encrypted form of the most frequent alphabet letter. For example, take "thisisatest" as the plain text. Given a substitution cipher, 't' would be the most frequent letter even though 'e' is the most frequent letter in the alphabet. The general frequencies of the alphabet would only be reasonably accurate for large quantities of cipher text. Otherwise the usual frequencies are no longer statistically significant.

Frequency analysis takes at least a little bit of correctness checking by a human, otherwise you'll end up with gibberish most of the time. Alternatively you can write a fuzzy dictionary comparison and use a confidence check, but that's probably beyond your project's requirements.

How about performing multiple runs on the letters with matching frequency? Starting with the most frequent letters, you can give the user a yes/no choice on whether the result looks promising, and either continue or try again accordingly.

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.