Skipped condition and I don't know why [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 was debugging this, and debugger skipped the last 'if' even 'sum' was equal to 'n' and jump straight to 'else', I don't know why. Please help.
P/s: Can I use dynamic array to increase the mobility of my program?
#include <iostream>
#include <math.h>
using namespace std;
int exponent_of_10(); // set position for digits
int exponent_of_10(int a, int b){
for(int j = b; j>0;j--)
{
a *= 10;
}
return a;
}
main() //check if the number was palindromic
{
int n;
int a[6]={0,0,0,0,0,0};
int i = 0;
int temp;
int S;
cout<< "Input n (maximum of 6 digits): ";
cin>> n;
do
{
if(n<1)
{break;}
temp=n%10;
a[i]=temp;
n=(n-temp)/10;
i++;
}
while (n!=0);
int sum = 0;
for(int j=0; j<=5; j++)
{
exponent_of_10(a[j],j);
S = exponent_of_10(a[j],j);
if (S==0)
{break;}
sum +=S;
}
if(sum==n)
{
cout<< "Congratz, this is PALIDROMIC NUMBER !!";
}
else
cout<< "Sad life, this is NOT palidromic number";
return 0;
}

When the code exits the do ... while() loop, n is 0. For the test in the if to be meaningful, the code should save the original value of n somewhere and compare sum to that original value.

Related

how to compare unknown elements of an array in c++? [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 26 days ago.
Improve this question
Sorry for bad English .
I was trying to write a program that gets a number and see if the digits of an entered number are repeated or not . I did try to if(analyse[0]==analyse[1]==analyse[2]==...) but since I don't know exactly how many elements will array have, it didn't work
#include<iostream>
int main(){
int number,number_help;
const int count{10};
std::cin>>number;
number_help = number ;
int digitcount{0};
while(number_help>0){
number_help/=10;
digitcount+=1;
}
int analyse[count]{};
for(size_t i {0}; i<digitcount ; i++){
analyse[i] = number%10;
number/=10;
}
//I don't know what to code here
return 0;
}
Change your approach: count how many there are of each digit instead of comparing them to each other.
This is much simpler.
Example:
#include<iostream>
int main(){
int number;
std::cin >> number;
const int count{10};
int frequency[count]{};
do {
frequency[number % 10] += 1;
number /= 10;
} while (number != 0);
for (int i{0}; i < count; i++) {
if (frequency[i] > 1) {
std::cout << i << " was repeated " << frequency[i] << " times.\n";
}
}
}

what will bw the output of this code? c++ [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 last year.
Improve this question
#include <iostream>
using namespace std;
int f(int i){
int k=0;
if(i>0)
{
int k=i*10;
}
else {
int k= i++;
}
cout <<k;
return i;
}
int main()
{
cout << f(1);
cout << ".";
cout << f(0);
return 0;
}
This is the code, compiler shows "01.01" which i quite don't understand, any help will be very much welcomed!
int k = i * 10; and int k = i++; are declarations of k that shadow the outer k. The statement std::cout << k; in the outer scope therefore always outputs zero.
The only effect of the if body is to increase i by 1. And it only does that if i is zero (or less). That value of i is returned printed.
Thus the output is 01.01. Armed with a line by line debugger, the shadowing effect will be obvious.

why run time error ! SIGFPE? [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 5 years ago.
Improve this question
4 of the samples shows ok.. Others are not running !
Problem Link: https://www.hackerearth.com/challenge/competitive/programming-indiahacks-2017/algorithm/hacker-with-prime-bebe28ac/
#include <iostream>
using namespace std;
int main()
{
long N,A[100000],Q,X[10000],R,p;
cin>>N>>Q;
for (int i=0;i<N;i++){
cin>>A[i];
}
for (int i=0;i<Q;i++){
cin>>X[i];
}
for (int i=0;i<Q;i++){
R=0;
bool isprime=false;
for (int j=0;j<N;j++){
R = (X[i]/A[j])*A[j];
if (R==X[i])
{
for (int k=2;k<=R/2;k++)
{
if(R%k==0){
R=0;
};
};
if(R==0){
cout<<"YES"<<endl;
isprime=true;
break;
}
else {
isprime=false;
}
}
}
if(isprime==false){
cout<<"NO"<<endl;
}
}
return 0;
}
My psychic powers suggest you have input that will trigger your code to attempt to divide by zero.
For example:
R = (X[i]/A[j])*A[j];
if(R%k==0){
If A[j] is zero in the first line, or if k is zero in the second line... that might be the cause....

C++ unclear output [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 7 years ago.
Improve this question
I'm trying to find all the divisors of a number (n) and to add to the array those divisors that are at the 1st power (that appear only once), but I get in output just zeros, what's wrong with my code?
#include<iostream>
using namespace std;
int k,A[100000],n,p,d=2,pozitia=0;
int main()
{
cin>>n;
while(n>1)
{
p=0;
while(n%d==0)
{
p=p+1;
n=n/d;
}
if (p==1) { A[pozitia]=d; pozitia++; }
d=d+1;
}
for (int i=0;i<=pozitia;i++) cout<<A[pozitia]<<" ";
return 0;
}
You print always the same value:
for (int i=0;i<=pozitia;i++)
cout<<A[pozitia]<<" ";
It should be
for (int i=0;i<pozitia;i++)
cout<<A[i]<<" ";
Also pay attention that it should be i<pozitia and not i<=pozitia because you increment pozitia each time you insert a new value so at the end pozitia will point to a not initialized value in A.
I couldn't follow your logic for computing the divisors. It seems to be much simpler than you make it out to be.
int stop = n/2 + 1;
for ( ; d < stop; ++d )
{
if ( n % d == 0 )
{
A[pozitia]=d;
pozitia++;
}
}
Here's a program that uses that logic.
#include<iostream>
using namespace std;
void printDivisors(int A[], int pozitia)
{
for (int i=0;i<pozitia;i++) cout<<A[i]<<" ";
}
void fun(int n)
{
int A[100000];
int d = 2;
int pozitia=0;
int stop = n/2 + 1;
for ( ; d < stop; ++d )
{
if ( n % d == 0 )
{
A[pozitia]=d;
pozitia++;
}
}
printDivisors(A, pozitia);
}
int main()
{
int n;
cin>>n;
fun(n);
return 0;
}
The output for input of 100:
2 4 5 10 20 25 50

how to get the last value of a vector [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 8 years ago.
Improve this question
i want to say if the value of the last element in the vector = 4 cout<<"Yes" how can i write the code write if(v.size()==y) where y is a number but it dosen't work i begin recently at writing codes
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int main()
{
int n,x,y;
cin >> n >> x >> y;
vector<int> v(n);
for(int i = 0; i < n; i++)
{
cin >> v[i];
}
for(int i = 0; i < n; i++)
{
if(v[0] == x)
{cout<<"EASY";
return 0;
}
if(v[0] == x && v.back() == y)
{
cout << "BOTH";
return 0;
}
if(v.back()==y)
{
cout << "HARD";
return 0;
}
if(v[0] != x && v.back() != y)
{
cout << "OKAY";
return 0;
}
}}
v.size() will returns the number of the elements in v, not the last element in the vector.
To get the last value of a vector, you can simply call std::vector::back().
Of course, you have to make sure the vector is not empty before that by checking std::vector::empty.
if (!v.empty() && v.back()==y)
{
...
}