c++ - This code shows error "stu undeclared"?? what should i do -


i know error because have declared stu inside loop scope necessity of program.i want declare array each test case (test case should input @ once).suggest me way achieve this.is dynamic memory alternative.

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int main() {     int t;     cin>>t;     int n[t],g[t];     int m =0;     for(int w=0;w<t;t++)     {         cin>>n[w]>>g[w];         int stu[n[w]];         for(int i=0;i<n[w];i++)         {             cin>>stu[i];         }      }         while(m<t)     {         int a,b;             int e;         e = (n[m]*(n[m]-1))/2;         int diff[e];         if (g[m]=1)         {             cout<<0<<endl;             return 0;         }         b=*(min_element(stu,stu+n[m]-1));         a=*(max_element(stu,stu+n[m]-1));         if (g[m]=n[m])         {             cout<<a-b<<endl;             return 0;         }         int z = 0;         for(int j=0;j<(n[m]-1);j++)         {             for(int k=(j+1);k<n[m];k++)             {                 diff[z]=abs(stu[j]-stu[k]);                 ++z;             }         }                 cout<<*(min_element(diff,diff+e-1))<<endl;         ++m;     }         cin.ignore();     cin.get();     return 0; }  

you declaring stu inside of for loop, limited scope of loop. try use outside of loop, undeclared.

for(int w=0;w<t;t++) {   ...   int stu[n[w]]; // beware: stu vla. non-standard c++.   // ok use stu here   ... }     // stu doesn't exist here 

also note standard c++ not support variable length arrays (vlas), attempting use in declaration of stu, here:

int t; cin>>t; int n[t],g[t]; 

you can replace these arrays std::vector<int>:

#include <iostream> #include <vector>  int main() {   int t=0;   cin>>t;   std::vector<int> n(t);   std::vector<int> g(t);   std::vector<int> stu ...;  } 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -