interpolation

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
i want to write a program that interpolates.i have to read the txt file that contain two columns ,the angle column and the amplitude column.this columns have headers.as like this:
angle amplitude
0.00 0.2200
0.80 0.0142
1.60 -0.01797 
2.40 0.0375
3.20 0.2186
4.00 -0.0091
4.80 -0.1769
5.60 0.0606

now i wrote a bit of the code to open the file and get the data in an array but they seems to be something wrong which i dont know.I want to take only values not headers put them in an array after that use those values from array and use them in my code.i did something like this
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
	int i=0;
    int j=0;
	double angle[8];
	double amplitude[8];
	//size of array more than number of entries in data file
	ifstream infile;
    infile.open("amplitude.txt");//open the text file
    if (!infile)
	{
       cout << "Unable to open file";
       exit(1); // terminate with error
    }

    while (!infile.eof())
    {
    do
    infile >>angle [8]>> amplitude[8];//To make three arrays for each column (a for 1st column, b for 2nd....)
    while (i<8);{
    ++i;
    };
       while (j<8){
       ++j;
       }
    cout<<angle[i];amplitude[j];
    }


    return 0;
}
now when i run this it says segmentation fault(core dumped)!what i am i doing wrong here.i am a beginner in programming
Last edited on
double angle[8];
Creates an array with 8 elements ... indexed from 0. These elements are angle[0], angle[1], ..., angle[7].

There is no angle[8] as your code requests. You probably meant angle[i].

I have no idea what you are doing with j.

Please put your code in code tags. http://www.cplusplus.com/articles/jEywvCM9/
Note that this:
1
2
3
4
5
do
infile >>angle [8]>> amplitude[8];//To make three arrays for each column (a for 1st column, b for 2nd....)
while (i<8);{
++i;
};
will be an infinite loop. i is not increased.
what is 0.2 left hand side input give on the right side interpolated value? Can you do that by hand? How do you turn the steps you took by hand into code... (no offense but a great many times when we get these types of questions, the problem is the coder does not know how to do the problem at all, let alone code it, so I ask).

Hello osteen91,

As lastchance has already mentioned I just do a copy and paste here.


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


When I loaded your code it came out like this:
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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;  // <--- Best not to use.

int main()
{
	constexpr size_t MAXSIZE{ 8 };

	int i = 0;
	int j = 0;
	double angle[MAXSIZE]{};  // <--- Changed.
	double amplitude[MAXSIZE]{};  // <--- Changed.
	//size of array more than number of entries in data file

	ifstream infile;

	infile.open("amplitude.txt");//open the text file

	if (!infile)
	{
		cout << "Unable to open file";
		return 1; //exit(1); // <--- if not in "main". // terminate with error  // <--- Changed.
	}

	while (!infile.eof())
	{
		do
			infile >> angle[8] >> amplitude[8];//To make three arrays for each column (a for 1st column, b for 2nd....)
		while (i < 8);
		{
			++i;
		};

		while (j < 8)
		{
			++j;
		}
		cout << angle[i]; amplitude[j];
	}


	return 0;
}

With a couple of minor fixes you are good down to line 27;

Then it falls apart.

On line 27 the while condition does not work the way you are thinking. By the time it figures out that you have reached "eof" you will have entered the do while loop one extra time.

Lines 32 - 34 increment the variable "i", but this is outside the do/while loop, so during the do/while loop "i" will always be zero and it will produce an endless loop.

Lines 36 - 39 As lastchance has asked what is this for. There is no real use. If your intent is to print out the arrays a for loop would work better.

Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// <--- First line in "main"
constexpr size_t MAXSIZE{ 8 };

if (!infile)
{
	cout << "Unable to open file";
	return 1; //exit(1); // <--- if not in "main". // terminate with error  // <--- Changed.
}

while (infile >> angle[i] >> amplitude[i])
{
	++i;
}

std::cout << std::fixed << std::showpoint << std::setprecision(2);

for (size_t lc = 0; lc < MAXSIZE; lc++)
{
	cout << std::setprecision(2) << std::setw(5) << angle[lc] << "  "
		<< std::setprecision(4) << std::setw(8) << amplitude[lc] << std::endl;
}

For line 7 it is best to use "return" when the if statement is in "main" and "exit()" when the if statement is in a function or a different file. Zero is considered a normal exit and any number greater than zero means there is a problem.

Hint: By using (1, 2, 3, etc.) the number can help track down where the problem occurred. This is useful when there are multiple "return" or "exit()" statements in a program. The "return" at the end of "main" should always return zero.

The while loop is the most often way to read a file of unknown length. When the stream fails to read anything then the while condition will fail and the program will move on.

Notice the use of "i" in the while condition. This will change the element of the array instead of what you did trying to put everything in element "8" of the array that dos not exist.

Line 15 is the way that it is added through My IDE. This line need done only once before it is used to output floating point numbers. Line 15 will manipulate the output until it is changed. Line 19 is a good demonstration of this.

The for loop is the usual method to print an array. If you need to a while loop can work.

I did not put the time into the output. Headings and spaceing are some additions that you could make later. Anyway the output looks like this:

 0.00    0.2200
 0.80    0.0142
 1.60   -0.0180
 2.40    0.0375
 3.20    0.2186
 4.00   -0.0091
 4.80   -0.1769
 5.60    0.0606

Hint: in the "setw"s that might have a minus sign be sure to include an extra place for the minus sign so that the decimal points will line up properly.

Hope that helps,

Andy
thank you so much for your all comments.i really thank you for taking yout time and reply to my topic.i learned a lot and i even went to edit my post and it looks nice than before.
#lastchance the reason why i was using a j there i thought to myself i have two colums ,angle and amplitude and i said okay i reference them as i and j respectively.thinking that my array while have angle elements of i from 1 to 8 (0.00,0.80,1.60,2.40,3.20,4.00,4.80,5.60) same thing for amplitude elements of j from 1 to 8 so that when i use them for example if i want 0.80 and 1.60 as my angles i will just say i2 & i3.same things applies to amplitude.


handy andy going into details with my code i have to use interpolation formula the one as :y = y1 + (y2-y1)/(x2-x1) * (x-x1).in this formula the values we know are y1 ,y2,x2,x1,x but the unknown is y from the formula.x is the value of an angle that the user enters into the code and it will interpolates buy the above formula to find its corresponding amplitude point which is y in the formula.the code have to ask the user to enter that x value as an input and it have to reject the characters,symbols and at the same time tell the user that the x value enterd is not in range if it is the case.The code as that x value is entered have to check where that value follow for example let say the entered value which is x (input is 0.45)based on the table values we have.so automatically you can see that ,that x input(0.45)follows between 0.00(x1) & 0.80(x2) ,and the corresponding amplitude points will be 0.2200(y1) & 0.0142(y2) .and by plugging those values in the formula the y value will be 0.1042 which will be corresponding to that x=0.45.just like that thats how the code how it have to work .you give it the input x and it have to find where that input follows and set the corresponding values according to the formula.and after that i will test it with some angles.

//The interpolation calculation should be done in a separate function that is called
from within the main function. MY program should be able to reject invalid
input characters such as letters, symbols and values outside the range of the
data. If the value entered is outside the range, your program should prompt the
user to reenter the angle. The program should also be able to check whether the
angle entered matches the one in the table. In that case, there is no need for
interpolation and the program should just print out the corresponding
amplitude//

also that part of headings is a challenge to me.i tried to include them but i failed when i put them i get all 0.00 throughout in both columns.THe output values is not important in but i just want to see if indeed i am using the right values which are from the txt file.

imy apology for writting this long.but i need your help on this.
Last edited on
Hello osteen91,

An observation: You edited your OP, but put everything in code tags. A little to much.

The part:

angle amplitude
0.00 0.2200
0.80 0.0142
1.60 -0.01797 
2.40 0.0375
3.20 0.2186
4.00 -0.0091
4.80 -0.1769
5.60 0.0606


The "outout" tag, ( the middle button that looks line two columns of lines would be better. And the code, starting with "#incluse <iostream>" to the closing brace } of main should be in "code" tags.

When I started with the program and created the input file I left out the headings because I was more focused on reading the file.

There are two ways you can deal with the headings:
First:
std::getline(infile, headings); where "headings" is defined as a "std""string". This is basically read the line and not use it.

Or:
1
2
infile >> angle;
infile >> amplitude;

where "angle" and "amplitude" are defined as "std::string"s. This way you can read the headings into variables and use them later.

Based on your OP and the code there in I was dealing with just reading the file and getting the information into the arrays. Until you get that part working there is no point in thinking about the rest of the program until you have read the file and have something to work with.

Other than dealing with the headings in the input file you should have something new to read the file with. Please post this in a new message and not change the first post any more than you have.

In the mean time I will see what else I can do with the program.

Hope that helps,

Andy
HANDY ANDY thank you for your response.

so you mean should i post this topic as new not editing in this old one?
Hello osteen91,

Post any changes to your code in a new message here. Not in a new topic. Changing your original post here can confuse people who come into this thread and can not see your original code, so they think everything is working properly and you could miss valuable help.

While I am thinking about it it is most often best to work in small steps instead of trying to do the whole program at one time.

This program, as an example, needs to read information from a file before you can use it in another part of the program.

Hope that helps,

Andy
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
56
[code]
THANK YOU Handy Andy
 going into details with my code i have to use interpolation formula the one as :y = y1 + (y2-y1)/(x2-x1) * (x-x1).in this formula the values we know are y1 ,y2,x2,x1,x but the unknown is y from the formula.x is the value of an angle that the user enters into the code and it will interpolates buy the above formula to find its corresponding amplitude point which is y in the formula.the code have to ask the user to enter that x value as an input and it have to reject the characters,symbols and at the same time tell the user that the x value enterd is not in range if it is the case.The code as that x value is entered have to check where that value follow for example let say the entered value which is x (input is 0.45)based on the table values we have.so automatically you can see that ,that x input(0.45)follows between 0.00(x1) & 0.80(x2) ,and the corresponding amplitude points will be 0.2200(y1) & 0.0142(y2) .and by plugging those values in the formula the y value will be 0.1042 which will be corresponding to that x=0.45.just like that thats how the code how it have to work .you give it the input x and it have to find where that input follows and set the corresponding values according to the formula.and after that i will test it with some angles.

//The interpolation calculation should be done in a separate function that is called
from within the main function. MY program should be able to reject invalid
input characters such as letters, symbols and values outside the range of the
data. If the value entered is outside the range, your program should prompt the
user to reenter the angle. The program should also be able to check whether the
angle entered matches the one in the table. In that case, there is no need for
interpolation and the program should just print out the corresponding
amplitude//.

i managed to do include the headings in the first part i may say of the code. it looks like this :

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;  // <--- Best not to use.

int main()
{
	constexpr size_t MAXSIZE{ 8 };

	int i = 0;
	double angle[MAXSIZE]{};  
	double amplitude[MAXSIZE]{};  
	string headings;
	//size of array more than number of entries in data file

	ifstream infile;

	infile.open("amplitude.txt");//open the text file
	std::getline(infile, headings);

if (!infile)
{
	cout << "Unable to open file";
	return 1; //exit(1); // <--- if not in "main". // terminate with error
}

while (infile >> angle[i] >> amplitude[i])
{
	++i;
}

std::cout << std::fixed << std::showpoint << std::setprecision(2);

for (size_t lc = 0; lc < MAXSIZE; lc++)
{
	cout << std::setprecision(2) << std::setw(5) << angle[lc] << "  "
		<< std::setprecision(4) << std::setw(8) << amplitude[lc] << std::endl;
}
}
Hello osteen91,

Your use of code tags is improving, but you do not have to put the code tags around the entire message just the program code. What you have done is make line 3 a vary very long line to read.

For lines 5 - 12 I believe your intention was a comment block, but you went about it the wrong way. The double forward slash "//" is a single line comment which lasts until a new line character is encountered.

To comment multiple lines as comments either each line needs to start with the "//" or use the multi line comment of "/*" at the beginning and "*/" at the end. These can be on a line by its self or at the very beginning and the last characters. http://www.cplusplus.com/doc/tutorial/program_structure/#comments

You have managed to piece together my code into a working program and that is fine as long as you understand how it works and what is happening with the different parts.

Earlier we covered that the input file has headings that I did not realize at first. You need to do something before line 43 to deal with these headings before you read the numbers in the while loop.

The next step would be the user input. I suggest a function to get this input and verify that is is valid and usable by the time you return from the function. or you could do it in main if you really want to, but the function would be better.

One thing I do not understand quite yet is that you read a file to get some numbers, but what are they used for? Reading and rereading line 3 I am starting to understand.

For now you can comment lines 50 - 54 once you know that it is reading the file properly. I would save this for later in case you need it or at least need to refer to it.

Before you start on the function for the interpolation get all the input you need so you have everything you need to work with.

Hope that helps,

Andy
Topic archived. No new replies allowed.