Tiny Array Error

This is a discussion on Tiny Array Error within the C++ Programming forums, part of the General Programming Boards category; I keep getting the error: "invalid types'double[double]' for array subscript. It errors on line 105 and 111 twice. I have ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    11

    Tiny Array Error

    I keep getting the error: "invalid types'double[double]' for array subscript. It errors on line 105 and 111 twice. I have tried switching up various double and ints that are in the array, to no avail. I am using Dev C++ and MS Visual C++.

    Code:
    //AS-31
    #include <iostream>
    #include <string>
    using namespace std;
    
    double computemin ( double sortedData [], int length );
    double computemax ( double sortedData [ ], int length );
    double computemean ( double sortedData [ ], int length );
    double computemedian ( double sortedData [ ], int length);
    
    int main ()
    {
      double data [20],sdata[20];
      int length,i;
      double min,max,mean,median,sum,temp;
      bool swapped;
      //input data into array
      cout <<"Enter data size: "<<endl;
      cin >>length;
      for (i=0; i<length;i++)
      {
        cout <<"Enter a data item: ";
        cin >>data[i];
      }
        cout <<endl;
      //copy the contents of data into sdata
    
        for (i=0; i<length;i++)
      {
        sdata[i] = data[i];
      }
      //sort array sdata
      swapped=true;
      while (swapped)
        {
        swapped=false;
        for (i=0;i<length-1;i++)
        {
          if (sdata[i] > sdata[i+1])
         {
          //swap
          temp=sdata[i];
          sdata[i] = sdata [i+1];
          sdata [i+1] = temp;
          swapped=true;
          }
        }
      }
      //display original data
      cout <<"Original data: "<<endl;
      for (i=0; i<length;i++)
      {
        cout <<data[i] << " ";
      }
      cout <<endl;
      //display sorted data
      cout <<"Sorted data: "<<endl;
      for (i=0; i<length;i++)
      {
        cout <<sdata[i] << " ";
      }
    
      computemin (sdata,length);
      computemax (sdata,length);
      computemean (sdata,length);
      computemedian (sdata,length);
      
    system ("PAUSE");
    }
    
    double computemin ( double sortedData [], int length )
    {
      double min=sortedData[0];
      cout <<"Min: "<<min<<endl;
      return 1;
    }
    
    double computemax ( double sortedData [ ], int length )
    {
       double max=sortedData[length-1];
        cout <<"Max: "<<max<<endl;
      return 1;
    }
    
    double computemean ( double sortedData [ ], int length )
    {
      double sum=0;
      for (int i=0; i<length;i++)
      {
         sum=sum+sortedData[i];
      }
     double mean=sum/length; 
      cout <<"Mean: "<<mean<<endl;
        return 1;
    }
    
    double computemedian ( double sortedData [ ], int length)
    {
      double indexHi,indexLo,index,median,sdata;
      if ((length %2) != 0 )
      {
        index = length / 2;
       median = sdata [index];
      }
      else 
      {
       indexHi = length / 2;
       indexLo = indexHi -1;
       median = (sdata[indexLo] + sdata[indexHi] ) / 2;
      }
       cout <<"Median: "<<median<<endl;
         return 1;
    }

  2. #2
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    You are using "sdata[]" instead of "sortedData []".

    Also you are using a "double" as an array index --- can't do that! They have to be "int"
    Last edited by kcpilot; 07-23-2008 at 01:50 PM. Reason: added more problem fixes

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    but my data needs to be in a double

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    We're talking about the array index, not the contents of the array. How do you propose the compiler generate code to access element.... say 2.5? It doesn't make sense in context of arrays. You must deal with non-float types for any type of array index.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    Oh, you mean where the array is, not what's in it?

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Where the array is? That would be an address. The index is the numerical value representing the element of the array you are accessing.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    how would I fix my code then?

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by MacGyver View Post
    We're talking about the array index, not the contents of the array. How do you propose the compiler generate code to access element.... say 2.5? It doesn't make sense in context of arrays. You must deal with non-float types for any type of array index.
    Well, actually GPU's can access element 2.5, whihc is different from element 2 or 3. The 2.5 begines halfway into element 2 adn continues to just before halfway into element 3. For normal C/C++ though the compiler truncates it at runtime to 2.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    I am confused on what to change still.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    The indexes should not be doubles. It shouldn't be too hard to fix, since your variables have good names that indicate that they are indexes.

  11. #11
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    I fixed all errors, but one. No clude what the error is. It reads: Line 101: declaration of 'int sdata[20]' shadows a parameter

    Code:
    //AS-31
    #include <iostream>
    #include <string>
    using namespace std;
    
    double computemin ( double sdata [], int length );
    double computemax ( double sdata [ ], int length );
    double computemean ( double sdata [ ], int length );
    double computemedian ( double sdata [ ], int length);
    
    int main ()
    {
      double data [20],sdata[20];
      int length,i;
      double min,max,mean,median,sum,temp;
      bool swapped;
      //input data into array
      cout <<"Enter data size: "<<endl;
      cin >>length;
      for (i=0; i<length;i++)
      {
        cout <<"Enter a data item: ";
        cin >>data[i];
      }
        cout <<endl;
      //copy the contents of data into sdata
    
        for (i=0; i<length;i++)
      {
        sdata[i] = data[i];
      }
      //sort array sdata
      swapped=true;
      while (swapped)
        {
        swapped=false;
        for (i=0;i<length-1;i++)
        {
          if (sdata[i] > sdata[i+1])
         {
          //swap
          temp=sdata[i];
          sdata[i] = sdata [i+1];
          sdata [i+1] = temp;
          swapped=true;
          }
        }
      }
      //display original data
      cout <<"Original data: "<<endl;
      for (i=0; i<length;i++)
      {
        cout <<data[i] << " ";
      }
      cout <<endl;
      //display sorted data
      cout <<"Sorted data: "<<endl;
      for (i=0; i<length;i++)
      {
        cout <<sdata[i] << " ";
      }
    
      computemin (sdata,length);
      computemax (sdata,length);
      computemean (sdata,length);
      computemedian (sdata,length);
      
    system ("PAUSE");
    }
    
    double computemin ( double sdata [], int length )
    {
      double min=sdata[0];
      cout <<"Min: "<<min<<endl;
      return 1;
    }
    
    double computemax ( double sdata [ ], int length )
    {
       double max=sdata[length-1];
        cout <<"Max: "<<max<<endl;
      return 1;
    }
    
    double computemean ( double sdata [ ], int length )
    {
      double sum=0;
      for (int i=0; i<length;i++)
      {
         sum=sum+sdata[i];
      }
     double mean=sum/length; 
      cout <<"Mean: "<<mean<<endl;
        return 1;
    }
    
    double computemedian ( double sdata [ ], int length)
    {
      int indexHi,indexLo,index,median,sdata[20];
      if ((length &#37;2) != 0 )
      {
        index = length / 2;
       median = sdata [index];
      }
      else 
      {
       indexHi = length / 2;
       indexLo = indexHi -1;
       median = (sdata[indexLo] + sdata[indexHi] ) / 2;
      }
       cout <<"Median: "<<median<<endl;
         return 1;
    }

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Just what it says: you have a parameter sdata[] that you will never never never never ever get to work with, since you created a new variable of the same name that overrides ("shadows") the one passed in to your function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21