Array Problem

Hello I am writing a program that gets the minimum(xmin) and maximum value(xmax) of x from the user and then evaluates an equation over the range of values between the minimum value and maximum value.
Equation: f(x) = 0.0572*cos(4.667*x) + 0.0218*pi*cos(12.22*x);
Then the program has to display a table showing the x values and y values. I need to have at least 20 values, so assuming the user enters xmin as -2 and xmax as 2 and the number of values should be 21, the increment will be 0.2.
This is what i have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	const int rows = 20;
	const double pi = 3.1416;
	double xmin, xmax, x, y, increment, value, result, a;  
	
	cout << "Enter the minimum value of x";
	cin >> xmin; 
	cout << "Enter the maximum value of x"; n h
	cin >> xmax; 
	
	increment = (xmax - xmin) / rows; 
	
	for ( a = 1; a <= 20; a++);
	{
		y = 0.0572 * cos(4.667 * x) + 0.0218 * pi * cos(12.22 * x);
		result = value[a];
		x+= increment; 
	}
}


I am just having problems storing the 20 numbers in an array because when i execute the program i get the error "invalid types 'double[double]' for array subscript".

Any help is very much appreciated.
Last edited on
I haven't understood your clearly so I can help you only to compile the program.

a should be declared as int because index of particular element of array can not be double.
You haven't declared value as an array, it should be double ... value[20]
This will fix that error.

Indexing starts from 0 so for loop should look like this for (a = 0; a < 20; a++);
Also, variable x is undefined...

This is a golden rule in coding, always make sure variables are assigned some value, preferably upon declaration.

Thanks for the replies. I made the necessary changes and the error is not showing up now. Now I have to enter the 20 pairs of x and y values into a table. This is my updated code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
	const int rows = 20;
	const double pi = 3.1416;
	double xmin, xmax, y, increment;
	double value[20], xval[20];
	int a;   
	
	cout << "Enter the minimum value of x: ";
	cin >> xmin; 
	cout << "Enter the maximum value of x: "; 
	cin >> xmax; 
	
	double x = xmin; 
	increment = (xmax - xmin) / (rows - 1); 
	
	for ( a = 0; a < 20; a++);
	{
		y = 0.0572 * cos(4.667 * x) + 0.0218 * pi * cos(12.22 * x);
		y = value[a];
		x = xval[a]; 
		x+= increment;
	}
	
	cout << "X-Value  |  Y-Value" << endl; 
	cout << "_________|__________" << endl; 
	cout << setw(6) << xval[0] << setw(15) << value[0] << endl;
	cout << setw(6) << xval[1] << setw(15) << value[1] << endl;
	cout << setw(6) << xval[2] << setw(15) << value[2] << endl; 
	cout << setw(6) << xval[3] << setw(15) << value[3] << endl; 
	cout << setw(6) << xval[4]<< setw(15) << value[4] << endl; 
	cout << setw(6) << xval[5] << setw(15) << value[5] << endl; 
	cout << setw(6) << xval[6] << setw(15) << value[6] << endl; 
	cout << setw(6) << xval[7] << setw(15) << value[7] << endl; 
	cout << setw(6) << xval[8] << setw(15) << value[8] << endl; 
	cout << setw(6) << xval[9] << setw(15) << value[9] << endl; 
	cout << setw(6) << xval[10] << setw(15) << value[10] << endl; 
	cout << setw(6) << xval[11] << setw(15) << value[11] << endl; 
	cout << setw(6) << xval[12] << setw(15) << value[12] << endl; 
	cout << setw(6) << xval[13] << setw(15) << value[13] << endl; 
	cout << setw(6) << xval[14] << setw(15) << value[14] << endl; 
	cout << setw(6) << xval[15] << setw(15) << value[15] << endl; 
	cout << setw(6) << xval[16] << setw(15) << value[16] << endl; 
	cout << setw(6) << xval[17] << setw(15) << value[17] << endl; 
	cout << setw(6) << xval[18] << setw(15) << value[18] << endl;  
	cout << setw(6) << xval[19] << setw(15) << value[19] << endl;  
	cout << setw(6) << xval[20] << setw(15) << value[20] << endl; 

return 0;  
}


The values in the table are coming out incorrectly. What am I doing wrong?
Last edited on
Hi,

Lines 25 & 26 aren't right, the arrays are not initialised, are you sure you didn't mean:

22
23
24
25
26
27
28
29
30
	for ( a = 0; a < 20; a++);
	{
                // would yvalue be a better name?
		value[a] = 0.0572 * cos(4.667 * x) + 0.0218 * pi * cos(12.22 * x);
		//y = value[a];            
		//x = xval[a]; 
                xval[a] = x; // would xvalue be a better name?
		x+= increment;
	}


Are the arguments to the trig functions in radians? Is 4dp in the value of pi sufficient? Try 3.14159265358979323846

Also, consider using a for loop to print the table, and put that into a function.

Had these warnings:

52:53: warning: array subscript is above array bounds [-Warray-bounds] 
52:28: warning: array subscript is above array bounds [-Warray-bounds]


Arrays go from 0 to size-1, in this case 0 to 19. These problems are avoided if you use a for loop correctly.

Also, avoid magic numbers like 20 in your code, make it a const variable instead:

const unsigned int ArraySize = 20; //could use std::size_t here, as the type.

Just noticed you had rows which is the same thing, but you haven't used it as much as you should have.
Last edited on

Hi TheIdeasMan :D

I am required to use the value 3.1416 for pi.
Other than that I followed the things you said but I am still getting incorrect output from my program. Here is the updated version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
	const int rows = 20;
	const double pi = 3.1416;
	double xmin, xmax, y, increment;
	double yvalue[rows], xvalue[rows];
	int a;   
	
	cout << "Enter the minimum value of x: ";
	cin >> xmin; 
	cout << "Enter the maximum value of x: "; 
	cin >> xmax; 
	
	double x = xmin; 
	increment = (xmax - xmin) / (rows - 1); 
	
	for ( a = 0; a < rows; a++);
	{
		yvalue[a] = 0.0572 * cos(4.667 * x) + 0.0218 * pi * cos(12.22 * x);
		xvalue[a] = x;
		x+= increment;
	}
	
	cout << "X-Value  |  Y-Value" << endl; 
	cout << "_________|__________" << endl; 
	
	for (int j = 0; j < rows; j++)
	{
		cout << setw(6) << xvalue[j] << setw(6) << yvalue[j] << endl; 
		j++;
	} 

return 0;  
}


Ok, just spotted the pesky semicolon on line 22 :+)
Topic archived. No new replies allowed.