Hello,
I’ve been getting wrong answer on these two problems : turbo sort and holes in the text. They run just fine on my compiler - I’m using xcode.
Kindly help.
//Holes in the text
int main()
{
int t, i, j = 1;
cin >> t;
if (t > 40)
return 1;
char hole[100];
int count[40];
Regarding the turbo sort Question…
Your logic has some error…Currently you are just checking whether a number has occurred or not BUT
You must count the number of occurrences of each number.
Then print each and every occurrence.
You must also keep in mind that number can be less than or equal to 106. So the array should be of size 1000001, in order to have index 1000000.
The edited code is as follows:
#include
#include
using namespace std;
long turbo[1000001] = {0};
int main()
{
long j, x = 0, i, p;
long maxx = 0;
cin >> j;
for (i = 0; i < j; i++)
{
scanf("%ld", &x);
if (x <= 1000000)
{
turbo[x]++;
if (x > maxx)
maxx = x;
}
}
for (p = 0; p <= maxx; p++)
{
while(turbo[p]--)
printf ("%ld\n",p);
}
return 0;
}
You made the same mistake of array index…Here the number of test cases are <=40 so you array size should be atleast 41 to accomodate index 40 in it.
The length of the string can be anything less than 100. So running loop up to 100 always will lead to Wrong Answer, as there can be garbage after the actual length of the string…to keep a check for that you must run the loop only up to the length of the string.
You must always keep the size of a char array 1 more than the max size of the string , so that ‘\0’ can also be accommodated in the array as it is the check for the length of the string.
The edited code is as follows:
#include
#include
#include
using namespace std;
int main()
{
int t, i, j = 1;
cin >> t;
char hole[101];
int count[41];
while (j <= t)
{
scanf ("%s", hole);
count[j] = 0;
int len=strlen(hole);
for (i = 0; i < len; i++)
{
if (hole[i] == 'A' || hole[i] == 'D' || hole[i] == 'O' || hole[i] == 'P' || hole[i] == 'R' || hole[i] == 'Q')
count[j]++;
else if (hole[i] == 'B')
count[j] += 2;
}
cout << count[j] << endl;
j++;
}
/*for (j = 1; j <= t; j++)
cout << count[j] << endl;*/
return 0;
}