1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Alex777 [14]
3 years ago
13

Write a c++ program to calculate the approximate value of pi using this series. The program takes an input n that determines the

number of values we are going to use in this series. Then output the approximation of the value of pi. The more values in the series, the more accurate the data. Note 5 terms isn’t nearly enough. You will try it with numbers read in from a file. to see the accuracy increase. Use a for loop for the calculation and a while loop that allows the user to repeat this calculation for new values n until the user says they want to end the program.
Create an input file with nano lab04bin.txt


12


123


1234


12345


123456


1234567


12345678


123456789


It has to be written in c++
Computers and Technology
1 answer:
Tanya [424]3 years ago
6 0

Answer:

#include <iostream>

#include <iomanip>

using namespace std;

int main() {

  char choice;

  cout << setprecision(12) << endl;

  while(true) {

      int sign = 1;

      double pi = 0;

      cout << "Enter number of terms: ";

      long n;

      cin >> n;

      for(long i = 1; i < n; i += 2) {

          pi += sign/(double)i;

          sign = -sign;

      }

      pi *= 4;

      cout << "value of pi for n = " << n << " is " << pi << endl;

      cout << "Do you want to try again(y or n)? ";

      cin >> choice;

      if(choice == 'n' || choice == 'N') {

          break;

      }

  }

  return 0;

}

Explanation:

You might be interested in
What are the common causes of signal loss in fiber optic cable connectors
timurjin [86]
Bending, twisting, or the overall age of the cable, or other stuff like that.
8 0
3 years ago
Write a recursive function, displayFiles, that expects a pathname as an argument. The path name can be either the name of a file
timama [110]

Answer:

Here is the Python function:

import os   #module used to interact with operating system

def displayFiles(pathname):  #recursive function that takes a pathname as argument

  if (os.path.isdir(pathname)):  #checks if specified path (argument) is an existing directory

      for content in os.listdir(pathname):  #gets the list of all files and directories in the directory and iterates through the items of this list of directory

          contents = os.path.join(pathname, content)  #joins contents of path

          displayFiles(contents)  #calls function recursively

  else:  #if pathname refers to a file

      filename=pathname  #sets filename to pathname

      file = os.path.basename(filename)  #gets base name in specified path

      print("File Name: ", file) #displays the name of file

      with open(filename, "r") as contents:  #opens file in read mode

          print("Content:")  #prints Content:

          for lines in contents:  #iterates through the contents of file

              print(lines)   #displays the contents of file

Explanation:

The recursive function  displayFiles contains a single argument  pathname which can either be a pathname or a filename  . If the given argument is pathname for directory  , then the directory is opened and os.listdir returns a list containing the names of the entries in the directory given by pathname and append these contents in the list. Then this function is called recursively to print each file name and this function is applied to each name in the directory. However if the pathname refers to a file then the pathname is set to filename , the file is opened in read mode and the contents of the file are displayed.

8 0
4 years ago
What if i accidentally delete an assignment in canvas.
elena55 [62]

Answer:

it shouldnt matter. If it was one that was already graded then you should be good. But if it hasnt had a grade and it doesnt ever come back, then check in with your teacher to make sure your still getting the grade.

Explanation:

Hope this helps :)

8 0
2 years ago
The /tmp directory is a temporary directory and will not exist on a system at all times. True or False?
jeka94

Answer:

False.

Explanation:

The /tmp directory is a directory that contains files that are required temporarily and also for temporary storage of data.The data inside the /tmp directory gets deleted when the system boots or shuts down.Since the directory exists permanently the content inside it is temporary.

So we can conclude that the answer is False.

5 0
3 years ago
Write switch-case statement that displays the following information if the temperature is the input Activities at Lake LazyDays
Marta_Voda [28]

Answer:

//here is code in c++.

#include <bits/stdc++.h>

using namespace std;

int main()

{

// variable

int temp;

cout<<"Please enter the temperature:";

//read temperature from user

cin>>temp;

int n;

// reduce the case for switch

n=temp/10;

// print output according to the switch case

switch (n) {

case 7:

case 6:

cout<<"tennis"<<endl;

break;

case 5:

case 4:

cout<<"golf"<<endl;

break;

default:

if(temp>=80)

cout<<"swimming"<<endl;

else

cout<<"skiing"<<endl;

break;

}

return 0;

}

Explanation:

Read the value of temperature from user and assign it to variable "temp".

Calculate n=temp/10, to limit the number of cases in the switch statement.

if temperature is greater or equal 60 & less than 80, it will print "tennis".

If temperature is greater or equal 40 & less than 60, it will print "golf".

in default case, if temperature is greater than 80, it will print "swimming".

if less than 40, print "skiing".  

Output:

Please enter  the temperature::67                                                                                        

tennis

3 0
4 years ago
Other questions:
  • Compilers can have a profound impact on the performance of an application. Assume that for a program, compiler A results in a dy
    8·1 answer
  • On an Android device, where can a user find the correct app to use to sync contacts and apps among devices?
    10·1 answer
  • Multiple choice
    9·2 answers
  • If userA wants to send a secure message to userB using an asymmetric cryptographic algorithm, which key does userB use to decryp
    11·1 answer
  • I need help with Microsoft.
    6·1 answer
  • 4. A friend knows you are taking a technology class in college and asks you how a hard drive works. In your own words, describe
    6·1 answer
  • Small data files that are deposited on a user's hard disk when they visit a website are called _______.
    6·2 answers
  • Which popular file format loses some of the information from the image? JPEG TIFF RAW NEF
    12·1 answer
  • What is data and information
    9·1 answer
  • Which key doesn't relate to keyboard
    6·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!