In: Computer Science
How can i modify my c code so that each number stored in the array is not the array index but the value of the array index converted to radians. I have already made a function for this converion above main().
Below is the code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float Deg2Rad (float degrees)
{
// Calculate & return value
float Result;
Result = ((M_PI*degrees)/180.0);
return (Result);
}
int main(void)
{
// Declare variables
int Array[90];
int i;
// Loop from 0 to 90 inclusive
for ( i = 0 ; i < 91 ; i++ )
{
Array[i] = i;
printf ("Array number %d contains value %d\n", i, Array[i]);
}
// Exit the application
return 0;
}
Errors in your code
The corrected C Program is given below.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float Deg2Rad (float degrees)
{
// Calculate & return value in radians
float Result;
Result = ((M_PI*degrees)/180.0);
return (Result);
}
int main(void)
{
// Declare variables
float Array[90];
int i;
// Loop from 0 to 90 inclusive
for ( i = 0 ; i < 91 ; i++ )
{
// call the function Deg2Rad() passing the array index i
// and save the returned value to the array
Array[i] = Deg2Rad(i);
printf ("Array number %d contains value %f\n", i, Array[i]);
}
// Exit the application
return 0;
}
Console Output