Structure of array

Jun 25, 2019 at 8:35am
here is my code, as u see i used structure of arrays. the question is how should i take the arrays element by user directly ?

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
#include<iostream>
#include<math.h>

using namespace std;

struct Data
{
	int x, y;
}S;

double interpolate(Data f[], int xi, int n)
{

	double P = 0; 
	for (int i = 0; i<n; i++)
	{
		double p= f[i].y;
		for (int j = 0;j<n;j++)
		{
			if (j != i)
				p =p*(xi - f[j].x) / (f[i].x - f[j].x);
		}
		
		P += p;
	} 
		return P;
}
int main()
{
	Data f[] = { { 0,2 },{ 1,3 },{ 2,12 },{ 5,147 }};

	cout << "Value of f(3) is : " << interpolate(S, 3, 4) << endl;
	system("pause");
	return 0;
}
Jun 25, 2019 at 9:06am
I'm not sure to fully understand your question. You want to let user input the values of your objects ?

If it's what you want, you can do :
 
cin>>f[i].x; //adapt this 
Jun 25, 2019 at 9:48am
Yes.I wanna to user cin values.I cant apply what you say. Are you sure ?
Jun 25, 2019 at 9:48am
> Data f[] = { { 0,2 },{ 1,3 },{ 2,12 },{ 5,147 }};
> cout << "Value of f(3) is : " << interpolate(S, 3, 4) << endl;
Try passing f, your actual array.

Not S, some uninitialised global variable with only 1 instance.


Jun 25, 2019 at 9:55am
To allow a random user to input the points of your array :
1
2
3
4
5
for(int i(0);i<sizeof(f);++i){
    cout<<"Input x and y"<<endl;
    cin>>f[i].x;
    cin>>f[i].y;
}


The sizeof(f) is only to get the number of cells of f[], but i suggest you to ask to the user how many cells he wants to input and then create an array of this size and fill it like :
1
2
3
4
5
6
7
8
9
int n;
cout<<"Nb values ?"<<endl;
cin>>n;
Data f[n];
for(int i(0);i<n;++i){
    cout<<"Input x and y"<<endl;
    cin>>f[i].x;
    cin>>f[i].y;
}


EDIT : My bad keskiverto is right. You should still use a vector instead of a static array.
Last edited on Jun 26, 2019 at 6:29am
Jun 25, 2019 at 9:57am
Your (Lagrange) interpolation (yes, I had to do two takes!) won't work very well if your struct elements are int. They should be of type double.

Your arrays would be much more flexible if you used a std::vector rather than a standard, fixed-size array.

Read @salem c's comment about which actual array to use.
Last edited on Jun 25, 2019 at 10:11am
Jun 25, 2019 at 9:52pm
@Zaap:
1
2
3
int n;
cin>>n;
Data f[n]; // this is not legal C++ 

The f has automatic storage duration. Memory for it is allocated from stack. The amount to allocate has to be known already when the executable is compiled. Alas, the compiler does not know what every user will type on every run of the program.

The C language does have support for VLA (variable lenght array), but the C++ does not. C++ Library has std::vector (which does not store data in stack).

Some C++ compilers do support VLAs for C++ as an "extension". Others don't. Use of nonstandard features is not portable.


Edit: Input can fail.
1
2
3
4
5
6
7
8
double x {};
double y {};
size_t count {};
while ( count < n && std::cin >> x >> y ) {
  f[count].x = x;
  f[count].y = y;
  ++count;
}

Here a new Data object is added only if it can be fully set and the 'count' holds the actual number of elements after the input loop.

With vector:
1
2
3
4
5
6
7
std::vector<Data> f;
f.reserve( n );
double x {};
double y {};
while ( f.size() < n && std::cin >> x >> y ) {
  f.emplace_back( x, y );
}
Last edited on Jun 25, 2019 at 10:00pm
Topic archived. No new replies allowed.