C++ :: How To Determine If Two Numbers Differ From A Specific Value
Dec 2, 2014
I have to write a program that use a while loop and that each time around the loop reads two double, prints out the largest, the smallest and if they are equal.
I have no problem with this program but I can't understand how to modify it following the author : Change the program so that it writes out "the numbers are almost equal" after writing out which is the larger and the smaller if the two numbers differ by less than 1.0/100.
for now this is my code :
int main() {
double a = 0;
double b = 0;
while (cin >> a >> b) {
cout << "first number is : " << a << "
second number is : " << b << "
[Code] ....
The problem is that I don't know how to test if the numbers differ by less than 0.01 because my if statement doesn't work in any case. If I enter 2 and 1.99 It doesn't work, why ?
I'm trying to determine the number of times I have to change each specific character in a string to make it a palindrome. You can only change a character one at a time from the end.
Example: "abc" -> "abb" -> "aba" should print 2. "aba" will print 0 because it's already a palindrome. "abcd" -> "abcc" -> "abcb" -> "abca" -> "abba" will print 4 because it took 4 changes to make a palindrome.
I'm not too sure how to approach this - I figured out the case where if it's a palindrome (if reversed string is the same) then it'll print out a 0.
int main() { int number; cin >> number; //expecting a number for first line user input for (int i = 0; i < number; i++) { string str;
I've pretty much finished the entire program, except for the actual calculation part.
"Given a range of values determine how many integers within that range, including the end points, are multiples of a third value entered by the user. The user should be permitted to enter as many of these third values as desired and your output will be the sum of total multiples found."
I've defined functions to take user input for the low range, high range and a do-while loop to take as many third inputs as the user wants (terminated by entering -1, as requested by the question)
To actually calculate if they're divisible, I found out that if A%B = 0, then they are divisible, so I thought I would create a loop where each value in the range between A and B is checked against the third value to see if they output a zero.
What I need to end up with is a program that tells the user how many integers are divisible by the numbers in the range, i.e: "Enter the low range value: 335 Enter the high range value: 475 Enter a value to check within the range: 17 Enter a value to check within the range: -1 There are 8 total values that are divisible by the numbers in the range." Going back to my original question, how would I create a loop or something to "check" how many values are equal to zero, and consequently increment a variable for each instance? (This is how I think it should be done)
Code:
#include <stdio.h> //GLOBAL DECLARATIONS int getlowR(); int gethighR(int);
/* Task: create a function that determines prime number and use it to print out prime numbers from 0-50: */
Function prototype: Should return a `bool` (because it answers a yes/no question) and should take in a single int (to ask whether it's prime).
- An int greater than 2 is prime if it is not divisible by any number between 2 and itself, not including itself. - Use this function to print out the primes less than 50. Check it against my list: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
#include <iostream> using namespace std; int prime(int&x) { if(x%2==1)
[Code] ....
It is printing out the correct prime numbers but also printing this between each number: 1629974960
The program I have is from a tutorial where the user enters two points on a line, and then the program calculates the mid-point and slope.
I want to modify it from it's initial form so that co-ordinates can be input in (x,y) fashion. Right now the user has to enter the x-coordinate, enter a space, and then enter the y-coordinate (lame...)
I saw people using SStream and using that to either write functions to ignore the comma or similar things for converting one file into an array, but not quite what I am trying here.
I am just starting out programming and have an assignment due in the near future. the problem is I cant seem to figure it out. I started it already but got stuck cant seem to figure it out. Here is the assignment.
Create a program that will generate a list of 200 random numbers (ranging from 1-1000) and determine the medium, mode, and average of the list of numbers. Have the program display the original list ant then display the list in ascending and descending order on the screen.
I'm trying to make a program that allows the user to input an arbitrary amount of numbers and finding the largest of all the inputs but I keep having problems with the output.
javascript:tx(' #include <iostream> using namespace std; //****************************************** //CLASS COMPARATOR //******************************************
class comparator { public: comparator();
[Code] .....
And regardless of what numbers I enter, I always get the output of 10. Also I got the EOF idea from my textbook so if there is a better way of doing this I'd like to hear it. I don't know any clear ways that looks nice to end the while loop when the user doesn't have any more numbers to enter.
I've made an effort for three days to write this code. But, my brain has stopped now. I wrote code to find the status of the game (win, loss or tie). However, I can't determine the tie status of the game. Tie status is the problem
I'm working with arrays that might have NULL bytes in them and I'm wondering how to determine the length of the array or store it somewhere with the array (strlen() won't work because of the NULL, right?).
I've found advice like store the length of the array in the first byte of the array, but since sizeof(size_t) is 8 should I leave the first 8 bytes for the length?
Would it be better do define my own structure which would store the array and its length? What's the usual way these things are handled in practice?
I am making an eVoting program which takes input from .txt file and outputs in the same .txt file. I need to ask the user to enter the candidate they wish to vote and then read the previous tally from the .txt file and add one to it. The problem is determining the numbers in a .txt file and adding one to it.
For example voting_Tally.txt contains:
Bloomberg 1234 Bill De Blasio 6789
How would it be possible to first determine the name and then add one to their tally. For Example:
I have a service A, and an application B. Service A needs to launch application B only if it's not running currently. This can be easily done in Windows by calling GetExitCodeProcess function. I cannot find an equivalent method for doing so in Linux/Mac.
So my current code says:
system("open /Users/adsmaster/client/client &"); // to launch the application from the service in a new shell
I read that on a Linux-line machine you can use $ cal to get the exit value of the recently run process but I am not sure how can get the exit value of a particular process?
I need to count how many times letter appears in a text. I know that for default letters from 'a' to 'z' and from 'A' to 'Z' there is an interval. But I need also Lithuanian letters, such as ž,č,ę. I wrote this method: (it accepts char code and checks whether that char is a letter)
As you can see, a lot of checking in switch statement. I use 256 for this reason to have the same effect as unsigned char. But maybe there is a way to shrink down this method, or use some library?
Is there any code I can use to determine my compiler version and which Standard It uses? I know the following code determine that my compiler followed ANSI But how about a version of that? ****My OS is now Ubuntu
Code: #include <stdio.h> int main(void){ printf("File :%s ", __FILE__ ); printf("ANSI :%d ", __STDC__ ); //return 1 if it follow ANSI but version? return 0; }
I am trying to write a simple application that resembles the game "Computer Co", but I am stuck with this problem: If the board is represented by a dimensional array, with what algorithm could I tell if one player's pieces are captivated by the other's? In other words, how can I recognise whether the pieces of one colour form a circle?
You are given an integer, perhaps a very long long integer, composed of only the digits 1 and/or 2. You have the ability to change a 1 digit into a 2 and a 2 digit into a 1 and must determine the min. number of changes that you can make resulting in no 2 digits remaining in the number that are in a position(in terms of powers of ten) higher than any 1 digit.
example:
2222212 number of changes:1 1111121 1 2211221 3 1122112 2
no negative numbers.
how to get started. Also I'm not allowed to use anything related to arrays or sorting.
I have a current assignment for C++ involving us to make a program to determine coin change for example if you input the number 127 you would need 2 half dollars 1quarter and 2 pennies I have no way how to program this.
This is my code that doesn't do what i want it to
#include <iostream> using namespace std; int main ( ) { float change; int half_dollars, quarters, dimes, nickels, pennies; // declare variables
Write a C++ program that can be used to determine grades at the end of the semester. For each student, who is identified by an integer number between 1 and 60, four examination grades must be kept. Additionally, two final grade averages must be computed. The first grade average is simply the average of all four grades. The second grade average is computed by weighting the four grades as follows: the first grade gets a weight of 0.2, the second grade gets a weight of 0.3, the third grade a weight of 0.3, an the fourth grade a weight of 0.2; that is computed as:
0.3*grade1 + 0.2*grade2 + 0.2*grade3 + 0.3*grade4
Using this info, you are to construct a 60X7 two dimensional array, in which the first column used for the student number, the next four columns for the grades, and the last two columns for the computed final grades. The output of the program should be a display of the data in the completed array.
Originally I had to create a simple integer palindrome program that looped while the user entered 5 digit inputs (entering -1 stopped the loop). I did this using a conversion to string, reading the length to determine if the length was valid, and then reading the string forward and backwards inside of a while loop. (snippet below)
while( digitsEntered != -1)//Allow user to quit by entering -1 to end the loop { ostringstream convert;//conversion stream convert << digitsEntered;//converted text from number goes in the stream convertedString = convert.str();//store the resulting conversion to convertedString
[Code] ....
The next stage of this program was to do the same thing with strings instead of integers. However, the option to end the loop by entering -1 is still a requirement.
I think the way to do this is to first determining whether the input is a string or an integer, and if it is a string then read it and if it's an integer determine if it's -1. However, whenever I write code to do this, it converts strings to 0 so the string is not stored and cannot be read to determine if it is a palindrome. Is there a way to determine the type of input without converting it into a different type i.e. read string and then keep string or read number and keep number?
Write a program that will prompt a user to enter a single character, the prompting will continue till a sentinel value is entered. For each character entered perform the following tests and print out a relevant message if the character passes the test. Print out a default message if the character does not pass any of the tests.
Tests that should be in program: Punctuation, Upper Case, Digit, White Space.
Sample Run: “A”, “a”, “7”, <tab>, “?”, “$”
So far I have the following code done. The problem is that when I run the program, the first character is correctly identified. However, every character afterwards is defined as a whitespace character.
#include <iostream> #include <cctype> #include <cstring> using namespace std; int main() { char input; char response;
In a fashion similar to that in Fig. 3.11(shown below), write a short program to determine the smallest number, xmin, used on the computer you will be employing along with this book. Note that your computer will be unable to reliably distinguish between zero and a quantity that is smaller than this number.