# How to count the negative numbers generated in an Array

Page 1 of 1

## 2 Replies - 5255 Views - Last Post: 03 October 2012 - 07:56 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294056&amp;s=8a8d5d1bdf3314939dd2ccac916fb627&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Zarrion

Reputation: 0
• Posts: 1
• Joined: 03-October 12

# How to count the negative numbers generated in an Array

Posted 03 October 2012 - 12:52 AM

Greetings.

I've gotten the task in school that is the following:
1: Create an array of 20
2: Add a random number between -50 and 50 to every space in the array
3: Count all the negative numbers generated
4: Print how many negative numbers found

I am pretty sure i got 1 and 2 right. I just dont understand why my program wont count all the negative numbers and print them.

I hope some of you are of some help

```#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

#define Array_SIZE 20

int main ( void )
{
srand( time(0) );

int i, negativeCount=0;
float myArray[ Array_SIZE ];

for( i=0; i<Array_SIZE; i++ ) {
myArray[i] = rand()%-50 + 49;
}

if( myArray[i]<0);
negativeCount++;

printf("Number of negatives:");
scanf("%d", &myArray, &negativeCount);

return (0);
}

```

Is This A Good Question/Topic? 0

## Replies To: How to count the negative numbers generated in an Array

### #2 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,440
• Joined: 14-September 12

## Re: How to count the negative numbers generated in an Array

Posted 03 October 2012 - 01:35 AM

Zarrion, on 03 October 2012 - 01:22 PM, said:

Greetings.

I've gotten the task in school that is the following:
1: Create an array of 20
2: Add a random number between -50 and 50 to every space in the array
3: Count all the negative numbers generated
4: Print how many negative numbers found

I am pretty sure i got 1 and 2 right. I just dont understand why my program wont count all the negative numbers and print them.

I hope some of you are of some help

```#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

#define Array_SIZE 20

int main ( void )
{
srand( time(0) );

int i, negativeCount=0;
float myArray[ Array_SIZE ];

for( i=0; i<Array_SIZE; i++ ) {
myArray[i] = rand()%-50 + 49;
}

if( myArray[i]<0);
negativeCount++;

printf("Number of negatives:");
scanf("%d", &myArray, &negativeCount);

return (0);
}

```

I think the problem with it not printing negative count is because you have used a scanf instead of a printf statement.
Replace the scanf with this code

```printf("%d",negativeCount);
```

and remove the semicolon after the if(myArray[i]) and use brackets like this.

```if(myArray[i]<0)
{
negativeCount++;
}
```

regards,
Raghav

This post has been edited by raghav.naganathan: 03 October 2012 - 01:40 AM

### #3 Skydiver

• Code herder

Reputation: 4378
• Posts: 14,138
• Joined: 05-May 12

## Re: How to count the negative numbers generated in an Array

Posted 03 October 2012 - 07:56 AM

Line 19 for generating your random numbers between -50 and +50 looks highly suspect. I recommend reviewing it to make sure you are getting the results you are expecting.