Jump to content

A program to compare these words and find the one with the longest length, and the one with the largest value

alphabeta
Go to solution Solved by Mojo-Jojo,

 

I fixed those 2 now it's doesn't take the longest as the greatest but still the greatest value ain't right

You're subtracting '0' which is not equal to 96. 

 

Here's my code which I tested working. I edited the varnames for more clarity too.

#include<iostream>#include<string>using namespace std;int main(){    int iWordCount,iCurrentValue,iHighestValue = 0;    string highestValueString,longestString,currentString;    cin>>iWordCount;    for(int i=0; i<iWordCount; i++)    {        cin>>currentString;        iCurrentValue = 0;        for(int j=0;j<currentString.size();j++)        {            iCurrentValue += currentString[j] - '`';        }        if(iCurrentValue>iHighestValue)        {            highestValueString.assign(currentString);            iHighestValue = iCurrentValue;        }        if(currentString.size()>longestString.size())        {            longestString.assign(currentString);        }    }    cout<<highestValueString<<endl<<longestString;    return 0;}

Edit: Subtracting 'a' returns a value too small by 1. Subtract '`' instead. 

post-93043-0-95746400-1422201507.png

You are given N words, write a program to compare these words and find the one with the longest length, and the one with the largest value.

A word's value is computed by summing up the values of its characters. i.e A=1, B=2..Z=26.

If two words have the same length or two words have the same value, consider the word that comes later in input.

 

it just output the longest string as the longest and the greatest value x1,x2

 

where it's supposed to be like x1 >> longest  ,  x2 >>largest

#include<iostream>#include<string>using namespace std;int main(){int num,sumnew,sumold,A=1,B=2,C=3,D=4,E=5,F=6,G=7,H=8,I=9,J=10,K=11,L=12,M=13,N=14,O=15,P=16,Q=17,R=18,S=19,T=20,U=21,V=22,W=23,X=24,Y=25,Z=26;string x1,x2,s1;cin>>num;for(int i=0;i<num;i++){    cin>>s1;    sumnew=0;    for(int j=0;j<s1.size();j++)    {        sumnew=sumnew+s1[j];    }    if(sumnew>sumold)    {        x1.assign(s1);        sumold=sumnew;    }    if(s1.size()>x2.size())    {        x2.assign(s1);    }}cout<<x1<<endl<<x2;    return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

"sumold" is uninitialized

...and i don't think that there is really a need for those 26 variables, right?

Link to comment
Share on other sites

Link to post
Share on other sites

"sumold" is uninitialized

...and i don't think that there is really a need for those 26 variables, right?

 

are they assigned by default ?

Link to comment
Share on other sites

Link to post
Share on other sites

:v I forgot to write the problem .. just a sec

I had a look at your code and there are a couple problems:

 

1) sumold is uninitialized. You should always initialize values to 0. Not doing so may lead to whatever being in the allocated memory being used for its value.

2) 

sumnew=sumnew+s1[j];

does not add 1 to sumnew if the letter is A. It adds the ASCII table value. If you want to convert them to their respective index value in the alphabet, you'll have to subtract the offset (sumnew=sumnew+s1[j] - 96;) in this case, but remember that Capitals have different offsets. You can either convert all letters to lower case first or do some other conversion.

 

 There's more nitpicky stuff, but these two are the main problems.

Link to comment
Share on other sites

Link to post
Share on other sites

are they assigned by default ?

nope, they're not

only static variables are "assigned by default"

Link to comment
Share on other sites

Link to post
Share on other sites

 

 

I fixed those 2 now it's doesn't take the longest as the greatest but still the greatest value ain't right

#include<iostream>#include<string>using namespace std;int main(){int num,sumnew,sumold,A=1,B=2,C=3,D=4,E=5,F=6,G=7,H=8,I=9,J=10,K=11,L=12,M=13,N=14,O=15,P=16,Q=17,R=18,S=19,T=20,U=21,V=22,W=23,X=24,Y=25,Z=26;string x1,x2,s1;cin>>num;for(int i=0;i<num;i++){    cin>>s1;    sumold=0;    sumnew=0;    for(int j=0;j<s1.size();j++)    {        sumnew=sumnew+s1[j]-'0';    }if(sumnew>sumold)    {        x1.assign(s1);        sumold=sumnew;    }    if(s1.size()>x2.size())    {        x2.assign(s1);    }    s1.clear();}cout<<x1<<endl<<x2;    return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

 

I fixed those 2 now it's doesn't take the longest as the greatest but still the greatest value ain't right

You're subtracting '0' which is not equal to 96. 

 

Here's my code which I tested working. I edited the varnames for more clarity too.

#include<iostream>#include<string>using namespace std;int main(){    int iWordCount,iCurrentValue,iHighestValue = 0;    string highestValueString,longestString,currentString;    cin>>iWordCount;    for(int i=0; i<iWordCount; i++)    {        cin>>currentString;        iCurrentValue = 0;        for(int j=0;j<currentString.size();j++)        {            iCurrentValue += currentString[j] - '`';        }        if(iCurrentValue>iHighestValue)        {            highestValueString.assign(currentString);            iHighestValue = iCurrentValue;        }        if(currentString.size()>longestString.size())        {            longestString.assign(currentString);        }    }    cout<<highestValueString<<endl<<longestString;    return 0;}

Edit: Subtracting 'a' returns a value too small by 1. Subtract '`' instead. 

post-93043-0-95746400-1422201507.png

Link to comment
Share on other sites

Link to post
Share on other sites

Edit: Subtracting 'a' returns a value too small by 1. Subtract '`' instead.

Just adding 1 after subtracting 'a' works too.

Link to comment
Share on other sites

Link to post
Share on other sites

My solution. Didn't check, but should work.
 

#include <iostream>#include <string>using namespace std;int main(){	int t;	int currentVal=0;	int maxVal=0;	string currentString = "";	string longestString = "";	string highestString = "";	cin >> t;	while(t--){		cin >> currentString;		currentVal = 0;		for(int i=0; i<currentString.size(); i++){			currentVal += currentString[i] - 'A' + 1;		}		if(currentVal > maxVal){			highestString = currentString;			maxVal= currentVal;		}		if(currentString.size() > longestString.size()){			longestString = currentString;		}	}	cout << longestString << endl << highestString;	return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

Just adding 1 after subtracting 'a' works too.

True, makes it more readable too.

int getAlphabetIndex( const char x ){    return ( tolower(x) - 'a' ) + 1;}

That should do it. Takes care of Uppercase letters as well as lowercase letters and is more readable.

Link to comment
Share on other sites

Link to post
Share on other sites

alphabeta take care in reading your assignment, because the devil is in the details.  Look at the bolded portion of the quote, and underlined portion

 

You are given N words, write a program to compare these words and find the one with the longest length, and the one with the largest value.

A word's value is computed by summing up the values of its characters. i.e A=1, B=2..Z=26.

If two words have the same length or two words have the same value, consider the word that comes later in input.

 


That means instead of using > in comparing the values, you really need be using >=

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

alphabeta take care in reading your assignment, because the devil is in the details.  Look at the bolded portion of the quote, and underlined portion

 

That means instead of using > in comparing the values, you really need be using >=

Sharp!

Link to comment
Share on other sites

Link to post
Share on other sites

You're subtracting '0' which is not equal to 96. 

 

Here's my code which I tested working. I edited the varnames for more clarity too.

#include<iostream>#include<string>using namespace std;int main(){    int iWordCount,iCurrentValue,iHighestValue = 0;    string highestValueString,longestString,currentString;    cin>>iWordCount;    for(int i=0; i<iWordCount; i++)    {        cin>>currentString;        iCurrentValue = 0;        for(int j=0;j<currentString.size();j++)        {            iCurrentValue += currentString[j] - '`';        }        if(iCurrentValue>iHighestValue)        {            highestValueString.assign(currentString);            iHighestValue = iCurrentValue;        }        if(currentString.size()>longestString.size())        {            longestString.assign(currentString);        }    }    cout<<highestValueString<<endl<<longestString;    return 0;}

Edit: Subtracting 'a' returns a value too small by 1. Subtract '`' instead. 

 

 

 

My solution. Didn't check, but should work.

 

#include <iostream>#include <string>using namespace std;int main(){	int t;	int currentVal=0;	int maxVal=0;	string currentString = "";	string longestString = "";	string highestString = "";	cin >> t;	while(t--){		cin >> currentString;		currentVal = 0;		for(int i=0; i<currentString.size(); i++){			currentVal += currentString[i] - 'A' + 1;		}		if(currentVal > maxVal){			highestString = currentString;			maxVal= currentVal;		}		if(currentString.size() > longestString.size()){			longestString = currentString;		}	}	cout << longestString << endl << highestString;	return 0;}

 

 

alphabeta take care in reading your assignment, because the devil is in the details.  Look at the bolded portion of the quote, and underlined portion

 

 

That means instead of using > in comparing the values, you really need be using >=

 

Thanks alot :)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×