0

How to replace numbers in c++

Can anyone help me solve this question? The keypad on your oven is used to enter the desired baking temperature and is arranged like the digits on a phone: 1 2 3 4 5 6 7 8 9 0 Unfortunately the circuitry is damaged and the digits in the leftmost column no longer function. In other words, the digits 1, 4, and 7 do not work. If a recipe calls for a temperature that can’t be entered, then you would like to substitute a temperature that can be entered. Write a program with function/s that inputs a desired temperature. The temperature must be between 0 and 999 degrees. If the desired temperature does not contain 1, 4, or 7, then output the desired temperature. Otherwise, compute the next largest and the next smallest temperature that does not contain 1, 4, or 7 and output both. For example, if the desired temperature is 450, then the program should output 399 and 500. Similarly, if the desired temperature is 375, then the program should output 380 and 369.

6th Jan 2019, 9:52 AM
Zain Asif
Zain Asif - avatar
10 Respuestas
+ 1
HonFu Did you read what I said? It was only the beginning, and doesn't help too much. That's why I told him to analyze and try to understand it also. I explained how it could work. There are multiple answers, so not only mine is right and it isn't optimized either. I basically guided him closer to the solution and gave him a direction.
6th Jan 2019, 4:46 PM
Rain
Rain - avatar
+ 1
It's fine. I know exactly what you are referring to. I usually tell those people to study and do it themselves. In rare cases, I'll help them with the whole solution. For example, someone needed help with a project for college. His degree was not programming related, so he had no clue what he was doing, just how he wanted it done, so I helped him out. I would appreciate help like that if I had to do something not in my degree/main focus.
6th Jan 2019, 5:09 PM
Rain
Rain - avatar
+ 1
Tbh I didn't tell him to convince me. I started doing it without explanation, and then he told me that it was for his degree that had nothing to do with programming, mid way through when I already helped a lot.
6th Jan 2019, 5:29 PM
Rain
Rain - avatar
+ 1
Zain Asif How about you say that next time in your question? Lol.
6th Jan 2019, 6:51 PM
Rain
Rain - avatar
0
#include <iostream> using namespace std; int main(){ int num; do{ cout << "Enter Number: "; cin >> num; cout << num << endl; } while(num < 0 || num > 999); int digits[3]; digits[2] = num % 10; digits[1] = (num / 10) % 10; digits[0] = (num / 100) % 10; for(int i = 0; i < 3; i++){ cout << digits[i] << endl; } return 0; } ^ Hopefully this helps. Gives you the individual numbers. Analyze and see how it works. Once you do, use the numbers to find your answer. I'd recommend checking each digit in the array using switch statement. If found, Put it through a function to find upper and lower.
6th Jan 2019, 10:32 AM
Rain
Rain - avatar
0
Rain, we're supposed to help people solve a problem on their own here, not dish out the solution
6th Jan 2019, 10:55 AM
HonFu
HonFu - avatar
0
Someone else told me before I may have exaggerated here. We've got dozens of 'do my homework' requests here daily and after a while of reading that, you get a bit 'hyper-responsive' to the issue. Sorry if I did you injustice here, I hope you understand where it came from.
6th Jan 2019, 5:05 PM
HonFu
HonFu - avatar
0
Yeah, that sounds like an understandable case. Problem is, we don't know the truth here, really. In a place like this, when it helps your purpose, a sad story is quickly woven.
6th Jan 2019, 5:24 PM
HonFu
HonFu - avatar
0
Yeah, over time you pick up a few techniques to assert an honest question. ;)
6th Jan 2019, 5:38 PM
HonFu
HonFu - avatar
0
Rain HonFu your answer didn't help me a bit because I'm not supposed to use arrays and switch but thanks anyway i managed to do it on my own. Here's my solution: int num=0;    cout<<"Enter Temperature: ";    cin>>num;    int num2=num,number=0;    int digit2= (num/10)%10;    int digit3= num%10;    if(num/100==1 || num/100==4 || num/100==7 )    {        num=num/100-1;        num=num*100+99;        num2=num2/100 + 1;        num2=num2*100;        cout<<num<<"   "<<num2;    }    else if(digit2==1 || digit2==4 || digit2==7 )        {            int a=digit2;            digit2=digit2+1;            digit2=digit2*10;            num=num/100;            num=num*100+digit2;            a=a-1;            a=(a*10)+9;            num2=num2/100;            num2=num2*100+a;            cout<<num<<"   "<<num2;        }    else if(digit3==1 || digit3==4 || digit3==7)    {        num=num+1;        num2=num2-1;        cout<<num<<" "<<num2;    }    else    {        cout<<num;    }
6th Jan 2019, 6:44 PM
Zain Asif
Zain Asif - avatar