How to cout the negative numbers from a array

Hello, im new in c++ programming.I made an array with 10 numbers.Can someone tell me how to cout the negative numbers from the array.I know that i have to use if but i dont know how to do it.
Can someone tell me how to cout the negative numbers from the array.


Just to be clear, do you want to count how many negative numbers are in the array with 10 elements?
Last edited on
You can display the negative numbers directly to the console(screen) just the way you do with positive numbers but make sure you declare the array as int (it implies signed int).
Last edited on
No, i dont want to cout how many the negative numbers are.For example if in my array they are 10 numbers - (10,12,13,14,15,16,17,18,20,-25) i want to cout "-25".
@mbsaicharan.I know that i can input negative numbers in my array.But when i input my 10 numbers i want to cout the negative ones.Like after the numbers in the array i want to cout something like that "The negative numbers in the array are - ..."
Last edited on
You need to iterate through your array and if a number is negative then display it.
1
2
3
4
5
6
7
8
  int numbers[] = {10,12,-13,14,-15,16,17,-18,20,-25};
  int num_items = sizeof(numbers) / sizeof(numbers[0]);
  
  for (int i = 0; i < num_items; i++)
  {
    if (numbers[i] < 0)
      std::cout << numbers[i] << '\t';
  }
Can you explain to me what does sizeof do?Im new into c++ programming so i dont know how to use most of the codes.I understand the other part of the code but i cant understand the sizeof thing
sizeof gives you the size in bytes of a variable. In this case - on a 32 bit system - the size of an int is 4 bytes. The whole array has 10 * 4 bytes = 40 bytes. To get the number of elems in an array you divide the size of the array by the size of an element.

http://www.tutorialspoint.com/cplusplus/cpp_sizeof_operator.htm
Can i input the negative numbers in other way?Or i have to use the sizeof ?
If you know your array is going to hold 10 values, you can skip the 'sizeof' statement and replace 'num_items" in the 'for' statement by '<=9'
Topic archived. No new replies allowed.