|
| 1 | +//PROGRAMMER: Ngoc Chau Nguyen |
| 2 | +//DATE: 2025-04-23 |
| 3 | +//DESCRIPTION: This program is a simple C++ program that prints "Hello, World!" to the console. |
| 4 | +//ASSISTANT: CHAT GPT and classmate Jasper Liu |
| 5 | + |
| 6 | +#include <iostream> //do I need it? |
| 7 | +#include <fstream> |
| 8 | +#include <sstream> //string stream -- allows me to treat a string as if it is a stream |
| 9 | +#include <vector> |
| 10 | + |
| 11 | + |
| 12 | +int main() |
| 13 | +{ |
| 14 | + //Read two lines of numbers in input.txt and store them into two vectors |
| 15 | + //std::ifstream -- standard input file stream. Read data from a file. |
| 16 | + std::ifstream inputFile("input.txt"); |
| 17 | + std::string line; |
| 18 | + std::vector<double> vector1; |
| 19 | + std::vector<double> vector2; |
| 20 | + |
| 21 | + |
| 22 | + if(inputFile.is_open()) |
| 23 | + { |
| 24 | + //Read the first line into first vector. |
| 25 | + //(Use while loop if want to read continuously) |
| 26 | + |
| 27 | + if(std::getline(inputFile, line)) |
| 28 | + //getline function reads an entire line of text from a stream |
| 29 | + //(in this case, that stream is inputFile) |
| 30 | + { |
| 31 | + std::stringstream ss(line); //treat string like a stream. Able to extract tokens using >>. |
| 32 | + double number; |
| 33 | + while(ss >> number) |
| 34 | + { |
| 35 | + vector1.push_back(number); |
| 36 | + } |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + //Read the second line //A few thought: maybe create a separate function to read each line |
| 41 | + if(std::getline(inputFile, line)) |
| 42 | + { |
| 43 | + std::stringstream ss(line); |
| 44 | + double number; |
| 45 | + while(ss >> number) |
| 46 | + { |
| 47 | + vector2.push_back(number); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + inputFile.close(); //closes the file |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + std::cout << "File could not be opened!" << std::endl; |
| 56 | + } |
| 57 | + |
| 58 | + //Print out vectors to check. |
| 59 | + std::cout << "x f[] f[,] f[, ,]" << std::endl; |
| 60 | + |
| 61 | + for(size_t i = 0; i < vector1.size(); ++i) |
| 62 | + { |
| 63 | + std::cout << vector1[i] << " " << vector2[i] << std::endl; |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + std::vector<double> vector3; |
| 68 | + std::cout << "This is vector 3" << std::endl; |
| 69 | + for(size_t i = 1; i < vector1.size(); ++i) |
| 70 | + { |
| 71 | + std::cout << (vector2[i] - vector2[i-1]) / (vector1[i] - vector1[i-1]) << std::endl; |
| 72 | + } |
| 73 | + |
| 74 | + std::vector<double> vector4; |
| 75 | + std::cout << "This is vector 4" << std::endl; |
| 76 | + std::cout <<"print out"<< std::endl; |
| 77 | + |
| 78 | + for(size_t i = 1; i < vector3.size(); ++i) |
| 79 | + { |
| 80 | + std::cout << (vector3[i] - vector3[i-1]) / (vector1[i+1] - vector1[i-1]) << std::endl; |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + return 0; |
| 85 | + |
| 86 | +} |
0 commit comments