// i dont know where i am getting wrong..the answer is contniously zero...plz help me
    #include<iostream>
    #include<cstdlib>
    using namespace std;
    int rand_val()
    {
        return rand()%1000000+10000;
    }
    int main()
    {
        int i,j;
        int salary_vec[100]={};
        for(  i=0;i<100;i++)
        {
            salary_vec[j]=rand_val();
        }

        double tax_vec[100]={}; 
        for( j=0;j<100;j++)
        {
            if(salary_vec[i]>=70000)
            {
                tax_vec[j]=salary_vec[i]*(10/100);
            }
            else if((salary_vec[i]>=50000)&&(salary_vec[i]<70000))
            {
                tax_vec[j]=salary_vec[i]*(7.5/100);
            }
            else if((salary_vec[i]>=30000)&&(salary_vec[i]<50000))
            {
                tax_vec[j]=salary_vec[i]*(5/100);
            }
            cout<<tax_vec[j]<<endl;
        }
        return 0;
    }

Recommended Answers

All 6 Replies

return rand()%1000000+10000;

What is the maximum value rand() will return?
What is the remainder of that value and %1000000 ?

Think about what WaltP said. If the result is always zero, then none of the if statements are true. You never get a value greater than or equal to 70,000 or 50,000 or 30,000.

@dx9_programmer
I have checked the values through debugging.Some values are greater than 30,000 like 42,526 etc, but still it is giving zero.Is there any logical error or something.
The rand_val() function returns the values from 10,000 to 100,000.

The maximum value that rand() will return is determined by a variable in cstdlib called RAND_MAX. I think it's compiler/system-dependent, but should be at least 32767. What is it on your system?

The rand_val() function returns the values from 10,000 to 100,000.

Your rand_val() function should only return 10,000 and (10,000 + RAND_MAX), which the constant RAND_MAX depends on your system.

However, another look at your code I could see several minor issues:

On line 15 you have, salary_vec[j]=rand_val(); you are supposed to put an 'i' instead of a 'j' as in `salary_vec[i]=rand_val()

for( i=0;i<100;i++)
{
    salary_vec[i]=rand_val();   // use index 'i'
}

And in your second for loop you always refer to if(salary_vec[i]>=70000) (line 21) or else if((salary_vec[i]>=50000)&&(salary_vec[i]<70000)) (line 25). Here you're using variable 'i' but it should be using variable 'j'.

Instead of using the 'j' variable you can just stick to using the 'i' because it's not being used in a nested loop.

Thank you for pointing out the mistakes.I had replaced 'j' with 'i'.
The real problem was with rand_val().I have modified it and it works now.

int rand_val(void)
{
int upper_limit=100000;
int lower_limit=10000;
int range=upper_limit-lower_limit;
int val=(5*rand())%range;
int shift_val=val+lower_limit;
return shift)_val;
}
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.