I want to make a program in which user enter the three digits separated by the space and i want to show the smallest digit.
See my code:
#include<iostream>
using namespace std;
int main( )
{
int a,b,c;
cout<<" enter three numbers separated by space... "<<endl;
cin>>a>>b>>c;
int result=1;
while(a && b && c){
result++;
a--; b--; c--;
}
cout<<" minimum number is "<<result<<endl;
system("pause");
return 0;
}
Sample input:
3 7 1
Sample output:
2
It doesn't show the smallest digit. What's the problem in my code and how can I solve my problem?
The result should be initialized by zero
int result = 0;
nevertheless the approach is wrong because the user can enter negative values.
The program could be written the following way
#include <iostream>
#include <algorithm>
int main( )
{
std::cout << "Enter three numbers separated by spaces: ";
int a, b, c;
std::cin >> a >> b >> c;
std::cout << "The minimum number is " << std::min( { a, b, c } ) << std::endl;
return 0;
}
There's a hidden assumption here that a, b and c are positive. If you allow for such assumption, you're nearly there - you just need to initialize result to 0 instead of 1.
hint:
When writing c++ always prefer to write code in terms of the algorithms that have been thoughtfully provided for you in the standard library.
#include <iostream>
#include <algorithm>
using namespace std;
int main( )
{
int a,b,c;
cout<<" enter three numbers separated by space... "<<endl;
cin>>a>>b>>c;
int result = std::min(a, std::min(b, c));
cout<<" minimum number is " << result<<endl;
system("pause");
return 0;
}
Much pain, it will prevent. More productivity, it will produce.
;)
Related
I wanted to solve a problem, yet my code passes only 8 out of 10 tests. The problem is to determine whether a number 1<=N<=10^9 can be a numeric polyndrome. The thing is, you may add as many leading zeros as it requires to make a non-polyndrome into a polyndrome. If it is possible, or a number is polyndrome, the result must be yes, otehrwise no. For example, 2020 is not a polyndrome, but If I add a leading zero, it becomes 02020, which is a polyndrome. One main problem of my code is that i don't know the number of leading zeros needed to make a number a polyndrome. Here's my code:
#include <cstdio>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string N, N2;
cin >> N;
N2 = N;
reverse(N.begin(), N.end());
if (N2 == N) {
cout << "Yes" << "\n";
return 0;
}
else {
N2 = "0" + N2;
N = N + "0";
if (N != N2) {
cout << "No" << "\n";
return 0;
}
else {
cout << "Yes";
return 0;
}
}
}
I would be grateful for any help to enhance my code!
edit: I have to add leading zeros if it can turn a number into a polyndrome, that's the main thing
I would do something like:
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
string n;
cin>>n;
string m =n;
reverse(n.begin(),n.end());
if(n==m){
cout<<"Yes"<<endl;
return 0;
}
int count=0;
int k=m.length();
for(int i=1; i<=k;i++){
if(m[i]=='0'){
count+=1;
}
}
for(int i=1;i<=count;i++){
m='0'+m;
}
string m2=m;
reverse(m.begin(), m.end());
if (m2 == m)
{
cout << "Yes" << endl;
return 0;
}
else{
cout << "No" << endl;
return 0;
}
}
Just to be complete, I will show you the answer based on the comments.
It is the same, if you add 0es to the beginning or remove them from the end.
If you have 2020, you can either add one leading 0 --> 02020 or remove a trailing 0 -> 202. Result is the same.
So, the algorithm will be.
Search from the right end for the first character that is not '0'
Build a pair forward and a pair reverse iterators, based on the iterator, found by `std::find
Compare temporary created strings, build from forward and reverse iterators
Output comparison result
Very simple example code:
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
int main() {
if (std::string numberAsString{}; std::cin >> numberAsString) {
std::string::reverse_iterator newEnd = std::find_if(numberAsString.rbegin(), numberAsString.rend(), [](const char c) {return c != '0'; });
std::cout << (std::string(numberAsString.begin(), newEnd.base()) == std::string(newEnd, numberAsString.rend()) ? "Yes\n" : "No\n");
}
}
I only changed in main else part of your code.
1. Now understand if some numerical string is not palindrome and you are going to make it palindrome by adding leading 0's then it must end with 0's.
For Ex. 100 is not palindrome but you can make it by 00100 it possible because two 0's present at end.
same with 10 -> 010
2. some numbers which are ends with 0's and not palindromes but we can make them by adding leading 0's
10, 20, 30.....90 -> (respective palindrome) 010,020,.....090
100, 110, 200, 220,.....900, 990 -> (respective palindrome) 00100, 0110, 00200, 0220.....00900, 0990
3. some numbers which are ends with 0's and not palindromes and we can't make them by adding leading 0's
120, 130...190
210, 230, 240,....290
.
.
.
11010
4.
Now if you see carefully the numbers which we can make palindrome you get that to be able to do so we have to add exact number of 0's at starting as present at the end. if you pause for minute and think then you get logical sense too.
5.
Now I am creating condition to right code on above analysis. I will talk about just else part
i> So I will check first that how many 0's present at end of number(while loop). at least one 0's must be present.
ii> Then I am adding as many leading 0's present at end.( for loop).
iii> Then storing N2 in N3 so I can reverse and check palindrome become or not because all numbers end with 0's do not become palindromes.
Code
#include <iostream>
#include <algorithm>
#include<string>
using namespace std;
int main()
{
string N, N2;
cin >> N;
N2 = N;
reverse(N.begin(), N.end());
cout<<N<<" "<<N2<<"\n";
if (N2 == N)
{
cout << "Yes" << "\n";
return 0;
}
else
{
int k=N2.length(), count=0, i=1;
while(N2[k-i]=='0')
{
count++;
i++;
}
if(count==0)
{
cout<<"No";
return 0;
}
for(int i=1; i<=count; i++)
{
N2="0" + N2;
}
string N3=N2;
reverse(N2.begin(), N2.end());
cout<<N3<<" "<<N2<<"\n";
if (N3 != N2)
{
cout << "No" << "\n";
return 0;
}
else
{
cout << "Yes";
return 0;
}
}
}
Also just note that you added <cstdio> two times even it didn't required one time also. Sometime if you include same header file twice it might cause error. Also there is no requirement of <cmath> library file. By the way you didn't include <string> library file might be in some cases your program run quite well without including it but it is not standard practise.
My task is to find the minimum number between n input values that the user should enter in an infinite loop until a certain number or character is entered to stop the loop.
The problem that I am facing is, I can't get the condition that tests the input to see which of the entered numbers is the smallest to work. Also, a second problem is, I want to end the loop with a char not an int, but I don't know if that is even possible.
I searched online but I can't find any answers.
Side note: I am new to C++. I am using Borland C++ v5.02.
#include <iostream>
#include <conio.h>
int I, min =0;
cout<<"Enter a number :";
do{
cin >> I;
if (I < min){
if (I > 0){
min = I;
}
}
}while (I > -1);
cout << min;
I solved your problem by using a try-catch block and stoi().
stoi() is used to convert a string into a number. If the number input is not convertible (meaning that a char is entered and the loop should break), const std::invalid_argument & e is catch and automatically break the loop.
#include <iostream>
using namespace std;
int main()
{
int Min = INT_MAX; string I; int x;
do
{
cout << "Enter a number or a char : ";
cin >> I;
try
{
x = stoi(I);
if (x < Min)
{
if (x > 0) {Min = x;}
}
}
catch(const std::invalid_argument & e) {break;}
}
while(x > 0);
cout << "Minimum positive number entered : " << Min;
}
Output:
Enter a number or a char : 10
Enter a number or a char : 8
Enter a number or a char : 5
Enter a number or a char : 7
Enter a number or a char : a
Minimum positive number entered : 5
As your code is a bit unclear, I changed both constraint to I>0, and you can easily modified this bit.
For the prolem with INT_MAX, maybe #include <climits> or #include <limits.h> will help, as specified here. If the problem persist, the workaround is to set Min to something high, for example 10^9.
*Note: Ran on Code::Blocks 20.03, Windows 10 64-bit.
the problem with the code was with the header I couldn't find a one that was working with my compiler Borland v5.02 c++ but thanks to #JerryJeremiah he leads me to it.
also, I redeclared the min =INT_MAX; because I am using this code in a loop.
the code is working with me now.
#include <iostream>
#include <conio.h>
#include <limits.h>
int main()
{
int I, min =INT_MAX;
cout<<"Enter number of input :";
do{
cin>>I;
if (I<min ){
if(I>0){
min =I;
}
}
}while(I>-1);
cout<<min;
min =INT_MAX;
getch();
}
I need to be able to input a series of integers on the same line, I cannot make them characters because I need to add the integers up.
#include <iostream>
#include<string>
using namespace std;
int main(){
int a; //1
int b; //2
bool sit = true;
cout << "Enter a ten digit date" <<endl;
cin>>a>>b;
cout<<a<<b<<endl;
if (sit == true){
b = b+a;
cout << b<<endl;
}
return 0;
}
So if I enter
12
it waits till I enter another two digits then adds both two digit numbers.
12
45
57
What can I do?
Thanks
i dont quite understand your problem , but this is how you read a series of integers .
int x;
cin>>x;
//keep reading x until 0 is entered
while(x!=0)
{
//do whatever you need here
cin>>x;
}
if you knew how many integers you had to read , you could use a for loop.
for (int i=0;i<n;i++)
{
cin>>number;
}
I have to make a program that asks for the number of rows and number of columns and prints a rectangle based on these values.
The only clue I got was:
Note that char tkn can be used to declare a character.
And I received an example of how input and output should look:
The number of lines: 3
The number of columns: 4
Which characters do you want to use: #
####
####
####
This is what I got so far (I'm just guessing how to do the char bit at the moment):
#include <iostream>
using namespace std;
int main()
{
int lines, columns, character;
char tkn;
cout<<"The number of lines: ";
cin>>lines;
cout<<"The number of columns ";
cin>>columns;
cout<<"What character do you want to use? ";
cin>>tkn;
cin.ignore();
getchar();
return 0;
}
Add header
#include <iomanip>
and include the following loop
std::cout << std::setfill( tkn );
while ( lines-- )
{
std::cout << std::setw( columns + 1 ) << '\n';
}
Also it would be better to use identifier character (or simply c) or filler instead of this strange identifier tkn
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int lines, columns, character, i=0, l=0;
char tkn;
cout<<"The number of lines: ";
cin>>lines;
cout<<"The number of columns ";
cin>>columns;
cout<<"What character do you want to use? ";
cin>>tkn;
for(l=0;l<lines;l++;)
{
{
cout<<tkn;
}
for(i=0;i<columns;i++)
{
cout<<tkn;
}
cout<<endl;
}
cin.ignore();
getchar();
return 0;
}
The loop in the function require() takes 3 conditions, a > b or "a" or "b" aren't digits. Even when I don't satisfy the conditions and put 2 integers in, it just loops once again.
Also when I put in a character then it just endlessly loops "Enter minimum number Enter maximum number" ignoring the cins. Anyone know why? I'm a beginner so this is probably really obvious
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int random(int minN, int maxN) //generates random number within specified range
{
srand (time(NULL));
int x = (maxN - minN);
int y = minN + (rand() % (x+1));
return y;
}
int require() //makes sure a < b and both are digits
{
int a,b;
do {
cout << "Enter minimum number" << endl;
cin >> a;
cout << "Enter maximum number. Note: Has to be greater or equal to minimum." << endl;
cin >> b;
} while (a > b || !isdigit(a) || !isdigit(b));
return random(a,b);
}
int main()
{
cout << require() << endl;
}
You should not use isdigit as this relates to a particular character is a digiti. Instead the loop should look like this:
int require() //makes sure a < b and both are digits
{
validNumbers = true;
do
{
cout << "Enter minimum number" << endl;
cin.clear();
cin >> a;
} while (cin.fail());
do
{
cout << "Enter maximum number. Note: Has to be greater or equal to minimum."
<< endl;
cin.clear();
cin >> b;
} while (cin.fail() || a > b);
return random(a,b);
}
PS: You only need to call srand (time(NULL)); once at the start of the program.
You are reading the numbers as, well, numbers not as characters as the isdigit function expects. If you are using a C++11 compliant standard library, the values of a and b will actually be zero if the input is not valid integer numbers, which means that e.g. !isdigit(a) will be true. If you are using a non-C++11 library, then the value of a and b will be random, and will most likely cause !isdigit(a) to be true as well as the amount of valid digit ASCII values in a full 32-bit integer range is quite small.
If you read a reference about the input operator, like this one you will see that if extraction fails, then the streams failbit will be set. This can either be tested "inline" like this:
if (!(std::cin >> a))
{
std::cout << "Not a valid number, try again: ";
continue;
}
Or it can be tested using the streams fail function.