Count the number of instances in an array.

Hello, let me start by saying that I am extremely inexperienced and confused, so please bear with me. I am attempting to write a program in which I am given a data file with up to 100 pairs of (x,y) coordinates, and I must perform various calculations. One of these calculations is to "find the average of only the negative y-values" in the given set of data. I have created a loop which collects all of the x-values and y-values and placed them into arrays, so that I may use those arrays to perform all of the necessary calculations. This is what I have for a loop to calculate the needed value, although it is wrong.

The following describe the objects that I have used.
int i // Integer used in array.
int N // Total number of data pairs. (This is given in data file)
float x[100] // Array of x-values.
float y[100] // Array of y-values.
float sumny // Sum of only the negative y-values.
float avgny // Average of only the negative y-values.

1
2
3
4
5
6
7
8
9
10
11
12
13
for (i=0; i<N; i++)
{
  if (y[i] < 0)
  {
    sumny = sumny + y[i];
  }
  
  else
  {
    sumny = sumny;
  }
 avgny = sumny / i;
}

Any help would be appreciated greatly. Thanks for your time and effort with this, and hope you have a great day.
Last edited on
Firstly, please use the code tags when posting code. You'll find them on the right (they look like <>) when you're editing your post.

Ok. Let's forget about code for a minute, and put what you want to do into WORDS. This might be called 'pseudo-code'. We want to write some pseudo-code to describe how to compute the average of your negative y-values:
1. Look through the collection of y values. When we find a negative one, we need to modify a cumulative sum variable (initialised to zero at the start) by the amount of that y value.

sumny = sumny + y[i];

or simply

sumny += y[i];

We also need to increment a counter that represents the number of negative y values that we have found. Let's call this counter 'numMatches'. Incrementing this would simply be

numMatches++;

2. After looking through the list, we can find the average by taking the sum and dividing by the number of matches numMatches.

Once you understand what you're trying to do in ENGLISH (or another language), you can then try to translate it into code. Hint: Step 1 is your for loop. Step 2 is a simple calculation done after the for loop.

Post up your next attempt if you can't get it out...

Note: Be careful with your use of 'object'. Perhaps others can comment on this, but when somebody tells me they have an 'object' it conveys to me that they're talking about an instance of a class in the object-oriented sense, not a variable of an elementary data types like int, float etc. I think a better term in your case would be 'variable'. If you haven't learnt about classes and objects yet, don't stress about this at the moment.
Sammy34,

Thank you so much for all of your help with this, I was able to finish it correctly by using your advice. Also, thanks for the tip concerning how to post code on here by using the code tags. I had no idea how everybody else was doing that, and I couldn't quite seem to figure it out earlier. Finally, I'm sorry that it took me so long to reply back to you, but I actually had about 3 or 4 separate calculations in the program that I needed to use a counter on, and I'm really slow at this. I have absolutely no prior experience with any sort of programming, and apparently this is a major weakness of mine. Anyway, thanks again, and I hope you have a great evening. Take care. :-)
Re: 'major weakness of mine'...

I view your ability to 'have a go' and ask the pertinent questions as a strength. Whilst it may seem a bit cryptic at the start, it's like everything else - practise makes perfect. There's always a logical explanation for the way such computer programs behave, and this is why programming will probably become appealing to you very quickly :).

A hint for next time though: Force yourself to spend some time (e.g. 1hr) trying to figure out where the problem is. You will get frustrated, and this is part of the learning process. If you can't figure it out, you will nevertheless have enough knowledge to ask a DETAILED and SPECIFIC question on the forum. When you find receive a response, the solution will likely stick to your mind because you spent so much time sweating over it.

Enjoy :).
Topic archived. No new replies allowed.