The main function of your program must contain an array called sine values of 20
ID: 3856699 • Letter: T
Question
The main function of your program must contain an array called sine values of 20 elements of type double:
Use a constant called SIZE (set to the value 20) for the size of this array, and throughout the rest of program wherever the array size is needed, obtain it through this constant and not the literal number 20.
The values of the array elements must be set to the sine of their index, e.g. sine values[0] must be sin(0), sine values[1] must be sin(1), etc. Setting these values must be done in a function with the prototype:
void fill_array(double array[]);
The fill array function must be called once at the beginning of the program.
The program must display a menu of choices to the user, accept their selection from the menu and respond to it, then display the menu again, until the user chooses to quit.
The valid menu choices are the numbers 1, 2, 3, and 4. If the user enters an invalid integer, the menu must be redisplayed. The program is only required to work if the menu choice is an integer.
If the user chooses a formula from the menu, they must be prompted for the values a and/or b contained in the formula, and then a table displayed showing the calculated values of the resulting formula:
Get the final values of the formula using the pre-calculated values contained in the sine values array – don’t call the function sin again in this part of the program.
For the values of a and b, the user many enter numbers with or without a fractional part.
Numbers with fractions must be displayed with 3 places after the decimal point, and columns of numbers must be right-aligned, as shown in the sample run below.
Besides the fill array function which is required, you may use other functions of your own design to organize your program if you like.
Use comments to show the main steps in the program, and to document variable declarations.
A sample run of your program should look like:
Formulas for which you can display values:
1. y = a sin(x)
2. y = sin(x) + b
3. y = a sin(x) + b
4. Quit
Enter the number of your choice: 1
Enter the value of a: 1
x y
-- -----
0 0.000 1 0.841 2 0.909
3 0.141
4 -0.757
5 -0.959
6 -0.279
7 0.657 8 0.989
9 0.412 10 -0.544
11 -1.000
12 -0.537 13 0.420
14 0.991
15 0.650
16 -0.288
17 -0.961
18 -0.751
19 0.150
Formulas for which you can display values:
1. y = a sin(x)
2. y = sin(x) + b
3. y = a sin(x) + b
4. Quit
Enter the number of your choice: 3
Enter the value of a: -1.5 Enter the value of b: 2
x y
-- -----
0 2.000 1 0.738 2 0.636 3 1.788 4 3.135 5 3.438 6 2.419 7 1.015 8 0.516
9 1.382 10 2.816
11 3.500
12 2.805
13 1.370
14 0.514
15 1.025
16 2.432
17 3.442
18 3.126
19 1.775
Formulas for which you can display values:
1. y = a sin(x)
2. y = sin(x) + b
3. y = a sin(x) + b
4. Quit
Enter the number of your choice: 4
Explanation / Answer
Answer for the given Question:
See the below code for given problem is
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define SIZE 20
void fill_array(double array[])
{
int i=0;
for(i=0;i<SIZE;i++)
{
array[i] =sin(i);
}
}
int main()
{
double sine[20];
double a=0.0, b=0.0;
double re[20];
int option =0,i=0; // user's entered option will be saved in this variable
//fill_array(sine);
for(i=0;i<20;i++)
{
sine[i] =sin(i);
}
for(i=0;i<20;i++)
printf("%.3lf ",sine[i]);
printf("Enter your Option ");
printf("1. y = a sin(x) 2. y = sin(x) + b 3. y = a sin(x) + b 4. Quit ");
scanf("%d",option);
do //do-while loop starts here.that display menu again and again until user select to exit program
{
if(option == 1) // Checking if user selected option 1
{
printf("Enter Value a ");
scanf("%lf",a);
for(i=0;i<SIZE;i++)
{
re[i] = sine[i] *a;
}
for(i=0;i<SIZE;i++)
printf("%.3lf",re[i]);
}
else if(option == 2) // Checking if user selected option 2
{
printf("Enter Value b ");
scanf("%lf",b);
for(i=0;i<SIZE;i++)
{
re[i] = sine[i] +b;
}
for(i=0;i<SIZE;i++)
printf("%.3lf",re[i]);
}
else if(option == 3) // Checking if user selected option 3
{
printf("Enter Value a ");
scanf("%lf",a);
printf("Enter Value b ");
scanf("%lf",b);
for(i=0;i<SIZE;i++)
{
re[i] = a * sine[i] +b;
}
for(i=0;i<SIZE;i++)
printf("%.3lf",re[i]);
}
else if(option == 4) // Checking if user selected option 4
{
exit(0);
}
else //if user has entered invalid choice (other than 1,2,3 or 4)
{
printf("Invalid Entry re-enter your opinion ");
scanf("%d",option);
}
}
while(option <=4); //condition of do-while loop
return 1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.