I was writing a code , in which when i declare a vector globally , it gives wrong answer but when i declare it in the main function . it becomes right . so i want to know the difference between the two declarations
this is a code which find the min of first enteries, and 2nd min of 2nd enteries and then if min >= 2nd min print no . here i declared vector in the main class but when i declare it with my INTs declared , the site on which i was submitting gives me wrong answer.
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
using namespace std;
#define MAX 20000;
int a,b,c,d,i,z;
int main()
{
scanf("%d %d", &a, &b);
while( a!=0 && b!=0)
{
z = MAX;
for(i=0;i<a;i++)
{
scanf("%d",&c);
if(z>c)
{
z=c;
}
}
vector<int> v;
for(i=0;i<b;i++)
{
scanf("%d",&d);
v.push_back(d);
}
sort(v.begin(),v.end());
if (z >= v[1])
printf("N\n");
else
printf("Y\n");
scanf("%d %d", &a, &b);
}
return 0;
}
Sudhanshu
If you declare the vector as a global variable, you should invoke vector::clear() in the begin of the loop to clear all of the elements stored in the last loop.