When I compiled in devc++ compiler, output is not printed. Is there any logical error? This program should accept a number from the user, print the lowest palindrome greater than the given number.
It is a problem from SPOJ PALIN
include
using namespace std;
int main()
{
int t;
cin>>t;
cout<<endl;
while(t--)
{long long int i;
long long int k;
int flag=1;
long long int n;
int a[10000000];
cin>>n;
n++;
start:
i=0;
while(n!=0)
{
a[i]=n%10;
i++;
n=n/10;}
i--;
k=i;
for(int j=0;j<=k;j++)
{
if(a[i]!=a[j])
{flag=0;break;}
else
i--;
}
if(flag==1)
cout<<n<<endl;
else
{n++;goto start;}
}
return 0;
}
You have not initialized the flag variable. if(a[i]==a[j]) then the flag will remain uninitialized and may create problems while cheking the if condition unless the default value will be 0 for it.(I am not sure that it would be). Also I can see that the value of flag will never turn 1 according to your code.
#include<iostream>
using namespace::std;
int main()
{
int t;
cin>>t;
cout<<endl;
while(t--)
{
long int i, k, n; int a[1000]; int flag = 1, duplicaten;
cin>>n;
n++;
start:
//cout<<"inside start : n = "<<n<<"\n";
flag=1; i=0;
duplicaten=n;
while(n!=0)
{
a[i]=n%10;
i++;
n=n/10;
}
n=duplicaten;
i--;
k=i;
//cout<<"number of digits of n = "<<i+1<<"\n";
for(int j=0;j<=k;j++)
{
if(a[i]!=a[j])
{
flag=0;
break;
}
else
i--;
}
//cout<<"flag = "<<flag<<endl;
if(flag==1)
{
//cout<<"FOUND!!\n";
cout<<n<<endl;
}
else
{
n++;
//cout<<"NOT found\n";
goto start;
}
}
return 0;
}
You can change the size of your data types yourself and atleast vote up this answer.
< Output before using duplicaten : link to image >
Related
I would like to know how I can write a program to calculate the number of comparison in binary insertion sorting
I tried to code the binary insertion program below, but I would like to know how I can calculate the overall comparisons made.
#include<iostream>
using namespace std;
int binarysearch (int a[], int sel, int high, int low){
int mid=(high+low)/2;
if(high<=low){
if(sel>a[high]){
return high+1;
}
else{
return high;
}
}
else{
if(sel==a[mid]){
return mid+1;
}
else if(sel>a[mid]){
return binarysearch( a, sel, high, mid+1);
}
else{
return binarysearch( a, sel, mid-1, low);
}
}}
void insertionsort(int a[], int n){
for(int i=1; i<n; i++){
int j=i-1;
int sel=a[i];
int loc=binarysearch(a,sel,j,0);
while(j>=loc){
a[j+1]=a[j];
j--;
}
a[j+1]=sel;
}
}
int main(){
int a[]= {1,6,2,5,3,4};
int n=sizeof(a)/sizeof(a[0]);
insertionsort(a,n);
cout<<"Sorted array is :";
for (int i = 0; i < n; i++)
cout<<a[i]<<"\t";
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int N;
cin>>N;
int flag=1;
for(int i=2;i<=N;i++){
for(int j=2;j<i;j++){
if(i%j==0){flag=0;break;}
break;
}
if(flag==1){cout<<i<<" ";}
}
return 0;
}
program is to print prime numbers upto input number N with seperated space
ex - N=9, my o/p -2 3,
expected o/p - 2 3 5 7
help
Just initial flag = 1 after every iteration
#include<iostream>
using namespace std;
int main()
{
int N;
cin>>N;
int flag=1;
for(int i=2;i<=N;i++){
flag=1;
for(int j=2;j<i;j++){
if(i%j==0){flag=0;break;}
break;
}
if(flag==1){cout<<i<<" ";}
}
return 0;
}
You've got at least two issues:
You never reset flag to 1. So as soon as a non-prime is found, everything is non-prime. You want to set it back to 1 each time you loop in the outer loop.
You break out of your inner loop unconditionally (j is never anything but 2), so you're actually testing for "is odd" not "is prime". Only break if you've confirmed it's not prime.
Fix that, and the code works:
#include<iostream>
using namespace std;
int main() {
int N;
cin>>N;
for(int i=2;i<=N;i++){
int flag=1; // Set each loop
for(int j=2;j<i;j++){
if(i%j==0){
flag=0;
break; // Only break when confirmed composite
}
// no break otherwise
}
if(flag==1){
cout<<i<<" ";
}
}
return 0;
}
Try it online!
This is my code
#include <bits/stdc++.h>
using namespace std;
int srch(vector<int> arr, int ln, int fn)
{
for (int i = 1; i <= ln; i++)
{
if (arr[i] == fn)
return i;
}
return -1;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n, k;
scanf("%d%d", &n, &k);
vector<int> a(n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
printf("%d\n", srch(a, n, k));
}
return 0;
}
I am not understanding where is the problem. Here is the problem link Click here.
Please help me solving this problem. I am not understanding why geeksforgeeks is showing runtime error for this code.
Note that your loops use n-th entry of vector, but
vector< int> a(n);
has indexes from 0 to n-1
Notice that the first element has a position of 0 (not 1).
I was rewriting the sort function in helper.c. The program compiled, but upon running, it froze. To simplify the problem, I've separated the sort function into another program with a certain set of ints to be sorted. Still, the execution froze at the sorting loop.
Can anyone help?
#include <cs50.h>
#include <stdio.h>
void sort(int values[], int n);
int main(void)
{
int randoms[] = {5,486,48,89,78,164,57,54,9};
sort(randoms, 9);
for(int i = 0; i < 9; i++)
{
printf("%i\n", randoms[i]);
}
}
void sort(int values[], int n)
{
// TODO: implement an O(n^2) sorting algorithm
for(int i = 0, counter; i < n - 1; i++)
{
counter = 0;
for(int j = 0, extra; j < n; i++)
{
//swap adjacent values in wrong order
if (values[j] > values[j+1])
{
extra = values[j];
values[j] = values[j+1];
values[j+1] = extra;
counter++;
}
}
if (counter == 0)
break;
}
return;
}
In your second for loop, you are increasing the wrong variable, it should be j++ instead of i++.
I am not getting where i am going wrong implementing quicksort algorithm.
Below is the code:
#include <bits/stdc++.h>
using namespace std;
int part(vector<int> &arr,int i,int j)
{
int pivot=i;
i++;
while(i<j)
{
while(arr[i]<arr[pivot])
i++;
while(arr[j]>arr[pivot])
j--;
if(i<j)
swap(arr[i],arr[j]);
}
swap(arr[j],arr[pivot]);
return j;
}
void quickSort(vector <int> &arr,int p,int r) {
if(p<r)
{
int t=part(arr,p,r);
quickSort(arr,p,t-1);
quickSort(arr,t+1,r);
}
}
int main()
{
int n;
cin >> n;
vector <int> arr(n);
for(int i = 0; i < (int)n; ++i) {
cin >> arr[i];
}
quickSort(arr,0,arr.size()-1);
for(int i=0;i<arr.size();i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
i am giving input as
7
5 8 1 3 7 9 2
but getting output as :
2 1 3 7 5 8 9
Can anyone please point out where i am going wrong.
In the "part" function you're swapping at the end, even if the values are already in place.
Just check the values before swapping:
int part(vector<int> &arr, int i, int j)
{
int pivot = i;
i++;
while (i < j)
{
while (arr[i] < arr[pivot])
i++;
while (arr[j] > arr[pivot])
j--;
if (i < j)
swap(arr[i], arr[j]);
}
if (arr[j] < arr[pivot]) {
swap(arr[j], arr[pivot]);
}
return j;
}
If i'm not mistaken, the condition in
if(i<j)
swap(arr[i],arr[j]);
in the function part is not correct; it should check the relation of the array values arr[i] and arr[j] instead of i and j to decide whether the array entries are to be swapped.