Finding the maximum in given outputs [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have written a code to find the largest palindrome formed by multiplication of two 3-digit numbers. However instead of just getting the desired answer i.e. the largest palindrome, I am getting the list of all possible palindromes. How do I program it to find the largest.
Code:
#include <iostream>
using namespace std;
int revfunc(int x) {
int rev = 0, num, d;
num = x;
while(num != 0) {
d = num % 10;
rev = (rev * 10) + d;
num = num / 10;
}
long int maxi = 0;
if(x == rev && maxi < x) {
maxi=x;
cout<<maxi<<endl;
}
}
int main() {
long int ans;
for(int i = 100; i <= 999; i++) {
for(int j = 100; j <= 999; j++) {
ans = i * j;
revfunc(ans);
}
}
cin.get();
return 0;
}

In your program you don't actually select the maximum palindrome, you just dump them all. Here is minimal correction for code to work:
bool revfunc(int x){
int rev = 0, num, d;
num = x;
while (num != 0){
d = num % 10;
rev = (rev * 10) + d;
num = num / 10;
}
long int maxi = 0;
return x == rev&&maxi < x;
}
int main()
{
int max_palindrome = 0;
long int ans;
for (int i = 100; i <= 999; i++){
for (int j = 100; j <= 999; j++){
ans = i*j;
if (ans > max_palindrome && revfunc(ans))
{
max_palindrome = ans;
}
}
}
cout << max_palindrome;
cin.get();
return 0;
}

Related

print condition while integer overflow not working

#include <iostream>
using namespace std;
int getFectorial(int n)
{
int ans = 1;
for (int i = n; i >= 1; i--)
{
ans = ans * i;
}
return ans;
}
int printNcr(int n, int r)
{
if (getFectorial(n) > INT_MAX)
{
return 0;
}
return (getFectorial(n)) / ((getFectorial(r)) * (getFectorial(n - r)));
}
int main()
{
int n = 14;
for (int row = 0; row < n; row++)
{
for (int col = 0; col < row + 1; col++)
{
cout << printNcr(row, col) << " ";
}
cout << endl;
}
return 0;
}
When I give value of n more than 13th I want integer overflow condition should be working that given in printNcr() function, but it's not working and all line after 13th are printing wrong values instead of returning false.
How to make given INT_MAX condition work?
int oveflow cannot be reliably detected after it happens.
One way to detect upcoming int overflow in factorial:
int getFactorial(int n) {
if (n <= 0) {
return 1; // and maybe other code when n < 0
}
int limit = INT_MAX/n;
int ans = 1;
for (int i = 2; i <= n; i++) {
if (ans >= limit) {
return INT_MAX; // Or some other code
}
ans = ans * i;
}
return ans;
}
Another way is at startup, perform a one-time calculation for maximum n. With common 32-bit int, that limit is 12.
int getFactorial(int n) {
if (n > getFactorial_pre_calculated_limit) {
return INT_MAX;
}
...
You can detect overflow by watching for negative value
int getFectorial(int n)
{
int ans = 1;
for (int i = n; i >= 1; i--)
{
ans = ans * i;
if (ans < 0) <<<<======
return -1;
}
return ans;
}
then
int printNcr(int n, int r)
{
if (getFectorial(n) < 0)
{
return 0;
}
return (getFectorial(n)) / ((getFectorial(r)) * (getFectorial(n - r)));
}
Please note though that strictly speaking this is undefined behavior. It would be better to simply fail if you know the result is going to be too big (Ie n > 13)
Or better do it like this
int getFectorial(int n)
{
long long ans = 1; <<<====
for (int i = n; i >= 1; i--)
{
ans = ans * i;
if (ans >INT_MAX) <<<<======
return -1;
}
return (int)ans;
}
or you could throw std::overflow_error
BTW the word is factorial not fectorial

Insert the number in the array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
This is the main code:
int main(){
unsigned d=0, x, a[100]={0};
cout << "Input the value of x: ";
cin>>x;
caricaArray(a,x,d);
for(unsigned i=0 ; i<d ;i++)
cout<<a[i]<<endl;
return 0;
}
I need to make a function the inserts in the array all the number between 0 to 100 that are divisible by the integer x.
I tried with my code:
void caricaArray(unsigned a[], unsigned x, unsigned d){
for(int i = 1; i < 100; i++ )
for(int j = 0; j < 100; j++ ){
if( i % x == 0 ){
d++;
a[j] == i;
}
}
}
Is there anyone here has another insight?
thank youu
In other words, you are storing multiples of x into the array, between x and 100.
unsigned int index = 0U;
for (unsigned int i = x; i < 100; i = i + x)
{
a[index++] = i;
}
A number that is evenly divisible by x is a multiple of x.
Edit 1: Division.
If you don't like multiplication, you could use division.
unsigned int array_index = 0U;
for (unsigned int i = 1; i < 100; ++i)
{
if ((i % x) == 0)
{
a[array_index++] = i;
}
}
In both of the above examples, you can replace the array assignment by printing the value.

Get the smallest number from given digit [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
i'm trying to get the smallest and the biggest number possible by rearanging the 3 digit number.
So i success get the biggest
int maxNumFromNum(int num) {
int freq[10] = {0};
string str = to_string(num);
for (int i = 0; i < str.length(); i++)
freq[str[i] - '0']++;
int res = 0, mul = 1;
for (int i = 0; i <= 9; i++) {
while (freq[i] > 0) {
res = res + (i * mul);
freq[i]--;
mul = mul * 10;
}
}
return res;
}
But now i'm trying to get the smallest one. How can be done that.
Example Outpots:
321 Output> 123, 321
598 Output> 985, 598.
And too, is there more efficiency way to do that?
Thanks in advice.
A simple solution:
#include<bits/stdc++.h>
using namespace std;
int main() {
int n = 231;
string s = to_string(n);
sort(s.begin(), s.end());
cout<<"Smaller: "<<s<<endl;
reverse(s.begin(), s.end());
cout<<"Bigger : "<<s<<endl;
}
It works for integers of any size and runs in O(n lg(n)), where n is the number of digits of the integer.
You could simply change the line:
for (int i = 0; i <= 9; i++) {
to
for (int i = 9; i >= 0; i--) {
void getBigAndSmall(string num, string &outBig, string outSmall){
outBig = 0;
outSmall = 0;
//since you're sorting digits you can use this to sort them with O(n)
//this would be faster than std::sort in this case.
int bigArray[10] = {0};
int smallArray[10] = {0};
for (int i = 0; i < 10; i++){
int digit = (int)num[i] - 48; //this might not be good idea.
bigArray[digit]++;
smallArray[digit]++;
}
int digitsSize = num.size();
for (int i = 0; i < digitsSize; i++){
for (int j = 0; j < 10; j++){
if (bigArray[j] > 0){
bigArray[j]--;
outBig += j;
}
}
for (int j = 9; j > -1; j--){
if (smallArray[j] > 0){
smallArray[j]--;
outSmall += j;
}
}
}
}
This might have some mistakes, since I didn't test it, but it has a runtime O(n).
It gives max integer using input digits without using string
int extractMax(int num) {
std::vector<int> digits;
bool flag = false;
int curDigit;
while (num > 0) {
curDigit = num % 10;
num /= 10;
flag = false;
for (int i = 0; i < digits.size(); ++i) {
if (digits[i] > curDigit) {
flag = true;
digits.insert(digits.begin() + i, curDigit);
break;
}
}
if (not flag) digits.push_back(curDigit);
}
std::sort(digits.begin(), digits.end());
int dec = 1, int_val = 0;
for (auto& it : digits){
int_val += it * dec;
dec *= 10;
}
return int_val;
}
If you want to get smallest integer, change this line:
std::sort(digits.begin(), digits.end());
With this:
std::sort(digits.begin(), digits.end(), greater<int>());

How to fix segmentation fault in this program [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Im solving this question but cannot get correct output it show segmentation fault
https://www.hackerearth.com/practice/algorithms/graphs/breadth-first-search/practice-problems/algorithm/agitated-chandan/description/
This is the code that i write for this program
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int visited[100001], dist[100001];
int largest;
int t, x, y, w, i, z, k;
int large;
cin >> t;
while (t--)
{
int large = 0;
int n;
cin >> n;
vector<pair<int, int>> adj[n];
for (i = 1; i < n; i++)
{
cin >> x >> y >> w;
adj[x].push_back(make_pair(y, w));
adj[y].push_back(make_pair(x, w));
}
queue<int> q;
for (k = 1; k <= n; k++)
{
q.push(k);
for (i = 1; i <= n; i++)
{
visited[i] = -1;
dist[i] = 0;
}
largest = 0;
while (!q.empty())
{
z = q.front();
visited[z] = 1;
q.pop();
for (i = 0; i < adj[z].size(); i++)
{
if (visited[adj[z][i].first] == -1)
{
dist[adj[z][i].first] = dist[z] + adj[z][i].second;
if (largest < dist[adj[z][i].first])
{
largest = dist[adj[z][i].first];
}
q.push(adj[z][i].first);
visited[adj[z][i].first] = 1;
}
}
}
if (large < largest)
{
large = largest;
}
}
if (large < 100)
{
cout << 0 << " ";
}
if (large > 100 && large < 1000)
{
cout << 100 << " ";
}
if (large > 1000 && large < 10000)
{
cout << 1000 << " ";
}
if (large > 10000)
{
cout << 10000 << " ";
}
cout << large;
}
}
I expect output be
0 8
Just heck the logic is it write or wrong
One possible cause is that you store indexes from 1 to n to the queue q:
for (k = 1; k <= n; k++) {
q.push(k);
and then use them (as z) for indexing adj of size n:
while (!q.empty()) {
z = q.front(); // z equals n in the first iteration here
...
for (i = 0; i < adj[z].size(); i++) // adj[n] will be accessed
Indexes in C++ are zero-based, therefore the valid ones are from 0 to n-1.

Process terminated with status -1073741510? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have the following code that compiles without any errors but does not run. Can someone tell me the problem ?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int kenken[4][4];
kenken[2][2] = 3;
for(int i = 0; i < 5; i++)
for(int j = 0; j < 5; j++)
for(int k = 0; k < 5; k++){
if(i + j + k == 9){
kenken[0][0] = i;
kenken[0][1] = j;
kenken[0][2] = k;
}
if(i * j * k == 6){
kenken[1][0] = i;
kenken[2][0] = j;
kenken[3][0] = k;
}
if(abs(i - j)== 3){
kenken[1][1] = i;
kenken[1][2] = j;
}
if(abs(i-j) == 2){
kenken[3][1] = i;
kenken[3][2] = j;
}
if(i/j == 2){
kenken[0][3] = i;
kenken[1][3] = j;
}
if(i * j * k == 12){
kenken[2][2] = i;
kenken[2][3] = j;
kenken[3][3] = k;
}
cout << kenken[0][0] << " " << kenken[1][0]
<< kenken[2][0] << " " << kenken[3][0]
<< "\n\n";
}
return 0;
}
When j==0 you're going to get a division by zero error here:
if(i/j == 2){
As Ben suggested, a fix would be:
if( i == 2*j ) {