2 Replies - 3151 Views - Last Post: 03 October 2012 - 07:56 AM Rate Topic: -----

#1 Zarrion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);
}



Thanks in advance!

Is This A Good Question/Topic? 0
  • +

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

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • 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

View PostZarrion, 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);
}



Thanks in advance!


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

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,291
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1