arrays - C++ algorithm exercise -


i have question 1 of problems. have array n elements , must see if 1 made rule: positive negative positive positive negative negative positive positive positive negative negative negative here code: // sa se verifice daca un vector contine elementele in ordinea: // pozitiv-negativ-pozitiv-pozitiv-negativ-negativ-pozitiv-pozitiv-pozitiv etc. // se va afisa da sau nu.

#include <iostream> using namespace std;  int main() { int i, j, k; int n; cout<<"dati numarul de elemente: "; cin>>n; int v[n+1]; cout<<"dati elementele vectorului: "; for(i=1; i<=n; i++)     cin>>v[i];  int stop = 1; int aici = 2; int pozitiv, negativ; for(int i=1; i<=n; i=i+stop) {     for(int k=i; k<=aici/2; k++)     {         cout<<"v[k] = "<<v[k]<<endl;         if(v[k]>0)             pozitiv = 1;         else         {              pozitiv = 0;             break;         }      }     for(int j=aici/2+1; j<=aici; j++)     {         cout<<"v[j] = "<<v[j]<<endl;         if(v[j]<0)             negativ = 1;         else         {              negativ = 0;             break;         }     }     if(pozitiv == 0 && negativ == 0)         break;     stop = 2*i;     aici = aici + stop;     cout<<stop<<" "<<aici<<endl; }  if(pozitiv == 1 && negativ == 1)     cout<<endl<<"da!"; else cout<<endl<<"nu!";  cout<<endl; return 0; 

}

int sign(int v) {    return (v == 0) ? 0 : ((v > 0) ? 1 : -1); }  int check(int v[], int n) // return 1 true, 0 false {    int sign = 1;    int count = 1;    int sc = 0;    (int = 0; < n; i++)    {       if (sign(v[i]) != sign)          return 0;       sc++;       if (sc == count)       {          sc = 0;          if (sign == 1)             sign = -1;          else          {             sign = 1;             count++;          }       }    }    return 1; }  int main() {    // ... put here code filling v     cout << (check(v, n)) ? "da!" : "nu!" << endl; } 

also, should declare v int v[n], , fill v[0] v[n-1].


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -