C/C++ :: Program That Calculate PI Based On User Input For Accuracy?
May 24, 2014
I am trying to create a program that will calculate pi based on a user input for accuracy. If the user input .3 then when the leibniz infinite sum value at a particular i becomes less then the input of .3 then the loop will exit.
I have looked at a number of examples on the internet but I feel lost. I have put together working code that will infinitely output sums but I need the loop to stop when the sum value is less then the accuracy value.
My question is what is wrong with my while loop, why will it only give me infinite summations? How do I make it so that the loop will exit when my accuracy input is greater then the sum?
int main () {
double accuracy;
cout<<"Give an accurate number." << flush;//looks nice
cin>>accuracy;
int d;//initialize denominator
double pi = 0.0;
while(accuracy < d){
I have to calculate the value of PI with the accuracy (eps) given by the user. I started with this but I just got stuck now, I don't know what is going on with the code or why does it get stuck in the second iteration...
PHP Code:
#include <stdio.h> #include <stdlib.h> #include <math.h> int main(){ float eps=0.000001;
I need to create a program that prints a certain number of asterisks based on user input. The user inputs 5 and I want my program to output "*****". How would I do this in C? I've tried printf("%#**", myvariable) but this does not work it only prints "*".
I'm trying to create a program that will take input from a user and calculate it in a do-while loop. The program does the calculation but the answer is wrong. The loop also doesn't work. The purpose of the program is to see how much an item will cost after a discount is taken off and tax is added.
I have an extra credit project where I'm supposed to calculate an approximate the user's age based on the current date and their birthday. My problem is that I am not getting the proper date and time. C++ keeps giving me some random date. I looked up c++ time format on the internet because this is extra credit and not in my book.
I am currently using dev C++ 4.9.9.2. Here is my current code :
// included libraries #include <cstdlib> #include <iostream> #include <iomanip> #include <time.h> #define cls system("cls") #define pauseOutput system("pause") void printM(int x); void age(int m, int d, int y, int &yearAge, int &monthAge, int &dayAge);
I have a problem with my code while trying to simulate a snakes and ladders program. The objective of the program is to simulate many games (100000+) to gain statistics:
1. The most likely number of rolls. 2. The mean value of the number of rolls. 3. The most common square, C, on which a token may come to rest. i.e. move on to. 4. The probability of coming to rest on square C. 5. The least likely square on which a token may come to rest and what is the probability of this occurring. 6. The distribution in the number of rolls needed per game presented as a normalised graph. 7. The minimum number of rolls of the die - called Nmin. 8. How many different paths (unique sequences of die rolls) take Nmin rolls to complete the game?
However my program (on Tenacious C) doesn't seem to run at all...
I have run it on internet compilers and other compilers but it must work on Tenacious C.
Below is the code I have at the moment, it works fine when stepped through or ran on a different compiler.
How to create code to answer the 8 points above. For instance, to get point number 2. I must find the total rolls divided by the total number of games, but am not sure how to do this.
Code: #include <stdio.h> #include <math.h> #include <stdlib.h> #include <time.h> #define NUMBEROFGAMES 10 int GetThePositionOfMovement(int CurrentPosition, int dieValue);
I am doing an assignment that is to calculate the federal tax based on the users input. I am not entirely sure what it is doing, but it is definitely not what I want it to do. I commented out the loops because they seemed to have caused a problem, but there is still problems that are making the program do something else. Here is my code:
#include<iostream> #include<string> using namespace std; double getData(); double taxAmount();
[Code] ....
Functions just seem like they are not my thing. It never gets to the second function to do the calculations and then return to main to display the results.
I tried changing the code to a switch(status) with a default: at the end that looped the "please try again" but for some reason, that was a fail, it created an endless loop.
My assignment is to pixelate an image based on users input. I have a structure that stores the image, and in this structure I'm storing the height and width and a dynamic array of RGBA struct.
This is my function. my main issue (that I've found) is offsetting and figuring out a way to handle the "edge" if the dimensions don't add up to the set pixelation size.
how to stop outputting data based on a user input. The text file is as follows:
1. a 2. b 3. c
and the code I'm using is as follows:
int main (){ string line; int search; cout << "Enter a number from 1-3" << endl; cin >> search; search++; ifstream myfile ("example.txt");
[Code]...
What I want to do is to just output number 1 (the whole line) if the user enters number 1. However, I get an error on the second condition w/c is the "&& line!= search"
I'm trying to create a function where it allows the user to type in multiple amounts of integers, so if the user wanted to have 3 different storages that hold different integers, the input would look something like this:
5 97 12 31 2 1 //let's say this is held in variable "a" 1 3 284 3 8 // "b" 2 3 482 3 4 // "c" 2 3 4 2 3 // "d" 99 0 2 3 42 // "e"
Since we don't know what number the user will input every time, I'm not sure how to create a dynamically allocated array that will create an x amount of arrays every time.. I want to be able to access each index of a, b, c, d, e or however many arrays there are.
So far, this is what I have, but I'm having trouble creating the arrays since it's unpredictable. I'm purposely not using vectors because I don't really get how pointers work so I'm trying to play around with it.
int* x; int length, numbers; cin >> length; x = new int[length] for (int i=0;i<length;i++) { cin >> numbers; //this doesn't work because it only grabs the first line for some reason x[i] = numbers }
i want to display all the contents of a file excluding one based on user input say i have 4 records in the file numbered 1 to 4....the user chooses 2 to exclude it outputs records 1,3,4,4 when it should be records 1,3,4 what am i doing wrong here is the code.its basically displaying the last record in the file twice
Write a program to generate a report based on input received from a text file. Suppose the input text file student_status.txt contains the student’s name (lastName, firstName middleName), id, number of credits earned as follows :
Doe, John K. 3460 25 Andrews, Susan S. 3987 72 Monroe, Marylin 2298 87 Gaston, Arthur C. 2894 110
Generate the output in the following format :
John K. Doe 3460 25 Freshman Susan S. Andrews 3987 40 Sophomore Marylin Monroe 2298 87 Junior Arthur C. Gaston 2894 110 Senior
The program must be written to use the enum class_level :
The function deriveClassLevel should derive the class_level of the student based on the number of credits earned.
The first namespace globalType tys should derive the class level based on a two year school policy. and the second namespace globalType fys should derive the class level based on a four year school policy.
Four Year School Policy: Freshman 0-29 creditsSophomore 30-59 credits Junior 60-89 creditsSenior 90 or more credits
Two Year School Policy: Freshman 0-29 creditsSophomore 30 or more credits
NOTE : use ignore() function with ifstream objects whenever you want to ignore the newline character.
For example : ifstream transferSchoolFile ; transferSchoolFile.open("student_status.txt", ios::in);
while( !transferSchoolFile.eof()) { getline(transferSchoolFile,name) ; transferSchoolFile >> id >> credits; transferSchoolFile.ignore(); //Used here to ignore the newline character. …. }
I did this in parts so I got it working with a four year criteria without the user defined name spaces.
include <iostream> #include <fstream> #include <string>
I know I have some stuff to mess around with but I am currently stuck with two errors, first -
Error1error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartupC:UsersstephenDocumentsVisual Studio 2013ProjectsinputConsoleApplication1MSVCRTD.lib(crtexe.obj)ConsoleApplication1
then -
Error2error LNK1120: 1 unresolved externalsC:UsersstephenDocumentsVisual Studio 2013ProjectsinputDebugConsoleApplication1.exeConsoleApplication1
Program Description: Write a program to generate a report based on input received from a text file. Suppose the input text file student_status.txt contains the student's name (lastName, firstName middleName), id, number of credits earned as follows :
Doe, John K. 3460 25 Andrews, Susan S. 3987 72 Monroe, Marylin 2298 87 Gaston, Arthur C. 2894 110
Generate the output in the following format :
John K. Doe 3460 25 Freshman Susan S. Andrews 3987 40 Sophomore Marylin Monroe 2298 87 Junior Arthur C. Gaston 2894 110 Senior
The program must be written to use the enum class_level :
The function deriveClassLevel should derive the class_level of the student based on the number of credits earned.
The first namespace globalType tys should derive the class level based on a two year school policy. and the second namespace globalType fys should derive the class level based on a four year school policy.
Four Year School Policy: Freshman 0-29 creditsSophomore 30-59 credits Junior 60-89 creditsSenior 90 or more credits
Two Year School Policy: Freshman 0-29 creditsSophomore 30 or more credits
NOTE : use ignore() function with ifstream objects whenever you want to ignore the newline character. For example :
I know I have some stuff to mess around with but I am currently stuck with two errors, first -
Error1error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartupC:UsersstephenDocumentsVisual Studio 2013ProjectsinputConsoleApplication1MSVCRTD.lib(crtexe.obj)ConsoleApplication1
then -
Error2error LNK1120: 1 unresolved externalsC:UsersstephenDocumentsVisual Studio 2013ProjectsinputDebugConsoleApplication1.exeConsoleApplication1
So I have to make a program that allows the user to enter both positive and negative numbers and the program is suppose to calculate the sum of only the positive values while ignoring the negative values. Also it is to be a sentinel-controlled loop with a number ending the set of values.
I am trying to code a program that takes in user inputted data about percentages and using it to later on calculate a final grade. 4 percents are inputted, for Assignments, Midterm1, Midterm2, and Finals. If the percents dont add up to 1 (i tell the user to enter the decimal value) I set up and if statement to catch it and end the program. Except that if i type in
I have tried writing a code which takes two numbers from the user and calculates their square root then the roots are added up to return the sum. The program is coming out with loads of errors.
#include<iostream> #include<cmath> float main(){ using namespace std; float m1,m2,m3,m4,m5;
I'm trying to make a c++ program of this but i don't know how to use the bubble sorting.
Write a C++ application that asks the user to enter 10 numbers. The program then stores those numbers in an Array. The program should display the Mean , Median, and Mode.
Mean is the average of the 10 numbers. Median is the average of the 5th and the 6th numbers. (But you have to arrange the numbers in ascending order first using Bubble Sort before calculating the Median.) Mode is the most frequent number among the 10 numbers.
Okay, so my assignment for my C class is to generate a program that will allow the user to enter data for a stock and calculate the amount of stocks that ended up being positive, negative, and neutral.I tried to do this using one stock, here is my code;
I built a program that accepts two input from the user, using a array inside a loop, it is pass to a function inside a class which will display the two number, the problem is when the user is inputting a number and it is 1 the program continuously as the user to input a number, and when 2 is entered the program ask another number and end, but for example you entered 2 and 3. . . it will then outpu 2 and 4 (so 3 + 1 ) and always the last number is plus one. here is the code.
main.cpp #include <iostream> #include "newclass.h" using namespace std;
So we have a weekend assignment that is "Write a c++ program that will allow a user to input their first name into a string, and use the switch/case statement to produce the following output. Your Program should prompt the use rwith the numbers and the options for each (school, classification, and mood) allowing them to make a choice"
My issue is with the output. The output at the end is the number that they input and not the name of the case they chose, so if they chose option 1 for school it does not print out "BRCC" at the end for the output it prints a 1.
#include <iostream> #include <string> using namespace std; int main(void) { string name; cout << "Please enter your name: "; cin >> name;
I need a program to run that will accept an input for user id. It will take the customer input and capitalize the letters, and return invalid id with the user inputted values. Then if it's valid it will add a counter counting the number of letters and numbers. It will keep track until the user puts in !. It seems when I try to pass values from the array to my toUpper function to capitalize it it doesn't seem to work right.
I am so close to finishing this program of games, but I'm running into some issues while debugging it.
I've attached the google drive folder since there are several files (ignore the ones with [conflict], etc)...
First, I have the main menu using a switch to select the game. The default was set to exit. If initially when immediately opening the program you put in for say, a letter, it would exit like its supposed to. But if you went and played any of the games ], went back to the main screen, and entered a letter then, it would crash. I've put in a testing line to make sure that you enter a number, and not anything else.
Now however, if you play a game and then want to go back to the main menu, it will simply skip over waiting for your response and close the program. The same thing happens in the slot machine, I want to have it spin the slot machine by pressing enter, but it simply skips over the input, and I'm not sure why.