Question

In: Computer Science

for C++ I'm trying to write a code that asks for double values and counts how...

for C++

I'm trying to write a code that asks for double values and counts how many times 2 consecutive values differ by at most 1. The output on 0 should be the answer.

With inputs 1, 1.7, 0.8, -0.1, -1, 0 : it should return 4 but keeps giving me 0. This is the correct output because only 1 and 1.7 differ by at most 1 as does 1.7 and 0.8, 0.8 and -0.1, and -0.1 and -1. We do not check whether 0 is within 1 of the previous number because 0 is the input that causes us to exit.

Why is this if every time a number differs by at least one my if statement should n++?

#include <iostream>
using namespace std;

int main ()
{
   double n1, n2 = 0;
   int n = 0;

   cout << "Enter a number." << endl;
   cin >> n1;

   while (n1 != 0) {
       if ((n1 - n2) > -1 && (n1 - n2) < 1) {
           n++;
           n1 = n2;
       }
       else {
           cout << "Enter another number or 0 to end." << endl;
           cin >> n1;
       }
   }
   cout << n << endl;
  
    return 0;
}

Solutions

Expert Solution

Here is the corrected code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include <iostream>

using namespace std;

//method to return the absolute value of a number

//i.e. this method removes the sign of a number if it is negative

int abs(double n)

{

    //if negative, returning negative of that number, which will be positive

    if (n < 0) {

        return -n;

    }

    //otherwise, returning that number

    return n;

}

int main()

{

    double n1, n2 = 0;

    int n = 0;

    cout << "Enter a number." << endl;

    cin >> n1;

    //looping as long as n1 is not 0

    while (n1 != 0) {

        //prompting and reading next number

        cout << "Enter another number or 0 to end." << endl;

        cin >> n2;

        //checking if n2 is not 0

        if (n2 != 0) {

            //finding absolute value of n1-n2 and checking if it is under 1

            //i.e subtracting n2 from n1 and removing the sign, so we dont have to check for

            //both cases separately, and this value will be positive. if it is less than or

            //equal to 1, then incrementing n

            if (abs(n1 - n2) <= 1) {

                n++;

            }

        }

        //assigning n2 to n1

        n1 = n2;

    }

    //displaying n

    cout << n << endl;

    return 0;

}

/*OUTPUT*/

Enter a number.

1

Enter another number or 0 to end.

1.7

Enter another number or 0 to end.

0.8

Enter another number or 0 to end.

-0.1

Enter another number or 0 to end.

-1

Enter another number or 0 to end.

0

4


Related Solutions

Write a C program that asks the user to enter double values (the values can be...
Write a C program that asks the user to enter double values (the values can be positive, zero, or negative). After entering the first double value, the program asks "Would you like to enter another value?", and if the user enters Y or y, the program continues to get additional values and stores these values into a double array (for up to 100 values — use a symbolic #define constant to specify the 100 size limit). The program will need...
arduino c code only write a code that counts down a timer on serial monitor and...
arduino c code only write a code that counts down a timer on serial monitor and if A1 is typed into serial monitor prints the timer counting down and writes at 1 second hello and at 5 secs goodbye and repeats every 5 secs A2 is typed as above at 2 seconds writes hello and 3 seconds writes goodbye A3 same as above but at 3 seconds says hello and 2 seconds goodbye This continues until c is pressed to...
Hello! I'm trying to write a code that will either encrypt/decrypt a message from an inputed...
Hello! I'm trying to write a code that will either encrypt/decrypt a message from an inputed text. I'm having the absolute hardest time getting stared and figuring out how to identify in the code if the input needs to be encrypted/decrypted and identifying the string to encrypt/decrypt with. These are the instructions: Objectives Command line input File input and output Rethrowing exceptions Program Description Gaius Julius Caesar encoded his battle messages so that the opponent could not read them should...
C language <stdio.h> use double and const Write a program that asks the user for the...
C language <stdio.h> use double and const Write a program that asks the user for the radius of a circle (as a double) and displays the diameter, circumference, and area of the circle. Use a constant of 3.14159 as the value of pi. Have a blank line between the input and the output. Follow the 3 steps in the Information Processing Cycle - Input, Processing, and Output. The formulas needed are: diameter = 2 * radius circumference = 2 *...
I'm trying to write a feet to meters and centimeters program and I'm looking for a...
I'm trying to write a feet to meters and centimeters program and I'm looking for a little help so I can see how close I got. It should do the following: Write a convertToMetric class that has the following field: standard - holds a double, standard length value in feet. The class should have the following methods: Constructor - that accepts a length in feet (as a double) and stores it in the standard field. setStandard - accepts a standard...
• Write a C++ program that asks the user to input two integer values, then calls...
• Write a C++ program that asks the user to input two integer values, then calls a void function "swap" to swap the values for the first and second variable. • As we mentioned before, in order to swap the valors of two variables, one can use the following: temp= variable1; variable1 = variable2; variable2 = temp; • Display the two variables before you call swap and after you call that function. Comment in code would be greatly appreciated to...
Write a C++ program that asks the user for the values of three resistances. We suppose...
Write a C++ program that asks the user for the values of three resistances. We suppose that the user will not enter a negative value. The program then asks the user whether he has a series or parallel montage. The user will answer with S or s for series and P and p for parallel. (If the user enters a wrong answer, he will see a message on the screen alerting him, and the program will end.) Finally, the program...
I'm trying to get an understanding of this code as it was done in class, can...
I'm trying to get an understanding of this code as it was done in class, can someone please explain step by step what is happening? namespace BMI { class Program { static void Main(string[] args) { double height, weight; Console.Write("Enter person's height in inches: "); bool value1 = double.TryParse(Console.ReadLine(), out height); Console.Write("Enter weight in pounds: "); bool value2 = double.TryParse(Console.ReadLine(), out weight); if (!value1 || !value2) { Console.WriteLine("Not valid input."); } else { if (height < 5 || height >...
Hello. I'm trying to write down java code making stars whose numbers are determined by user...
Hello. I'm trying to write down java code making stars whose numbers are determined by user input on the console (each star's data are stored in ArrayList). the class extends JFrame and its color, size(Zoom in and Zoom out), and location are changing every time. Moreover, its angular rotation is changed at a certain determined degree. Does anyone know how to write down the java code of this question?
in C please! Write a code that asks user for input N and calculates the sum...
in C please! Write a code that asks user for input N and calculates the sum of the first N odd integers. There should be a "pure" method that calculates the sum separately that Main method should call when printing the output.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT