0

I am writing this code to read a N integer array, then I suppose to check the even numbers and print them alone.

this is what I wrote, but I get this error: expression must have integral or unscoped enum type

#include <stdio.h>

void main(){
    int N;
    int count_even = 0, count_odd = 0;
    scanf_s("%d", &N);
    float *arr = new float[N];
    for (int i = 0; i < N; i++){
        scanf_s("%f", &arr[i]);
    }
    for (int j = 0; j < N; j++){
        if (arr[j] % 2 == 1){
            count_odd++;
        }
    }
}

the error on this line: " if (arr [j]) % 2 ==1)

Barry
  • 286,269
  • 29
  • 621
  • 977
Ruaa Brkat
  • 151
  • 1
  • 2
  • 14
  • 1
    "even" and "odd" usually apply to integers, so why an array of floats? – molbdnilo Nov 21 '14 at 12:25
  • It is a homework, I suppose to use array of floats – Ruaa Brkat Nov 21 '14 at 12:27
  • 2
    void main is illegal. You forget to delete your array. Why not use `vector` – Neil Kirk Nov 21 '14 at 12:29
  • @RuaaBrkat: Does your homework also require you to use deprecated headers, non-standard library functions, a non-standard type for `main`, and leak-prone manual memory management, as well as a strange definition of "odd" and "even"? If so, you should drop the course before you waste any more time, and teach yourself C++ from [a good book](http://stackoverflow.com/questions/388242). – Mike Seymour Nov 21 '14 at 12:31
  • @Mike Seymour.. this is the homework: Q1: Write a program which reads an integer N from the user. The program then allocates a float array of size N and reads the array elements from the user. The program then copies the elements with odd indicies into a new array called Odd and the elements with even indices into a new arrays called Even. Finally the program prints the Odd and Even Arrays. – Ruaa Brkat Nov 21 '14 at 12:33
  • 3
    @RuaaBrkat: Read it more carefully. It says "odd indicies" (sic), not "odd values". Your test should be `j%2` not `arr[j]%2`. – Mike Seymour Nov 21 '14 at 12:35

2 Answers2

3

You are trying to do <float> % <int>:

if (arr[j] % 2 == 1) {
//  ^^f^^^   ^i

There is no such operator. What you want to use instead is fmod, defined in the <cmath> library.

Barry
  • 286,269
  • 29
  • 621
  • 977
  • Is there another way without using cmath library and fmod operator, we didn't study them in University yet.. – Ruaa Brkat Nov 21 '14 at 12:31
  • @RuaaBrkat You could write that function yourself, it's not that complicated, just think about the definition of mod. Also, now that I think about it... what is an odd float anyway? – Barry Nov 21 '14 at 12:35
1

If you only have integer values, you could use int for arr:

int *arr = new int[N];
Laura Maftei
  • 1,863
  • 1
  • 15
  • 25