Question

How to turn this file into a main.cpp, a header which contains the function declaration, and...

How to turn this file into a main.cpp, a header which contains the function declaration, and a implementation fiel containing the function definition ?
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

#define NUM 1
#define MULT 4

void getPi(int iter);

int main()
{

   int it;

   cout << "Enter the number of iterations needed to find PI: ";
   cin >> it;

   while (it < 1) {
       cout << "Error!!! Iteration input should be positive." << endl;
       cout << "Enter the number of iterations needed to find PI: ";
       cin >> it;
   }

   getPi(it);
    return 0;
}

void getPi(int iter) {
   double j = 1, k = 1;
   for (int i = 1; i <= iter; i++) {
       if (i == 1) {
           cout << fixed << setprecision(5);
           cout << "Iterations: " << i << " Pi is approximated to be " << (j*MULT) << endl;
           j = 1;
           k += 2;
       }
       else {
           if (i % 2 == 0) {
               cout << fixed << setprecision(5);
               cout << "Iterations: " << i << " Pi is approximated to be " << ((j - (NUM / k))*MULT) << endl;
               j = (j - (NUM / k));
               k += 2;
           }
           else {
               cout << fixed << setprecision(5);
               cout << "Iterations: " << i << " Pi is approximated to be " << ((j + (NUM / k))*MULT) << endl;
               j = (j + (NUM / k));
               k += 2;
           }
       }
      
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

so we are creating our own header file to complete your objective the main.cpp file contains the main function.

I have created a header file with headerfilename.h extension that contains the function declaration.

The definition of the function is declared in headerfilename.cpp file.

main.cpp

#include <iostream>

#include "exampleheader.h" //the header file included that is created manually is shown below.

using namespace std;
int main()
{

int it;

cout << "Enter the number of iterations needed to find PI: ";
cin >> it;

while (it < 1) {
cout << "Error!!! Iteration input should be positive." << endl;
cout << "Enter the number of iterations needed to find PI: ";
cin >> it;
}

getPi(it);
return 0;
}

exampleheader.h

#ifndef EXAMPLEHEADER_H //this is to define our header file
#define EXAMPLEHEADER_H

void getPi(int x);

#endif

exampleheader.cpp // definition of our function

#include <iostream>
#include<string>
#include<iomanip>
#include "exampleheader.h"
#define NUM 1
#define MULT 4

int i,j,k;
void getPi(int iter) {
double j = 1, k = 1;
for (int i = 1; i <= iter; i++) {
if (i == 1) {
std::cout << std::fixed << std::setprecision(5);
std::cout << "Iterations: " << i << " Pi is approximated to be " << (j*MULT) << std::endl;
j = 1;
k += 2;
}
else {
if (i % 2 == 0) {
std::cout << std::fixed << std::setprecision(5);
std::cout << "Iterations: " << i << " Pi is approximated to be " << ((j - (NUM / k))*MULT) << std::endl;
j = (j - (NUM / k));
k += 2;
}
else {
std::cout << std::fixed << std::setprecision(5);
std::cout << "Iterations: " << i << " Pi is approximated to be " << ((j + (NUM / k))*MULT) << std::endl;
j = (j + (NUM / k));
k += 2;
}
}
  
}
}

## any queries feel free to comment

Add a comment
Know the answer?
Add Answer to:
How to turn this file into a main.cpp, a header which contains the function declaration, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The value of π can be approximated by using the following series: The program in main.cpp...

    The value of π can be approximated by using the following series: The program in main.cpp uses this series to find the approximate value of π. However, the statements are in the incorrect order, and there is also a bug in this program. Rearrange the statements and remove the bug so that this program can be used to approximate π. The code that they have: #include <iostream> #include <iomanip> using namespace std; int main() {     double pi = 0;     long...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string;...

    Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string; class Account { public: Account(); Account(string, double); void deposit(double); bool withdraw(double); string getName() const; double getBalance() const; private: string name; double balance; }; #endif ////////////////////////////////////////////// //AccountManager.hpp #ifndef _ACCOUNT_MANAGER_HPP_ #define _ACCOUNT_MANAGER_HPP_ #include "Account.hpp" #include <string> using std::string; class AccountManager { public: AccountManager(); AccountManager(const AccountManager&); //copy constructor void open(string); void close(string); void depositByName(string,double); bool withdrawByName(string,double); double getBalanceByName(string); Account getAccountByName(string); void openSuperVipAccount(Account&); void closeSuperVipAccount(); bool getBalanceOfSuperVipAccount(double&) const;...

  • Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int...

    Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...

  • please Code in c++ Create a new Library class. You will need both a header file...

    please Code in c++ Create a new Library class. You will need both a header file and a source file for this class. The class will contain two data members: an array of Book objects the current number of books in the array Since the book array is moving from the main.cc file, you will also move the constant array size definition (MAX_ARR_SIZE) into the Library header file. Write the following functions for the Library class: a constructor that initializes...

  • C++ programming I need at least three test cases for the program and at least one...

    C++ programming I need at least three test cases for the program and at least one test has to pass #include <iostream> #include <string> #include <cmath> #include <iomanip> using namespace std; void temperatureCoverter(float cel){ float f = ((cel*9.0)/5.0)+32; cout <<cel<<"C is equivalent to "<<round(f)<<"F"<<endl; } void distanceConverter(float km){ float miles = km * 0.6; cout<<km<<" km is equivalent to "<<fixed<<setprecision(2)<<miles<<" miles"<<endl; } void weightConverter(float kg){ float pounds=kg*2.2; cout<<kg<<" kg is equivalent to "<<fixed<<setprecision(1)<<pounds<<" pounds"<<endl; } int main() { string country;...

  • Consider the following program in which the statements are in the incorrect order. Rearrange the statements...

    Consider the following program in which the statements are in the incorrect order. Rearrange the statements so that the program prompts the user to input the height and the radius of the base of a cylinder and outputs the volume and surface area of the cylinder. Formant the output to two decimal places. #include <iomanip> #include <cmath> int main () {} double height; cout << ”Volume of the cylinder = “ <<PI * pow(radius, 2.0) * height << endl; cout...

  • c++ programming : everything is done, except when you enter ("a" ) in "F" option ,...

    c++ programming : everything is done, except when you enter ("a" ) in "F" option , it does not work. here is the program. #include <iostream> #include <string> #include <bits/stdc++.h> #include <iomanip> #include <fstream> using namespace std; #define MAX 1000 class Inventory { private: long itemId; string itemName; int numberOfItems; double buyingPrice; double sellingPrice; double storageFees; public: void setItemId(long id) { itemId = id; } long getItemId() { return itemId; } void setItemName(string name) { itemName = name; } string...

  • I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include...

    I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink {    string name;    double cost;    int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() {    const int size = 5;       Drink drinks[size] = { {"Cola", 0.65, 2},    {"Root Beer", 0.70, 1},    {"Grape Soda", 0.75, 5},    {"Lemon-Lime", 0.85, 20},    {"Water", 0.90, 20} };    cout <<...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT