In these assignments, you must design your own structs with at least 3 meaningful fields. To make checking easier, you must use the provided output functions to print results. Your solutions will be graded on correctness and adherence to the format.
Each assignment includes multiple test cases with corresponding input and output files:
- Input files:
test_data/input{X}{case}.txt(e.g.,test_data/input1a.txt,test_data/input1b.txt) - Output files:
test_data/output{X}{case}.txt(e.g.,test_data/output1a.txt,test_data/output1b.txt) - Test script:
check.sh- runs all programs and validates output against expected results
The programs are designed to accept the input file path as a command line argument.
To test your solutions:
./check.shThe script will test each question with multiple input cases and report which test cases pass/fail.
To test a specific program:
./check.sh question1.cppThis will only test question1.cpp with its input cases.
To run a program manually with a specific input file:
g++ -o question1 question1.cpp
./question1 test_data/input1a.txtReplace question1 with the appropriate question number and input1a.txt with the desired input file.
Your Task:
- Design a struct
Bookwith fields for bibliographic data (title, pages, isbn at minimum). - Create an array of books and fill with values.
- Print each book using the given output function.
Required Output Function:
void print_book(int index, const char* title, int pages, const char* isbn) {
std::cout << "Book[" << index << "]: "
<< "title=" << title << ", "
<< "pages=" << pages << ", "
<< "isbn=" << isbn << "\n";
}Example Input File Content:
2
The Hitchhiker's Guide
224
978-0345391803
Foundation
255
978-0553293357
Example Struct Values:
Book b1 = {"The Hitchhiker's Guide", 224, "978-0345391803"};
Book b2 = {"Foundation", 255, "978-0553293357"};Example Output:
Book[0]: title=The Hitchhiker's Guide, pages=224, isbn=978-0345391803
Book[1]: title=Foundation, pages=255, isbn=978-0553293357
Your Task:
-
Design a struct
Pointwith x and y coordinates. -
Write a function to rotate a point around the origin by angle θ (in radians):
x' = x*cos(θ) - y*sin(θ) y' = x*sin(θ) + y*cos(θ) -
Print the point before and after rotation.
Required Output Function:
void print_point_rotation(double x_before, double y_before,
double theta, double x_after, double y_after) {
std::cout << "Before rotation: (x=" << x_before << ", y=" << y_before << ")\n";
std::cout << "After rotation (θ=" << theta << " rad): "
<< "(x=" << x_after << ", y=" << y_after << ")\n";
}Example Input File Content:
1.0
0.0
1.5707963267948966
Example Output:
Before rotation: (x=1, y=0)
After rotation (θ=1.5708 rad): (x=0, y=1)
Your Task:
-
Design a struct
Rectanglewith fields such as width, height, thickness, density. -
Compute the mass of each plate using:
mass = width * height * thickness * density -
Store at least 2 plates and print results.
Required Output Function:
void print_plate(int index, double width, double height,
double density, double mass) {
std::cout << "Plate[" << index << "]: "
<< "width=" << width << ", "
<< "height=" << height << ", "
<< "density=" << density << ", "
<< "mass=" << mass << "\n";
}Example Input File Content:
2
2.0
3.0
0.01
7850
1.5
2.5
0.01
2700
Example Output:
Plate[0]: width=2, height=3, density=7850, mass=471.0
Plate[1]: width=1.5, height=2.5, density=2700, mass=101.25
Your Task:
- Design a struct
Sensorwith fields such as id, temperature, and voltage. - Store several sensors in an array.
- Use a pointer to iterate through the array (not indexing).
- Print the details of each sensor using the given output function.
Required Output Function:
void print_sensor(int index, int id, double temperature, double voltage) {
std::cout << "Sensor[" << index << "]: "
<< "id=" << id << ", "
<< "temperature=" << temperature << ", "
<< "voltage=" << voltage << "\n";
}Example Input File Content:
3
101
36.5
3.3
102
40.2
3.1
103
38.7
3.4
Example Output:
Sensor[0]: id=101, temperature=36.5, voltage=3.3
Sensor[1]: id=102, temperature=40.2, voltage=3.1
Sensor[2]: id=103, temperature=38.7, voltage=3.4
Your Task:
-
Design a struct
TaylorTermwith fields: order, coeff, value. -
Use it to approximate
sin(x)with N terms:sin(x) ≈ Σ (-1)^n * x^(2n+1) / (2n+1)! -
Store each term in an array and print each one.
-
Print the final approximation.
Required Output Functions:
void print_taylor_term(int index, int order, double coeff, double value) {
std::cout << "Term[" << index << "]: "
<< "order=" << order << ", "
<< "coeff=" << coeff << ", "
<< "value=" << value << "\n";
}
void print_taylor_result(const char* function_name, double x,
int terms, double approximation) {
std::cout << "Approximation of " << function_name
<< "(x) at x=" << x
<< " with " << terms << " terms: "
<< approximation << "\n";
}Example Input File Content:
1.0
3
Example Output:
Term[0]: order=1, coeff=1, value=1
Term[1]: order=3, coeff=-0.166667, value=-0.166667
Term[2]: order=5, coeff=0.00833333, value=0.00833333
Approximation of sin(x) at x=1 with 3 terms: 0.841667