1. Homepage
  2. Programming
  3. ENGN2912B Scientific Computing in C++ - Assignment 03: Prime Numbers, String and Expressions parser

ENGN2912B Scientific Computing in C++ - Assignment 03: Prime Numbers, String and Expressions parser

Engage in a Conversation
Brown UniversityENGN2912BScientific Computing in C++Prime NumbersExpression ParserC++

Assignment 03

Summary

This assignment contains two problems. First, we will implement a simple command-line application that calculates and displays prime numbers. Second, we will implement a String class capable of storing strings of variable lengths and use this class to write a function that parses mathematical expressions and computes their result. CourseNana.COM

As usual, the goal of the assignment is to practice writing these functions from scratch to understand all details that such implementation entails. Therefore, students must not use existing library functions providing such implementations but to write them using control structures and simple operations. CourseNana.COM

Topics

The main concepts to practice in this assignment are: CourseNana.COM

  • Standard input and output
  • Memory management with new and delete
  • Character arrays
  • Passing arguments by reference and by pointers
  • Working with classes:
    • Declaration and definition
    • Constructors and destructors
    • Data members
    • Member functions
    • Operators

Grading

The assignment grade is based on automatic testing results, adherence to the assignment rules, and specific details of the implemented solution. CourseNana.COM

Per problem points are as follows: CourseNana.COM

  • Problem 1: 20 pts
  • Problem 2: 80 pts
    • String class: 40 pts
    • Expression calculator: 40 pts

Automated tests check the output of command-line programs for different inputs. They check some individual functions and classes independently from the command-line programs. They also check if all allocated memory has been properly deallocated when the program finishes. If any memory block remains allocated, tests fail with a message like MemoryCheck: ERROR: X blocks leaked!. CourseNana.COM

General Instructions (command line)

These are generic instructions about how to compile the assignment from a command-line window. CourseNana.COM

  1. Download the starter code using git clone <your repository url>.
  2. Open a command line shell and execute ./build.sh or build.bat to generate the project files using CMake and compile the provided code.
  3. Open any file you want edit with a text editor or C++ development environment (e.g. Visual Studio, Xcode) and add your code to them.
  4. Compile your code again with ./build.shor build.bat, or using the development environment.
  5. Execute the programs and verify they work as expected based on the specific instructions for each problem.
  6. Repeat steps 3-5 as many times as desired.
  7. Once you are satisfied with the result, save your changes using a git commit command. For example git commit -a -m "Complete assignment".
  8. Upload your assignment using git push.

You can use git commit and/or git push as many times as desired, either to save work in progress or to improve your code incrementally. CourseNana.COM

Visual Studio Code

VSCode can be used to complete the assignment and in replacement of some of the steps in the command-line instructions. Detailed steps were given during lecture. A quick summary is to clone the repository as in step 1, then use "Open Folder" in VSCode to open the cloned repo. CourseNana.COM

Student comments

A file named NOTES.txt is included in this repository. Students may use this file to write any comment about the assignment and its solution. CourseNana.COM

Problem 1: Prime numbers

Students must implement a command-line application called print_primes which asks the user for a number indicating how many prime numbers are desired, and then calculates and displays those prime numbers. CourseNana.COM

A prime number is a positive integer greater than 1 that is not a product of two smaller ones. CourseNana.COM

Students may place all their code within the provided main function, or split it in one or more functions as they wish. CourseNana.COM

Problem 1 - Requirements

The submitted code must meet the following requirements: CourseNana.COM

  • The program must display a prompt to the user asking how many primes to calculate. It should say How many primes?.
  • The program must read an integer n from the standard input using std::cin.
  • It must calculate and display the first n prime numbers. The first number to display is 2, then 3, and so forth.
  • All calculated primes must be stored in a dynamically allocated array. Use new and delete to manage the array memory.
  • Prime numbers must be displayed one per line as prime: <number>, where <number> must be replaced by an actual prime number. See the examples below.

Problem 1 - Examples

$ ./build/prob1/Release/print_primes.exe
How many primes?
1
prime: 2

$ ./build/prob1/Release/print_primes.exe
How many primes?
4
prime: 2
prime: 3
prime: 5
prime: 7

Problem 2: String class and expressions parser

This problem has two parts. CourseNana.COM

Part 1: variable length strings

In this section we will implement a String class capable of holding strings of variable length. This is similar to the one discussed in lecture, but instead of setting a fixed limit for the maximum string length, memory must be dynamically allocated with a size appropriate to the string each instance holds. CourseNana.COM

The class must support copy operations and some basic operators as described in the source code comments. CourseNana.COM

A command-line application named string_test is provided which students can use to help validate their String class implementation. Instructions about how to use this application is given in the source code. CourseNana.COM

Requirements

Students must do the following: CourseNana.COM

  • Fill the implementation of the String class in the files prob2/String.hpp and prob2/String.cpp following the instructions in them.
  • The String class must support copies, assignment and equality operators, and other basic operations detailed in the assignment source code. In particular, memory for storing the string must be allocated in the heap with the new operator. The implementation must only include control structures and loops, as well as, simple mathematical operations. In particular, all the string operations such as computing its length must be implemented by students and not obtained with library functions such as std:strlen and str::stcpy.

Part 2: expressions parser

In this section, we will use our String class to implement a command-line application expression_calc which solves simple mathematical expressions. Different from cmd_calculator in an earlier assignment, this program takes a single argument which is a mathematical expression, parses it, and computes its result. CourseNana.COM

The program is strict in the expression format which must either be a numeric constant or a binary operation. The left and right hand side of a binary operation are also expressions. We can create complex expressions by composing binary operations and constants at multiple levels. An exact definition of what constitutes a valid expression is given in the file prob2/calc_functions.hpp. A recommendation of how to implement the parser function is also given in the source code. CourseNana.COM

Requirements

Students must do the following: CourseNana.COM

  • Complete the implementation of the function parse_and_solve in the file prob2/calc_functions.cpp. Its declaration and notes about its behavior are in prob2/calc_functions.hpp. CourseNana.COM

  • Complete the implementation of the expression_calc program in the file prob2/expression_calc.cpp. The program takes a single argument which must be a valid expression and it displays its result. In any error situation, CourseNana.COM

    SOLVE: = CourseNana.COM

    where <expression> is the input expression given in as command line argument and <result> is a number which is the result the solving the expression. See the examples below. CourseNana.COM

    References

  • std::cin: standard input. CourseNana.COM

  • std::isdigit: checks if a character is a numeric digit. CourseNana.COM

  • std::atoi: converts text to an integer number. CourseNana.COM

  • std::atof: converts text to a floating point value. CourseNana.COM

Get in Touch with Our Experts

WeChat WeChat
Whatsapp WhatsApp
Brown University代写,ENGN2912B代写,Scientific Computing in C++代写,Prime Numbers代写,Expression Parser代写,C++代写,Brown University代编,ENGN2912B代编,Scientific Computing in C++代编,Prime Numbers代编,Expression Parser代编,C++代编,Brown University代考,ENGN2912B代考,Scientific Computing in C++代考,Prime Numbers代考,Expression Parser代考,C++代考,Brown Universityhelp,ENGN2912Bhelp,Scientific Computing in C++help,Prime Numbershelp,Expression Parserhelp,C++help,Brown University作业代写,ENGN2912B作业代写,Scientific Computing in C++作业代写,Prime Numbers作业代写,Expression Parser作业代写,C++作业代写,Brown University编程代写,ENGN2912B编程代写,Scientific Computing in C++编程代写,Prime Numbers编程代写,Expression Parser编程代写,C++编程代写,Brown Universityprogramming help,ENGN2912Bprogramming help,Scientific Computing in C++programming help,Prime Numbersprogramming help,Expression Parserprogramming help,C++programming help,Brown Universityassignment help,ENGN2912Bassignment help,Scientific Computing in C++assignment help,Prime Numbersassignment help,Expression Parserassignment help,C++assignment help,Brown Universitysolution,ENGN2912Bsolution,Scientific Computing in C++solution,Prime Numberssolution,Expression Parsersolution,C++solution,