Reputation:
Hi there I am writing a program that is meant to take the numbers stored in an array and if they are even, add them up. However I keep running into this error
"main.c:9:16: warning: comparison between pointer and integer [enabled by default]
for (i = 0; i < MAX_LEN; ++i){
This is my program here I dont know if it will calculate properly or not, as I have never been able to successfully run it. Thanks for any help
#include <stdio.h>
int main() {
int MAX_LEN[15] = {10,12,52,131,15,84,3,4,11,14,32,2,1,6,7};
int i = 0;
int sum = 0;
for (i = 0; i < MAX_LEN; ++i){
if (MAX_LEN[i] % 2 == 0){
sum = sum + MAX_LEN[i];
}
else {
sum = sum;
}
}
printf("Sum: %d", sum);
return 0;
}
Upvotes: 1
Views: 40
Reputation: 1903
You have little mistake in your for loop (and your naming MAX_LEN for an array is little bit confusing)
for (i = 0; i < sizeof(MAX_LEN)/sizeof(int); ++i){ //size of the array not the array
if (MAX_LEN[i] % 2 == 0){
sum = sum + MAX_LEN[i];
}
else {
sum = sum;
}
}
Upvotes: 1
Reputation: 493
Your problem resolved, see the below C code snippet:
#include <stdio.h>
#define MAX_LEN 15U
int main() {
int my_array[MAX_LEN ] = {10,12,52,131,15,84,3,4,11,14,32,2,1,6,7};
int i = 0;
int sum = 0;
for (i = 0; i < MAX_LEN; ++i){
if (my_array[i] % 2 == 0){
sum = sum + my_array[i];
}
else {
sum = sum;
}
}
printf("Sum: %d", sum);
return 0;
}
Upvotes: 0