Menu

take a look plz

fi fol
2008-12-19
2012-09-26
  • fi fol

    fi fol - 2008-12-19

    guys i have sompe problem with project below...
    eror in line 5 (int x[] storage size of x isnt known)
    anothe eror (in function int main) what kind of error is this?
    any suggestion?

    int main()
    {
    int i;
    int j;
    int x[];
    i=0;
    cout << "\n enter a number or 7777 to end:";
    cin>> x[i];
    while (x[i]!= 7777)
    {
    cout <<"\n enter a number or 7777 to end:";
    cin>>x[i];
    i++;
    }
    cout << "n the number of values entered:";
    cout << i;
    float avg;
    avg= (x[0]+x[i-1])/2;
    printf("\n average of first and last numbers:");
    cout << avg;
    int sum=0;
    int max=x[0];
    int min=x[0];
    for (j=0; j<i;j++){
    if (x[j]>0)
    sum=sum+x[j];

    if (x[j]&gt;max) 
        max=x[j];
    
    if ((x[j]&gt;0) &amp;&amp; (x[j]&lt;min)) 
        min=x[j];
    

    }
    printf("\n sum of all positive numbers:");
    cout << sum;

    printf("\n 70% of smallest positive number:");
    cout << min*0.7;

    printf("\n largest number entered:");
    cout << max;

    int max_count=0;
    for (j=0;j<i;j++){
    if (x[j]==max)
    max_count++;
    }
    printf("\n the number of times the largest number is entered:");
    cout << max_count;

    printf("\n last two numbers in descending order:");
    if (x[i-1] < x[i-2])
    cout << x[i-1],x[i-2];
    else
    cout << x[i-2],x[i-1];

    }

     
    • cpns

      cpns - 2008-12-19

      When you define an array, you have to give it a size. Moreover in C89, the size must be a constant expression (known at compile time), not a variable. C99 allows variable length arrays, but it is not supported by many compilers so best avoided.

      > anothe eror (in function int main) what kind of error is this?
      Don't make us work unnecessarily hard - post the log! We need to see what errors you are getting, not teh ones we might get - which could be different! Copy & Paste it form the "Compile Log" tab, It is far easier for you, and will get you a better answer. It is also clearly requested in the "PLEASE READ BEFORE POSTING A QUESTION" thread, which you should have read.

      drmoo wrote:
      > Try putting a number between []. Make it high.

      Vague and dangerous advice. How high - high enough to blow the stack perhaps? Because i is unbounded, the code is dangerous in any case, simply 'making it high' is not a solution - that is how buffer overrun exploits are allowed to propagate, don't promote such practices!

      Note that your input loop is also horribly flawed. You get an initial value for x[0], so the while loop test is fine first time, then first time in the loop x[0] is replaced with new input (so the first value is overwritten), then i is incremented so that when x[1] is tested in the while loop, it is an unitialised value. The chances of this loop actually ending are minimal, and will be more luck than judgement - somewhere in RAM the value 7777 may exist, but it is unlikley to still be in the bounds of the array. Even if that were fixed, the final value 7777 will be included in the count subsequent calculations.

      Consider this fragment:


      const int MAX_INPUT_COUNT = 32 // or some other value as appropriate

      int x[MAX_INPUT_COUNT] ;
      int inval = 0 ;
      i=0;

      while( i < MAX_INPUT_COUNT && < inval != 7777)
      {
      cout <<"\n enter a number or 7777 to end:";
      cin >> inval;

      if( inval != 7777 )
      {
      x[i] = inval ;
      i++;
      }

      }


      Finally make up your mind whther you are going to use C++ iostream or C stdio, avoid mixing both - loose the printf's I suggest..

      drmoo wrote:
      > Also, make sure you have
      > using namespace std;
      > at the beginning.

      Also not best advice. Moving the entire standard namespace to the global namespace just to access two symbols is not a good idea. Near everyone does it from time to time, even me, but I would not actively promote the practice for anything other than perhaps porting legacy code. For example, the following code fails to compile:

      //////////////////////////////////

      include <algorithm>

      using namespace std ;

      static int count ;

      int main()
      {
      count++ ;
      }
      //////////////////////////////////

      You tell me why!? Yes the example is contrived, but I have seen examples of this in real code, posted to this forum, more than once.

      Clifford

       
    • anonymous nobody

      > int x[];
      Try putting a number between []. Make it high.
      >cout << x[i-2],x[i-1];
      Shouldn't that be
      cout << x[i-2] << x[i-1];
      ? Also, make sure you have
      using namespace std;
      at the beginning.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.