Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C Programming Why is the rate value no being passed by properly? ** I know using

ID: 3828732 • Letter: C

Question

C Programming

Why is the rate value no being passed by properly? ** I know using arrays is way better for storing values, but I am not allowed to use them** I would like to make the loop basically get the input as salary then spit out the rate for 7 times, thanks

#include<stdio.h>

void main()
{
int i;
float totsal, sal,rate;

for(i=0;i<7;i++)
{

input(&sal);
checkrate(&rate, sal);
printf("%f", &rate);

}


}

void input(float *sal)
{
printf("Enter your salary: ");
scanf("%f", &*sal);

}
void checkrate(float *rate, float sal)
{


if(sal>0 && sal<30000)
*rate=7.0;


else if(sal>=30000 && sal<=40000)
*rate=5.5;


else if(sal>40000)
*rate=4.0;
*rate=0;
}

Explanation / Answer

Hi

I have fixed the issue and highlighted the code changes below

#include<stdio.h>
void input(float *sal);
void checkrate(float *rate, float sal);
int
main()
{
int i;
float totsal, sal,rate;
for(i=0;i<7;i++)
{
input(&sal);
checkrate(&rate, sal);
printf("%f ", rate);
}
return 0;
}
void input(float *sal)
{
printf("Enter your salary: ");
scanf("%f", &*sal);
}
void checkrate(float *rate, float sal)
{

if(sal>0 && sal<30000)
*rate=7.0;

else if(sal>=30000 && sal<=40000)
*rate=5.5;

else if(sal>40000)
*rate=4.0;
else
*rate=0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                                

sh-4.2$ main                                                                                                                                                                                                                                                             

Enter your salary: 100                                                                                                                                                                                                                                                   

100.000000                                                                                                                                                                                                                                                               

0.000000                                                                                                                                                                                                                                                                 

Enter your salary: ^C                                                                                                                                                                                                                                                    

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                                

sh-4.2$ main                                                                                                                                                                                                                                                             

Enter your salary: 100                                                                                                                                                                                                                                                   

7.000000                                                                                                                                                                                                                                                                 

Enter your salary: 1000                                                                                                                                                                                                                                                  

7.000000                                                                                                                                                                                                                                                                 

Enter your salary: 30001                                                                                                                                                                                                                                                 

5.500000                                                                                                                                                                                                                                                                 

Enter your salary: 3                                                                                                                                                                                                                                                     

7.000000                                                                                                                                                                                                                                                                 

Enter your salary: 15000                                                                                                                                                                                                                                                 

7.000000                                                                                                                                                                                                                                                                 

Enter your salary: 100000000                                                                                                                                                                                                                                             

4.000000                                                                                                                                                                                                                                                                 

Enter your salary: 11                                                                                                                                                                                                                                                    

7.000000