run time error

#include
#include

//Write a function that is able to return the index
//of the inputed char

int getIndex(char currentChar)
{
int currentIndex;
char alpha[26] =
{'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'};
for(currentIndex = 0; currentIndex < 26; currentIndex++)
{
if (alpha[currentIndex] == currentChar)
return currentIndex;
}
}

float IndexOfCoincidance()
{
char CipherText[238] = "NGTSAIPNGEPBSFWNCPBNRASGFASGEWJSVSFNRCNIWKGPWKIGEWPSWWMSYEWNNFUNGEPBSFWNCPBNRSWSPWSWRGMNRSUSVGHVSBRGMNRSUKCPTSSDNSPBSBMRCNNRSIFHIYINWCFSNGMRIKRGPSWMSRCVSCNNCKRSBNCTGGWCPBMRCNNRSUKCPNSCKREWCTGENNRSFCVIWRIPAMGFHBMSRCVSNRSXFIVIHSASNGIPRCTIN
";
//237 = n in this case
int n = 237; //number of characters in string
int bottom = n * (n-1); //bottom of division equation
int i; //index for rolling through ciphertext
int a, b; //current index and current index - 1 respectively
for(i = 0; i < 238; i++)
{
a = getIndex(CipherText[i]);
b += (a * (a-1));
}
float c; //Index of Coincidace
c = (1 / bottom) * b;
return c;
}

void main()
{
float a = IndexOfCoincidance();
cout << a;
//n = 237; //Number of letters in CipherText

}

Is my code....it returns a value of zero, which it shouldn't. Why does it return 0, I can't find the logic that is causing it to do so, it should return some very low value under 1 like 0.0032 or 0.0586, the value outputted should not be larger then 0.065

If anyone can help me with this I will love you forever!
Anthony

Comments

  • Hi!!

    Declare bottom as float or typecast it as float in the expression (1/bottom) secondly initialise b

    Regards
    Shaunak
    : #include
    : #include
    :
    : //Write a function that is able to return the index
    : //of the inputed char
    :
    : int getIndex(char currentChar)
    : {
    : int currentIndex;
    : char alpha[26] =
    : {'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'};
    : for(currentIndex = 0; currentIndex < 26; currentIndex++)
    : {
    : if (alpha[currentIndex] == currentChar)
    : return currentIndex;
    : }
    : }
    :
    : float IndexOfCoincidance()
    : {
    : char CipherText[238] = "NGTSAIPNGEPBSFWNCPBNRASGFASGEWJSVSFNRCNIWKGPWKIGEWPSWWMSYEWNNFUNGEPBSFWNCPBNRSWSPWSWRGMNRSUSVGHVSBRGMNRSUKCPTSSDNSPBSBMRCNNRSIFHIYINWCFSNGMRIKRGPSWMSRCVSCNNCKRSBNCTGGWCPBMRCNNRSUKCPNSCKREWCTGENNRSFCVIWRIPAMGFHBMSRCVSNRSXFIVIHSASNGIPRCTIN
    : ";
    : //237 = n in this case
    : int n = 237; //number of characters in string
    : int bottom = n * (n-1); //bottom of division equation
    : int i; //index for rolling through ciphertext
    : int a, b; //current index and current index - 1 respectively
    : for(i = 0; i < 238; i++)
    : {
    : a = getIndex(CipherText[i]);
    : b += (a * (a-1));
    : }
    : float c; //Index of Coincidace
    : c = (1 / bottom) * b;
    : return c;
    : }
    :
    : void main()
    : {
    : float a = IndexOfCoincidance();
    : cout << a;
    : //n = 237; //Number of letters in CipherText
    :
    : }
    :
    : Is my code....it returns a value of zero, which it shouldn't. Why does it return 0, I can't find the logic that is causing it to do so, it should return some very low value under 1 like 0.0032 or 0.0586, the value outputted should not be larger then 0.065
    :
    : If anyone can help me with this I will love you forever!
    : Anthony
    :

  • : Hi!!
    :
    : Declare bottom as float or typecast it as float in the expression (1/bottom) secondly initialise b
    :
    : Regards
    : Shaunak
    : : #include
    : : #include
    : :
    : : //Write a function that is able to return the index
    : : //of the inputed char
    : :
    : : int getIndex(char currentChar)
    : : {
    : : int currentIndex;
    : : char alpha[26] =
    : : {'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'};
    : : for(currentIndex = 0; currentIndex < 26; currentIndex++)
    : : {
    : : if (alpha[currentIndex] == currentChar)
    : : return currentIndex;
    : : }
    : : }
    [blue]This is an easier and quicker way to accomplish this function[/blue]
    [code]
    int getIndex(char currentChar)
    {
    int index = 0;
    if(currentChar >= 'A' && currentChar <= 'Z')
    index = currentChar - 'A';
    return index;
    }

    int main()
    {
    int index = getIndex('C');

    return 0;

    }

    [/code]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion