#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
I was trying to do linear search program but there was no output, what is wrong in the code?
I think the problem is that you declared " i " twice and that has no sense. Here is the right code:
#include<iostream>
using namespace std;
int main(){
int n, i=0;
int current_element;
cin>>n>>current_element;
int arr[n];
for(i=0;i<n;i++){
cin>>arr[i];
}
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
This way you initialize " i " once with the value of 0 and it should work.
i think its because in your for loop you said
for(int i = n-1; i <= 0; --i)
this doesnt seem like what you are trying to do as it says that it will keep running as long as i<=0 however i is never less than or equal to zero. i think you would want to write i>=0 instead. also, you have 2 "i" variables which you initialized.
It seems that you made a mistake on the second loop clause.
Your code
...
//loop condition is causing the problem
for(i=n-1;i<=0;--i){
...
Fixed code
...
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
...
The loop condition causes the program to not enter the second for loop. Modifying it makes the program work.
Full code
#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
Related
#include<iostream>
#include<climits>
using namespace std;
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;++i){
cin>>a[n];
} //array instillisation
int cursum=0;
int maxsum=INT_MIN;
for(int i=0;i<n;++i){
cursum+=a[i];
if(cursum<0){
cursum=0;
}
maxsum=max(cursum,maxsum);
}
cout<<maxsum<<endl;
return 0;
}
//this code is for maximum subarray problem using kadane's algo.My compiler is retrurning wrong output
In cin you are doing incorrect operation it should be
cin>> a[i];
what you are doing is taking the value of a[n]
I think I've written correct code but I am not able to input anything. Please help me.
Question - https://codeforces.com/problemset/problem/25/A
My code-
#include<iostream>
using namespace std;
int main(){
int n,count(0);
int arr[n];
for(int i=0;i<n;++i){
cin>>arr[i];
}
for(int i=2; i<n; ++i){
if(arr[i]-arr[i-1] != arr[i-1]-arr[i-2]){
++count;
}
}
cout<<count<<endl;
return 0;
}
It's probably undefined behavior from your int n variable being uninitialized. Initializing n to an explicit value allows for input as intended.
Here's my code. Whenever I run the program, I expect that the cout statement in the for loop in the function Binary() to execute. But whenever I input something, it gives me 0
#include<iostream>
#include<math.h>
using namespace std;
long int Binary(int x){
int temp=0,res=0;
for(int i=x;i<0;i--){
temp++;
res=res+ pow(10,temp)*(i%2);
i=i/2;
cout<<i<<" "<<res<<endl;
}
return res;
}
int main(){
int x; cin>>x;
cout<<endl<<Binary(x);
return 0;
}
I think the condition in your for loop is the opposite way round than your expect it. You seem to want it to continue looping until i<0. But it will actually loop while i<0. The condition is already not true when you enter the loop, so it will finish immediately without doing any iterations.
You just have to change the loop i check.
#include<iostream>
#include<math.h>
using namespace std;
long int Binary(int x){
int temp=0,res=0;
for(int i=x;i>0;i--){
temp++;
res=res+ pow(10,temp)*(i%2);
i=i/2;
cout<<i<<" "<<res<<endl;
}
return res;
}
int main(){
int x; cin>>x;
cout<<endl<<Binary(x);
return 0;
}
Check the live version here http://cpp.sh/95z74
My code is showing TLE when there when number of entries in array(n) is 1 *10^5. What should i do? I saw the submission status and in all cases it ran fine except in the last case when n is 100 000, it shows time limit error.
Problem:: https://codeforces.com/contest/474/problem/B
My solution: https://pastebin.com/5RLBirpF
Code:
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
int arr[t];
int arrfreq[t];
int sum=1;
for(int i=0;i<t;i++ )
{
cin>>arr[t];
sum+=arr[t];
arrfreq[i]=sum;
}
int m;
cin>>m;
int qsn[m];
int k;
for(int i =0;i<m;i++)
{
cin>>qsn[i];
}
for(int j =0;j<t;j++)
{
if(k<arrfreq[j])
{
cout<<j+1<<"\n";
break;
}
if(k==arrfreq[j])
{
cout<<j+2<<"\n";
break;
}
}
}
You are using one loop for calculating the number of pile. What you can do instead is find the answer in the same loop in which you are taking the input for qsn. Just take the input of qsn and find the pile number in that loop itself. That will reduce your code's time complexity and remove the TLE error.
I saw other solutions on the internet and found out that std::lower_bound function is what has to be used to avoid T.L.E.
But one thing, as mentioned on internet, this function returns a pointer to the lower bound, then , why are we subtracting c from it and then +1 also(in the 2nd last cout line)??(refer to the code.)
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,i,m,j,cnt=0,ans,x,sum=0;
cin>>n;
int a[n],c[n];
for(i=0; i<n; i++){
cin>>a[i];
sum+=a[i];
c[i]=sum;
}
cin>>m;
int b[m];
for(i=0; i<m; i++) cin>>b[i];
for(j=0; j<m; j++)
{
cout<<lower_bound(c,c+n,b[j])-c+1<<endl;
}
return 0;
}
I'm trying to convert this nested for loop to nested while loop:
The program:
#include<iostream>
using namespace std;
void main()
{
int i,j,n,stars=1;
cout<<"Enter the number of rows:";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=stars;j++)
cout<<"*";
cout<<"\n";
stars=stars+1;
}
}
While Trying the nested while loop the loop doesn't stop can someone please give me the solution?
#include<iostream>
using namespace std;
void main()
{
int n,i,j,k,stars=1;
cout<<"Enter the number of rows";
cin>>n;
i=1;
while(i<=n)
{
j=1;
while(j<=stars)
{
cout<<"*";
cout<<"\n";
stars=stars+1;
}
j=j+1;
}
i=i+1;
}
You have to incement your control varibales i ans j inside your loops. You did it outsid the loops directly after. Apart from this the varibale stars was incremented in the outer for loop. In the secend code snippet you did it in the inner while loop. Adapt your code like this:
#include<iostream>
int main()
{
int n;
std::cout<<"Enter the number of rows";
std::cin>>n;
int stars=1;
int i=1;
while ( i<=n ) // corresponds to for(i=1;i<=n;i++) { .... }
{
int j=1;
while ( j<=stars ) // corresponds to for(j=1;j<=stars;j++) cout<<"*";
{
std::cout<<"*";
j++; // increment control variable inside of the loop
}
std::cout<<"\n";
stars++;
i++; // increment control variable inside of the loop
}
return 0;
}
Note if you improve the formatting of the code, you'll find such mistakes easily.