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

Arrays: Write a C++ program which uses an array of numbers to store pre-calculat

ID: 3680266 • Letter: A

Question

Arrays: Write a C++ program which uses an array of numbers to store pre-calculated values of the sine function of certain values, and can then display values of various different formulas further calculated from the stored values.

Requirements:

Name the source file for your program program7.cpp

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

Hints:

The library <cmath> contains the function sin.

Explanation / Answer

/**C++ program that prompts user with a menu of choices.
Then prompt for a , b values of corresponding selection
and print the ressults to console*/
//header files
#include<iostream>

#include <cstdlib> //header file for exit function
#include<math.h>
#include<iomanip>
using namespace std;
//function prototype
void fill_array(double arr[]);
void print(double arr[]);
void product(double arr[], double a);
void add(double arr[], double a);
int menu();
//constant size value
const int SIZE =20;
int main()
{

   //declare variables
   double a,b;
   double arr[SIZE];

   //calling fill_array
   fill_array(arr);
  

   //repeat the loop until user enters 4 to quit the program
   while(true)
   {
       int ch=menu();

       switch(ch)
       {
       case 1:
           cout<<"Enter the value of a: ";
           cin>>a;
           product(arr,a);
           print(arr);
           break;
       case 2:
           cout<<"Enter the value of b: ";
           cin>>b;
           add(arr,b);
           print(arr);
           break;
       case 3:
           cout<<"Enter the value of a: ";
           cin>>a;
           cout<<"Enter the value of b: ";
           cin>>b;
           product(arr,a);
           add(arr,b);
           print(arr);
           break;
      
       case 4:
           //terminate the program
           exit(0);
       }
   }


   system("pause");
   return 0;

}
//The method product that takes array, arr and product a to the array
void product(double arr[], double a)
{
   for(int index=0;index<SIZE;index++)
   {
       arr[index]=a*arr[index];
   }
}

//The method add that takes array, arr and add b to the array
void add(double arr[], double b)
{
   for(int index=0;index<SIZE;index++)
   {
       arr[index]=arr[index]+b;
   }
}

//menu method that prompts for user choice
int menu()
{

   int choice;

   do
   {
       cout<<"1. y = a sin(x)"<<endl;
       cout<<"2. y = sin(x) + b"<<endl;
       cout<<"3. y = a sin(x) + b"<<endl;
       cout<<"4. Quit"<<endl;

       cout<<"Enter the number of your choice:";
       cin>>choice;

   }while(choice<0 || choice>4);


   return choice;
}

//fill array arr using numbers of size 20 array
//using sin functio
void fill_array(double arr[])
{

   for(int index=0;index<SIZE;index++)
   {
       arr[index]=sin(double(index));
   }

}
//print the array to console
void print(double arr[])
{

   double value=0;
   for(int index=0;index<SIZE;index++)
   {
       cout<<left<<setw(10)<<value
           <<left<<setw(10)<<setprecision(4)<<arr[index]<<endl;
       value++;
   }
}

--------------------------------------------------------------------------------------------------------------------------------

Sample output:

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
0         0
1         0.8415
2         0.9093
3         0.1411
4         -0.7568
5         -0.9589
6         -0.2794
7         0.657
8         0.9894
9         0.4121
10        -0.544
11        -1
12        -0.5366
13        0.4202
14        0.9906
15        0.6503
16        -0.2879
17        -0.9614
18        -0.751
19        0.1499
1. y = a sin(x)
2. y = sin(x) + b
3. y = a sin(x) + b
4. Quit
Enter the number of your choice:2
Enter the value of b: 1
0         1
1         1.841
2         1.909
3         1.141
4         0.2432
5         0.04108
6         0.7206
7         1.657
8         1.989
9         1.412
10        0.456
11        9.793e-006
12        0.4634
13        1.42
14        1.991
15        1.65
16        0.7121
17        0.0386
18        0.249
19        1.15
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
0         0.5
1         -0.7622
2         -0.8639
3         0.2883
4         1.635
5         1.938
6         0.9191
7         -0.4855
8         -0.984
9         -0.1182
10        1.316
11        2
12        1.305
13        -0.1303
14        -0.9859
15        -0.4754
16        0.9319
17        1.942
18        1.626
19        0.2752
1. y = a sin(x)
2. y = sin(x) + b
3. y = a sin(x) + b
4. Quit
Enter the number of your choice:

Note : Use visula studio c++ software to run this program.

Steps to create a new project.

File->New->Project

Select EmptyProject and set a name of choice.

Then right on the source Files and select the C++ file and give a name as Program7

and copy the code to the editor and run the program , press F5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote