I get the error "Output Limit Exceeded" on this below code - c++

here's the code i made for finding jumping numbers less than or equal to a given no. it shows error "Output Limit Exceeded"
int main() {
int t;
cin>>t;
while(t--)
{
long long int n ,rem,temp;
cin>>n ;
for(int i=0 ; i<=n ; ++i)
{
if(i<=10)
cout<<i<<" ";
else
{
temp=i;
do
{
rem=i%10;
i/=10;
}while(abs(rem-i%10)==1);
if(i==0)
{
cout<<temp<<" "; // printing jumping no.s
}
}
}
cout<<"\n";
}
return 0;
}

You're dividing i by 10 in your do-while loop! As soon as i hits 11, it will be divided by 10, and then at the next iteration of the main for loop, you'll have a very low i value, back to where it was earlier. You're loop will never end.

Related

Partition of Array by Element given X

I Am Trying To Find Partition Of Array ,On Condition By Checking Variable x ,when less then x they will be on one side or else on another. but my code need some correction.
HERE am not able to find the error , i will be thankful to you if you help me.
Code is:-
#include<iostream>
using namespace std;
int partition(int arr[],int n,int x){
for(int i=0;i<n;){
if(arr[i]<x){
i++;
}
else if(arr[i]==x){
int temp=arr[i];
arr[i]=arr[n];
arr[n]=temp;
i--;
}
else if(arr[i]>x){
int temp=arr[i];
for(int j=i;j<n;j++){
arr[j]=arr[j+1];
}
arr[n]=temp;
i--;
}
}
return 0;
}
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int x;
cin>>x;
partition(arr,n,x);
for(int i=0;i<n;i++){
cout<<arr[i]<<"\t";
}
return 0;
}
Input >> array={2,10,15,1,3,15} ,x=10
Expected << {2,1,3,10,15,15}
Output I get << nothing .
The code isn't giving any output because, first, the "cin" and "cout" are in upper case which is syntactically incorrect, secondly, the variable j is in different case in loop statement and body inside the second else-if clause in the partition function, same goes for the "I" in the first for loop in the main() function. Sort this out and you should be good to go.
First in C++ the size of an array must be a compile-time constant. So for example, consider the following examples:
int n = 10;
int arr[n]; //INCORRECT
The correct way to write the above would be:
const int n = 10;
int arr[n]; //CORRECT
Similarly, in your code,
int n;
cin>>n;
int arr[n]; //INCORRECT because n is not a constant expression
Second, in your code, when you wrote:
arr[n] = temp; Undefined behavior
you're going out of bounds and so you have undefined behavior.
Solution
You can use std::stable_partition and std::vector to solve your problem as shown below:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
int n;
std::cout <<"Enter n:"<<std::endl;
std::cin >> n;
std::vector<int> arr(n); //create a vector of size n instead of an array
std::cout<<"Enter elements: "<<std::endl;
//iterate and take input from user
for(int &elem: arr){
std::cin >> elem ;
}
int x;
std::cout << "Enter x: "<<std::endl;
std::cin>>x;
//use std::partition
std::stable_partition(arr.begin(), arr.end(), [x](int i){return (i < x);});
std::cout<<"This is the partitioned vector: "<<std::endl;
for(int i=0;i<n;i++)
{
std::cout<<arr[i]<<"\t";
}
return 0;
}
Output
The output of the above program is as follows:
Enter n:
6
Enter elements:
2
10
15
1
3
15
Enter x:
10
This is the partitioned vector:
2 1 3 10 15 15
which can be seen here.

How to print minimum of numbers in c++ with for loop?

I wrote this piece of code, which has t test cases, and for every test cases it would input 3 digits. I want this program to then identify the minimum element among those three inputs, and then print the minimum element. What should I do?
int main()
{
int t;
cin>>t;
while(t--)
{
for(int i=0;i<3;i++)
{
int n;
cin>>n;
}
}
return 0;
}
Here's a example of finding the minimum of three digits, using a running minimum:
#include <iostream>
int main()
{
unsigned int test_case_quantity = 0U;
std::cin >> test_case_quantity;
while(test_case_quantity--)
{
char minimum;
std::cin >> minimum;
char digit;
std::cin >> digit;
if (digit < minimum) minimum = digit;
std::cin >> digit;
if (digit < minimum) minimum = digit;
std::cout << " minimum: " << minimum << "\n";
std::cin.ignore(100000, '\n'); // Synchronize to next line.
}
return 0;
}
For a more robust program, you can add validation that the character read is actually numeric.
We can make use of an extra variable say small and when we are inputting the very first number, we can assign that first number to the small and in all other cases we can compare whether the current number is less than small, if yes then update small.
Finally outside the loop we can print the value of small and this will be our answer.
int main()
{
int t;
cin>>t;
while(t--)
{
int small;
for(int i=0;i<3;i++)
{
int n;
cin>>n;
if (i == 0)
small = n;
else {
if (n < small)
small = n;
}
}
cout<<"Smallest number is :" << small;
}
return 0;
}
input :
1
10 2 3
output :
Smallest number is :2
Flow :
At first the value 10 will be assigned as small, then for the next element 2, we compare whether 2 is less than small which is 10 in that case, and reassign small to 2. Then this small is checked with 3, but 2 < 3, so small value remains unchanged.
I assume there will be only integer values as input.

Why is it not printing the coding of the void functions?

Can some help me and explain where i got it wrong?
I just don't know where I'm wrong;C
void largest(int);
void smallest(int);
void average(double);
int main()
{
int n=0,i=1,num,max=0,min=0,sum=0;
double avg=0;
cout<<"Please enter total number of integers: ";
cin>>n;
cout<<"\n";
while (n>0)
{
cout<<"Enter integer "<<i<<": ";
cin>>num;
if (num>max)
max=num;
if (num<min)
min=num;
sum=sum+num;
n--;
i++;
}
avg=sum/n;
largest(max);
smallest(min);
average(avg);
return 0;
}
void largest(int max)
{
cout<<"The largest integer is: "<<max;
}
void smallest(int min)
{
cout<<"The smallest integer is: "<<min;
}
void average(double avg)
{
cout<<"The average is: "<<avg;
}
From my understanding im not so sure if this is correct but i need to use void to print out a message and im a bit confuse as to when i should use void and what does the difference between double& and double when i use it?
Process exited ... with return value 3221225620
3221225620 is the numeric code for a STATUS_INTEGER_DIVIDE_BY_ZERO (0xC0000094) exception, which means your code is crashing before it has a chance to print out its messages.
On this line:
avg=sum/n;
n is 0 at this point, because the while loop above it had decremented n on each iteration until n reached 0.
To avoid that, change the while loop to not modify n anymore:
while (i <= n)
{
...
i++;
}
Or, use a for loop instead:
for (int i = 1; i <= n; ++i)
{
...
}
you're dividing by 0, since you're modifying n until it reaches 0 and then use it to divide, it's better if you iterate with another variable instead of reducing the value of n. so the problem isn't that it's not printing, it's that the program dies before reaching that

Why program is not working for n=100000 even if time complexity of sieve of eratothenes is O(nlog(log(n)))

#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cout<<"enter the no till which prime nos to be found :"<<endl;
cin>>n;
vector<int> prime(n+1,0);// we want n index too
for(int i=2;i<=n;i++) {
if(prime[i]==0) {
for(int j=i*i;j<=n;j=j+i) {
prime[j]=1;
}
}
}
cout<<"prime numbers are: "<<endl;
for(int i=2;i<=n;i++) {
if(prime[i]==0) {
cout<<i<<endl;
}
}
return 0;
}
above code should be working for n>=100000 since n(log(log(n))) will equal to 400000.
Your i*i overflows a 32-bit int when n is more than about 46000. This causes undefined behavior. On my system, it wraps around and initializes j with a negative value, causing a segmentation fault when prime[j] is accessed.
This has nothing to do with time complexity.

No syntax error but not printing result

I can't get the sum to print out
I did not get any warnings or such.
Trying to do this question here:http://projecteuler.net/problem=1
the program runs and then just stalls forever.
// Adding multiples of this and following equations
#include "std_lib_cmpt125.h"
void SumMultiple(int Max,int Base)
{
int i=0;
int sum=0;
for(i;i<Max;i+Base)
{
sum=i+sum;
};
cout<<"The sum"<<sum;
}
int main()
{
int base=0;
int max=0;
int sum=0;
cout<<"Please enter the sum's multiple: (ex. 3: 3,6,9,12...)\n";
cin>>base;
cout<<"Please enter the sum's maximum: (ex. 10000)\n";
cin>>max;
SumMultiple(max,base);
return 0;
}
}
You missed to increment the i,
for(i;i<Max;i=i+Base)
{
..
}
Or
for(i;i<Max;i+=Base)
{
..
}
Change:
int i=0;
int sum=0;
for(i;i<Max;i+Base)
{
sum=i+sum;
};
To:
int sum = 0;
for (int i = 0; i < Max; i += Base)
{
sum += i;
}
Note that the first statement in the original for loop does nothing, that the value of i must be incremented (which is accomplished most simply with the += operator), and that the semicolon after the closing brace is unnecessary.
It's customary in C++ to declare a loop variable in the loop itself, to limit its scope and emphasize its purpose as a counter.
You have an extra Brace } at the end and you are not incrementing i
Change the
for(i;i<Max;i+Base)
{
sum=i+sum;
};
cout<<"The sum"<<sum;
into
for(i;i<Max;i=i+Base)
{
sum=i+sum;
};
cout<<"The sum"<<sum;
or
for(i;i<Max;i+=Base)
{
sum=i+sum;
};
cout<<"The sum"<<sum;
The above code will increment the counter i and display the necessary result
Also you aren't going to get the answer with that function. problem 1 is looking for the sum of all multiples of 3 or 5 from [1..999]
try this
for ( int i = 1; i < Max ; i++ )
sum += ((i % 3 && i % 5) ? 0 : i ;
return sum