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: 3828733 • 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

in main function checkrate(&rate, sal); here you are passing the address of rate in the function's first argument , but in function void checkrate(float *rate, float sal) you are accepting it as a float value that's not possible for an address.
To solve ur problem you can use the following program-
to compile follow the command -> cc filename.c
to execute follow the command -> ./a.out

#include<stdio.h>
float input()
{
float sal;
printf("Enter your salary: ");
scanf("%f",&sal);
return sal;
}
float checkrate(float sal)
{
   float rate;
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;
return rate;
}

int main()
{
int i;
float totsal, sal,rate;
for(i=0;i<7;i++)
{
float sal=input();
float rate=checkrate(sal);
printf("Rate is %f ",rate);
}
return 0;
}

to understand the addressing for variable check out this program too-
#include <stdio.h>
int main()
{
float float_value=1.234567;
unsigned long int address=(unsigned long int) &float_value;
printf("float value's address=%lu ", address);
printf("address's contents=%f ", * (float*) address);
return 0;
}