Reputation: 13
I am getting a runtime error after I submit the solution on Codechef. I can compile and execute the solution in Code blocks on my machine. Please check the code and let me know what is wrong.
Problem definition -
All submissions for this problem are available.
In a company an emplopyee is paid as under: If his basic salary is less than Rs. 1500, then HRA = 10% of base salary and DA = 90% of basic salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic salary. If the Employee's salary is input, write a program to find his gross salary.
NOTE: Gross Salary = Basic Salary+HRA+DA Input
The first line contains an integer T, total number of testcases. Then follow T lines, each line contains an integer salary. Output
Output the gross salary of the employee. Constraints
1 ≤ T ≤ 1000 1 ≤ salary ≤ 100000 Example
Input
3 1203 10042 1312
Output
2406 20383.2 2624
My solution -
#include <stdio.h>
#include <stdlib.h>
int main()
{
int arr1[10];
double arr2[10];
int t,t1;
int i,j;
float HRA,DA,GS;
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d",&arr1[i]);
}
i=0;
t1=t;
while(t>0)
{
if(arr1[i]<1500)
{
HRA=(0.1*arr1[i]);
DA=(0.9*arr1[i]);
GS=(arr1[i]+HRA+DA);
arr2[i]=GS;
}
if(arr1[i]>=1500)
{
HRA=500;
DA=(0.98*arr1[i]);
GS=(arr1[i]+HRA+DA);
arr2[i]=GS;
}
i++;
t--;
if(i==t1)
break;
}
for(j=0;j<i;j++)
{
printf("\n%g",arr2[j]);
}
return 0;
}
Upvotes: 0
Views: 508
Reputation: 13
The solution is now accepted. The question had a constraint
1 ≤ T ≤ 1000
I modified the code to int arr1[1000] and double arr2[1000] and it got accepted.
Thanks for the help!
Upvotes: 1
Reputation: 18381
The i
variable in the first loop is indexing an array of 10 elements and it is going from 0 to t-1
, while the t
variable is read from user/test script and is not guaranteed to be less than 10
. So once it is more than that, you get an index out of bounds and memory violation.
Upvotes: 1