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
}
Related
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.
The following is the code, i have tried al the other styles mentioned in the comments one by one but none worked to remove the garbage value, Although "jugaad" works but it's not performance wise good. Couldn't figure out what's wrong !
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,m;
cin>>n>>m;
// Other styles
// int data[n+1][m+1] = {0,0};
// int data[n+1][m+1] = {0};
// int data[n+1][m+1] = {{0,0}};
int data[n][m] = {0};
// Jugaad start:
for(int i=0;i<m;i++){
data[0][i] = 0;
}
// Jugaad end
cout<<"\n";
for(int x=0;x<n;x++){
for(int y=0;y<m;y++){
cout<<data[x][y]<<"\t";
}
cout<<"\n";
}
cout<<"\n";
return 0;
}
Screenshot of code and output
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
data[i][j] = 0;
}
``}
This will set all values of your array to zero
#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";
}
}
}
I used CodeBlocks to code this program:
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int main()
{
ifstream is;
is.open("game.inp");
int n,a[100];
is>>n;
for (int i=0; i<n; i++)
is>>a[i];
is.close();
int game[100];
int kt=0;
for (int i=0; i<n; i++)
{
for (int j=3; j<a[i]+1; j++)
{
if ((a[i]%j)==0)
{
int *x = find(begin(game),end(game),j); //ktra uoc hien tai da co trong mang hay chua, k co thi ms them
if (x==end(game))
{
game[kt]=j;
kt++;
}
}
}
}
int kq=0;
for (int i=0; i<kt; i++)
{
int d=0;
for (int j=0; j<n; j++)
{
if ((a[j]%game[i])==0)
d++;
}
if (d>kq)
kq=d;
}
ofstream o;
o.open("game.out");
o<<kq;
o.close();
return 0;
}
and the result was not correct. Then I decided to copy these codes to Visual Studio 2019 and it gave me the correct result. I don't know what happened. I copied the same codes from CodeBlocks to VS and the results were completely different.
Welcome to the wonderful world of undefined behavior.
You use the contents of the array game before it's initialized. Local variables are not initialized, their contents is indeterminate and using indeterminate values lead to undefined behavior.
If you want the array to be initialized to all zeroes you need to do it explicitly:
int game[100] = { 0 };
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.