-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogramming4.cpp
More file actions
150 lines (122 loc) · 4.31 KB
/
programming4.cpp
File metadata and controls
150 lines (122 loc) · 4.31 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//PROGRAMMER: Ngoc Chau Nguyen
//DATE: 2025-04-23
//DESCRIPTION: Placeholder
//ASSISTANT: CHAT GPT and classmate Jasper Liu
#include <iostream> //do I need it?
#include <fstream>
#include <sstream> //string stream -- allows me to treat a string as if it is a stream
#include <vector>
int main()
{
//Read two lines of numbers in input.txt and store them into two vectors
//std::ifstream -- standard input file stream. Read data from a file.
std::ifstream inputFile("input.txt");
std::string line;
std::vector<double> vector1;
std::vector<double> vector2;
int count = 0;
if(inputFile.is_open())
{
//Read the first line into first vector.
//(Use while loop if want to read continuously)
if(std::getline(inputFile, line))
//getline function reads an entire line of text from a stream
//(in this case, that stream is inputFile)
{
std::stringstream ss(line); //treat string like a stream. Able to extract tokens using >>.
double number;
while(ss >> number)
{
vector1.push_back(number);
++count;
}
}
//Read the second line //A few thought: maybe create a separate function to read each line
if(std::getline(inputFile, line))
{
std::stringstream ss(line);
double number;
while(ss >> number)
{
vector2.push_back(number);
}
}
inputFile.close(); //closes the file
}
else
{
std::cout << "File could not be opened!" << std::endl;
}
//Print out vectors to check.
std::cout << "x f[] f[,] f[, ,] " << std::endl;
for(size_t i = 0; i < vector1.size(); ++i)
{
std::cout << vector1[i] << " " << vector2[i] << std::endl;
}
std::vector<double> vector3;
std::cout << "This is vector 3" << std::endl;
for(size_t i = 1; i < vector1.size(); ++i)
{
vector3.push_back((vector2[i] - vector2[i-1]) / (vector1[i] - vector1[i-1]));
std::cout << vector3[i-1] << std::endl;
}
std::vector<double> vector4;
std::cout << "This is vector 4" << std::endl;
std::cout <<"print out"<< std::endl;
for(size_t i = 1; i < vector3.size(); ++i)
{
vector4.push_back((vector3[i] - vector3[i-1]) / (vector1[i+1] - vector1[i-1]));
std::cout << vector4[i-1] << std::endl;
}
std::vector<double> vector5;
std::cout << "Size of vector 4: " << vector4.size() << std::endl;
std::cout << "This is vector 5" << std::endl;
for(size_t i = 1; i < vector4.size(); ++i)
{
vector5.push_back((vector4[i] - vector4[i -1]) / (vector1[i + 2] - vector1[i -1]));
std::cout << vector5[i-1] << std::endl;
}
//print out interpolating polynomial
//cast vector1<double> to vector string
std::vector<std::string> vector1String;
for (const double& number: vector1)
{
vector1String.push_back("(x - " + std::to_string(number) + ")");
}
//print out the vector1String for now.
std::cout << "print out the vector1String for now." << std::endl;
for(const auto& str: vector1String)
{
std::cout << str << std::endl;
}
//print out the first element of each vector (from vector 2 to vector 5)
std::vector<std::vector<double>> vectorList;
vectorList.push_back(vector2);
vectorList.push_back(vector3);
vectorList.push_back(vector4);
vectorList.push_back(vector5);
//create a new vector that stores only first elements from vector 2 to vector 5
std::vector<double> firstElement;
for (const auto& vec: vectorList)
{
firstElement.push_back(vec[0]);
}
//print out first element
std::cout << "Testing--print out first element of each neccessary vector." << std::endl;
for (const auto& number: firstElement)
{
std::cout << number <<std::endl;
}
//print out interpolating polynomial
std::cout << "Print out interpolating polynomial--testing" << std::endl;
std::cout << firstElement[0];
for(size_t i = 1; i < firstElement.size(); ++i)
{
std::cout << " + " << firstElement[i];
for(size_t j = 0; j < i; ++j)
{
std::cout <<vector1String[j];
}
}
return 0;
}