# array subscripts question



## lounsbery (Oct 28, 2003)

Ok, what is an array subscript? Trying to debug my program and i get about 30 of these errors, but thats it. We havent talked about anything like this and I have messed around for hours not knowing how to fix it, so thought I would ask here. Here is an example of the errors I'm getting.

lounsbej8.C: In function `double Get_Largest (int, double, double)':
lounsbej8.C:133: invalid types `double[int]' for array subscript

It's either like above double[int] or its double *[double], and it does it on every line that the array variable is on.

Any help is appreciated, if not, oh well. The file should be linked if anyone wants to look at it for the hell of it.

C++ File


----------



## MustBNuts (Aug 21, 2003)

the array subscript is the numbering system for an array. Think of the array as columns and rows. Example, you have an array that is 2 columns, 2 rows, your subscripts would be (0,0), (0,1)(1,0),(1,1).

Make sense?


----------



## lounsbery (Oct 28, 2003)

Ok, I get the drift of what your saying. However, I dont get what would be wrong with the error in the program. Is one of the doubles/ints supposed to be a different type? Or how does that relate. I'm all ****ed up lol


----------



## MustBNuts (Aug 21, 2003)

Okay, it's been years since I've looked at C, but I'll give this a try:

You are passing this code some Numbers that are doubles, right, but then you are using the Numbers as an array name. I think that's where your problem is. I don't remember how to program it, but you have to declare your array Numbers(2,2) or whatever it should be, then pass the array. I don't think an array can be "double" because it ISN"T a number, it's an array. The way you have it now, Numbers[Comp] is like saying 157[1]; what you mean to say is that number 157 is located in MyArray[1] (at position one).

I think your passing the array the wrong way....but it's only my guess.

double Get_Largest ( int Size, *double Numbers*, double Largest )
// used to find the largest value in the array
{
for ( int Pass = 1; Pass < Size; ++ Pass )
{
for ( int Comp = 1; Comp <= Size - Pass; ++ Comp )
{
if ( *Numbers [ Comp - 1] *< Numbers [ Comp ] )
{
Swap ( Numbers [ Comp - 1 ], Numbers [ Comp ] );
}
}
}
Largest = Numbers [ 1 ];
return Largest;
}


----------



## lounsbery (Oct 28, 2003)

Awww, I am so retarded. Thanks for the help, I forgot to put the "[ ]" after numbers in all the functions when I called them. /bonk Now the file at least compiles  even though it says "floating point exception" and terminates. Progress is made, thanks again.


----------



## MustBNuts (Aug 21, 2003)

Glad I could help!


----------



## AlbertB (Nov 24, 2002)

I don't think you have the problems you imagine you do. MS Vis C++ compiles your supplied code quite easily with only a few simple errors reported:-

Do not use the variable "Position" as your array index within the function "Get_Sum", it will kick up an error about redefining a formal parameter. Rename it to something like Index within that function:


```
double Get_Sum ( double Numbers [ ], int Position, int Size, double Sum )
// used to find the sum of all the values in the array
{
   for ( int Index= 0; Index < Size; ++ Index )
   {
      Sum = Sum + Numbers [ Index ];
   }
   return Sum;
}
```
Initialise your variable list before doing anything with them. By this I mean write lines:

Size=0; Positive_Count=0; Negative_Count=0; Zero_Count=0; Position=0; Sum=0; Average=0; Largest=0; Smallest=0;

into your main function just after the declaration of these variables.

I had no trouble getting it to compile in MS Vis C++ after changing these points.

Now that is not to say it will do what you expect! 

Good luck.


----------



## lounsbery (Oct 28, 2003)

Ok, I didnt realize I had forgot to put it all equal to zero. We were even told to do that . I didnt have to change position though, because thats the exact way we went through it in class so just leaving it that way.

Ok guys, sorry, last question  but what is wrong with this? it is only doing the first counter and leaving the function, and I should be able to see it, but i dont.

void Positive_Negative_Zero_Count ( int Position, double Numbers [ ], int & Positive_Count, int & Negative_Count, int & Zero_Count )
// used to find the Positive, Negative, and Zero amounts from the text file
{
if ( Numbers [ Position ] > 0 )
{
++ Positive_Count;
++ Position;
void Positive_Negative_Zero_Count ( int Position, double Numbers [ ], int & Positive_Count, int & Negative_Count, int & Zero_Count );
}
else if ( Numbers [ Position ] < 0 )
{
++ Negative_Count;
++ Position;
void Positive_Negative_Zero_Count ( int Position, double Numbers [ ], int & Positive_Count, int & Negative_Count, int & Zero_Count );
}
else if ( Numbers [ Position ] == 0 )
{
++ Zero_Count;
++ Position;
void Positive_Negative_Zero_Count ( int Position, double Numbers [ ], int & Positive_Count, int & Negative_Count, int & Zero_Count );
} 
}


----------



## MustBNuts (Aug 21, 2003)

Do you mean it's only counting the first position..? I don't know what the rest of your code says, but once you advance Position by 1, where do you return to this code? 

Do you need a loop around this whole thing to count ALL the Positives, Negatives, Zero counts, THEN go to the PosNegZero function with the final results?


----------



## AlbertB (Nov 24, 2002)

Yeah, it looks as if you need a loop around the whole thing starting at 0 and stopping when you have worked on all of your array. You can manage that I am sure.

One thing I will point out though, is that you should prevent duplication of code as much as possible. If you look at the three parts of your IF statement you will see that in each one you increment Position. This means it does not need to be tested for and it could and should be brought out to after the IF statement where it will be needed only once, more controllable and simplifying each option. Like so:


```
void Positive_Negative_Zero_Count ( int Position, double Numbers [ ], int & Positive_Count,
 int & Negative_Count, int & Zero_Count )
// used to find the Positive, Negative, and Zero amounts from the text file
{
  if ( Numbers [ Position ] > 0 )
  {
    ++ Positive_Count;
  }
  else if ( Numbers [ Position ] < 0 )
  {
    ++ Negative_Count;
  }
  else if ( Numbers [ Position ] == 0 )
  {
    ++ Zero_Count;
  } 
  [b]++ Position;
  void Positive_Negative_Zero_Count ( int Position, double Numbers [ ], int & Positive_Count,
       int & Negative_Count, int & Zero_Count );
[/b]
}
```
I have just moved the functioncall to Positive_Negative_Zero_Count outside too on edit as this seems to be repeated without any ramifications.


----------



## lounsbery (Oct 28, 2003)

Nevermind  I got it. Forgot to put in the do/while part on top and bottom. It's finals week and I'm so stressed out I'm forgetting easy things, lol. Thanks again for the help MustBNuts and AlbertB


----------



## lounsbery (Oct 28, 2003)

Ok, I see. We havent really learned much about the loop statements and stuff like that, so let me ask a quick question. So after each if statement, if its true it goes to the end of all the ifs and finishs out the other parts of the function? Is that why they will still work when you put them on the bottom like that?

Here is what I fixed mine too after i remembered.

void Positive_Negative_Zero_Count ( int Size, int Position, double Numbers [ ], int & Positive_Count, int & Negative_Count, int & Zero_Count )
// used to find the Positive, Negative, and Zero amounts from the text file
{
do
{
void Positive_Negative_Zero_Count ( int Size, int Position, double Numbers [ ], int & Positive_Count, int & Negative_Count, int & Zero_Count );
if ( Numbers [ Position ] > 0 )
{
++ Positive_Count;
++ Position;
}
else if ( Numbers [ Position ] < 0 ) 
{
++ Negative_Count;
++ Position;
}
else if ( Numbers [ Position ] == 0 )
{
++ Zero_Count;
++ Position;
}
} while ( Position <= Size );
}

So I can move the ++ Position; to just after that while statement and it will still work fine?


----------



## AlbertB (Nov 24, 2002)

Spot on, you got the idea in one!  

Mind you, you don't want to move it outside of the loop, as in after the while statement, you want it immediately after the IF statement, before the while statement. You want it to increment every time you go through that loop which is what would happen if you left it where you had it in all 3 places. Putting it after the while it would only work once when all of the loops had finished.

Move out the Positive_Negative_Zero_Count function with it, your code becomes much clearer to follow and it saves a considerable amount of duplication.

Always try to prevent duplication of code, that is really why we have functions and make calls to them in the first place. It would be just as logical to put the function's code everywhere the function is called. But it would mean a heck of a lot more work typing it in there, plus a lot more work if you make a change in the code later, plus the greatly increased chances of an error creeping in in the first place.


----------



## lounsbery (Oct 28, 2003)

That's cool, glad I came here for the help, now I know a lot more  haha


----------



## MustBNuts (Aug 21, 2003)

No, your ++Position needs to be advanced everytime you pass through the loop, so if you put it AFTER the while, it will only advance once on its way out of the function.

So you put it after all your if statements but before the end of the loop.


PS: oops, dont mind me, I'm catching up to your posts.....


----------



## MustBNuts (Aug 21, 2003)

okay guys, don't mind me...I'm catching up to your posts....


----------

