Why am I not able to input anything? - c++

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.

Related

Why this code is returning wrong value? inputs= 5 ,{ -1,4,-6,7,-4}

#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]

Why am I not getting any output(linear search)?

#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";
}
}
}

How to avoid TLE when array is used to store 1 *10^5 integer entries

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;
}

What is " Variable ' i ' was not declared in scope " in c++?

While practicing c++ code, I used the variable that was declared in the for loop.i want it to use it again in another for loop. But it showed me an error that the
variable i was not declared in scope
and I tried the same loop in Eclipse IDE it showed me
the symbol i was not resolved.
The sample code looks similar to this:
#include<iostream>
using namespace std;
int main(){
for(int i=0;i<10;i++){
cout<<i;
}
for(i=10;i<20;i++){
cout<<i;
}
}
You have to declare the variable for each scope:
#include<iostream>
using namespace std;
int main(){
for(int i=0;i<10;i++){
cout<<i;
}
for(int i=10;i<20;i++){
cout<<i;
}
}
After the first loop, there is no i anymore. You can try what the compiler says and see that this will fail:
int main(){
for(int i=0;i<10;i++){
cout<<i;
}
cout<<i; // Error
}
i only defined within the scope of the first for loop, so it needs to be re-declared in the second one.
Early Microsoft C++ compilers had a bug where the i leaked into the scope of the for loop to produce effectively
int i;
for (i = 0; i < 10; i++){
By writing for(int i=0; i<10; i++) {...} you declare the int i inside the for loop scope and it only has effect inside the for loop.
If you want to re-use the int i then you should place it outside of & before any for loop:
#include<iostream>
using namespace std;
int main(){
int i = 0;
for(i=0; i<10; i++){
cout<<i;
}
for(i=10; i<20; i++){
cout<<i;
}
cout<<i; // <- fine, 20
}
Or, simply repeat the declaration with each for loop then int i in for loops are totally different variables.
#include<iostream>
using namespace std;
int main(){
for(int i=0; i<10; i++){
cout<<i;
}
for(int i=10; i<20; i++){
cout<<i;
}
//cout<<i; <- oops!!! error
}

My DOS crashes everytime I gave large input set for my C++ program

I have written a code in C++ and tried to compile it using MinGW and TDMGCC compiler. It get compiles very well. But the problem occurs when i tried to use .exe file generated by these compiler and i give a large input set to my program my dos stops working and says windows has stopped working. After clicking on OK it terminates my execution. How can i fix it.
Here is my code
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int t,n,k,c,x,w;
ofstream myfile;
myfile.open ("example.txt");
cin>>t;
for(w=1;w<=t;w++)
{
cin>>n>>k>>c>>x;
int a[n],b[n],array[n][n];
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
cin>>b[i];
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
array[i][j]=(a[i]*i+b[j]*j+c)%x;
}
}
int re[1000],z=0;
if(k>1){
for(int i=1;i+k-1<=n;i++)
{
for(int j=1;j+k-1<=n;j++)
{
re[z]=array[i][j]+array[i][j+1]+array[i+1][j]+array[i+1][j+1];
z++;
}
}}
else
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
re[z]=array[i][j];
z++;
}
}
}
int lar=re[0];
for(int i=1;i<z;i++)
{
if(re[i]>lar)
lar=re[i];
}
myfile<<"Case #"<<w<<": "<<lar<<endl;
}
myfile.close();
return 0;
}
You have a basic off-by-one error:
int a[n],b[n],array[n][n];
for(int i=1;i<=n;i++)
cin>>a[i];
a has length n, meaning valid indices are between 0 and n-1 inclusive. You're writing a value at index n, which is past the end.
Structure your loops like this instead:
for (int i = 0; i < n; i++)
cin >> a[i];
All your loops show this problem.
Also, like #NathanOliver says, use std::vector instead of variable-length arrays (VLA). VLA is in the C standard, but it's not in C++. So your use of it here is non-standard. Simply add #include <vector> and replace
int a[n];
with
std::vector<int> a(n);
and everything else can stay the same.