Even and odds

Im trying to make a program that will print several stats, including the average of the even and odds of 10 numbers. I have finished them all and they seemed to work fine. When I programmed the averages, it was rejected and even gave errors to the same things that worked for the other stuff. Here's the code.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  #include <cstdlib>
#include <iostream>

using namespace std;

void getData(int array[], int size);
void printData(int array[], int size);
int lowestValue(int array[], int size);
int highestValue(int array[], int size);
int countEvens(int array[], int size);
int countOdds(int array[], int size);
int sum(int array[], int size);
int average(array[], int size);
int new_average(array[], int size);
int* getDuplicateValues(int array[], int out[], int size);

int main() {

    int array[10], out[10];

    getData(array, 10);

    printData(array, 10);

    int low = lowestValue(array, 10);

    int high = highestValue(array, 10);

    int odds = countOdds(array, 10);

    int evens = countEvens(array, 10);

    int sum1 = sum(array, 10);

    int average1 = average(array, 10);

    int average2 = new_average(array, 10);

    int *out1 = getDuplicateValues(array, out, 10);

cout << "Lowest element : " << low << endl;

cout << "Highest element : " << high << endl;

cout << "Odd counts : " << odds << endl;

cout << "Sum of array : " << sum1 << endl;

cout << "Even Average of the array : " << average1 << endl;

cout << "Odd Average of the array : " << average2 << endl;

cout << "Duplicate elements : ";

for (int i = 0; i < 10; i++)

if (*(out + i) != 1)

cout << *(out + i) << " ";

cout << endl;

return 0;
}

void getData(int array[], int size)
{

cout << "Enter 10 integers (separated by a space): : " << endl;

for (int i = 0; i < size; i++)

cin >> array[i];
}

void printData(int array[], int size)
{

for (int i = 0; i < size; i++)

cout << "Array[" << i << "]: "  << array[i] << endl;

cout << endl;
}

int lowestValue(int array[], int size)
{

int min = array[0];

for (int i = 1; i < size; i++)

if (array[i] < min)

min = array[i];

return min;
}

int highestValue(int array[], int size)
{

int max = array[0];

for (int i = 1; i < size; i++)

if (array[i] > max)

max = array[i];

return max;
}

int countOdds(int array[], int size)
{

int count = 0;

for (int i = 0; i < size; i++)

if (array[i] % 2 != 0)

count++;

return count;
}

int countEvens(int array[], int size)
{
    int count = 0;

    for (int i = 0, i < size; i++)

    if (array[i] % 2 = 0)

    count++

    return count;
}

int sum(int array[], int size)
{

int sum = 0;

for (int i = 0; i < size; i++)

sum += array[i];

return sum;
}

int average(int array[], int size)
{

    int average = 0;

    for (int i = 0; i < size; i++)

    sum / evens;

    return average;
}

int new_average(int array[], int size)
{
    int new_average = 0;

    for (int i = 0; i < size; i++)

    sum / odds;

    return new_average;

}

int* getDuplicateValues(int array[], int out[], int size) {

for (int i = 0; i < size; i++)

out[i] = 1;

for (int i = 0; i < size; i++)

{

int count = 1;

for (int j = i + 1; j < size; j++)

{

if (array[i] == array[j])

count++;

}

if (count > 1)

out[i] = array[i];

}

return out;

} 


plz help?
Last edited on
Oh and here it is without the even and odds.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
 
#include <cstdlib>
#include <iostream>

using namespace std;

void getData(int array[], int size);
void printData(int array[], int size);
int lowestValue(int array[], int size);
int highestValue(int array[], int size);
int countOdds(int array[], int size);
int sum(int array[], int size);
int* getDuplicateValues(int array[], int out[], int size);

int main() {

int array[10], out[10];

getData(array, 10);

printData(array, 10);

int low = lowestValue(array, 10);

int high = highestValue(array, 10);

int odds = countOdds(array, 10);

int sum1 = sum(array, 10);

int *out1 = getDuplicateValues(array, out, 10);



cout << "Lowest element : " << low << endl;

cout << "Highest element : " << high << endl;

cout << "Odd counts : " << odds << endl;

cout << "Sum of array : " << sum1 << endl;

cout << "Duplicate elements : ";

for (int i = 0; i < 10; i++)

if (*(out + i) != 1)

cout << *(out + i) << " ";

cout << endl;

return 0;
}

void getData(int array[], int size) {

cout << "Enter 10 integers (separated by a space): : " << endl;

for (int i = 0; i < size; i++)

cin >> array[i];
}

void printData(int array[], int size) {

for (int i = 0; i < size; i++)

cout << array[i] << " ";

cout << endl;
}

int lowestValue(int array[], int size) {

int min = array[0]; //set first element as min

for (int i = 1; i < size; i++) //iterates throw all the elements in array

if (array[i] < min) //if any value is found less than min

min = array[i]; //update min

return min; //return min to called function
}

int highestValue(int array[], int size) {

int max = array[0]; //set first element as max

for (int i = 1; i < size; i++) //iterates throw all the elements in array

if (array[i] > max) //if any value is found greater than max

max = array[i]; //update max

return max; //return max to called function
}

int countOdds(int array[], int size) {

int count = 0;

for (int i = 0; i < size; i++) //iterates throw all the elements in array

if (array[i] % 2 != 0) //counts odd numbers

count++;

return count; //return count
}

int sum(int array[], int size) {

int sum = 0;

for (int i = 0; i < size; i++) //iterates throw all the elements in array

sum += array[i]; //summation of all elements

return sum; //return sum
}

int* getDuplicateValues(int array[], int out[], int size) {

for (int i = 0; i < size; i++)

out[i] = 1;

for (int i = 0; i < size; i++)

{

int count = 1;

for (int j = i + 1; j < size; j++)

{

if (array[i] == array[j])

count++;

}

if (count > 1)

out[i] = array[i];

}

return out;

}
closed account (SECMoG1T)
hello there , your average fns seems to have a few bugs:
1. always remember to define your variables-> first post line 160 and 171, variables evens,sum,odds aren't defined.

2. if you want to find the average of all even or odd numbers in a range then , find their count and their sum within that range and use that to calculate the ave;

example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
///calculates average of all even nums in an array
int even_ave(int arr[],int size)
{
    int even_count=0, even_sum=0;

    for(int indx =0; indx < size; indx++)
    {
        if(arr[indx]%2 == 0)
        {
            ++even_count;
            even_sum += arr[indx];
        }
    }

    int even_average = (even_sum / even_count);

    return even_average;
}
Last edited on
Hello Warzombie3701,

When I tried to compile your first code there were many errors. You should compile and try to fix as many errors as you can and ask about the ones you do not understand. Also post the entire error message not what you think it means.

Yolanda has pointed out part of the errors. I also found a missing semi-colon and an "=" that should be "==".

Your prototypes for the averages need to match the function definitions. The prototypes are missing the variable type.

In addition to Yolanda's example you could do both averages with only one function. I have not written this yet, but I am thinking of starting with:
int average(int array[], int size, bool evenOdd) and along with Yolanda's example and a change to
if (array[indx] % 2 == evenOdd). This way sending a 0 or a 1 to the function determines if your mod 2 is for even or odd numbers thus you can use one function for both.

Here are some tips:

The beginning of "main"
1
2
3
4
5
6
7
8
9
int main()
{
	constexpr size_t MAXARRSIZE{ 10 };
	constexpr bool EVEN{ 0 };  // <--- Or false.
	constexpr bool ODD{ 1 };  // <--- Or true.

	int array[MAXARRSIZE]{ 2, 10, 3, 11, 4, 12, 5, 13, 6, 14 }, out[MAXARRSIZE];

	//getData(array, MAXARRSIZE); 

"size_t" is basically another name for "unsigned int". When you define the size of the array it must be a positive number and a constant number or variable. This way you can use "MAXARRSIZE" in place of the magic numbers like int low = lowestValue(array, 10); becomes int low = lowestValue(array, MAXARRSIZE);. Now if you change "MAXARRSIZE" at the beginning of the program it is reflected all through "main" and you do not have to hunt for every line that needs changed.

On line 7 by initializing the array this way and commenting out line 9 yo do not have to enter the ten numbers every time the program runs and you can change the numbers on line 7 to anything you want or any way you want. By changing the vallue of "MAXARRSIZE" you can change the size of the array and add more numbers to work with.

Hope that helps,

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int even_ave(int array[],int size)
{
    int even_count = 0, even_sum = 0;

    for(int i = 0; i < size; i++)
    {
        if(array[i]%2 == 0)
        {
            ++even_count;
            even_sum += array[i];
        }

        return even_ave;
    }

int odd_ave(int array[],int size)
{
    int odd_count - 0, odd_sum = 0;

    for(int - 0; i < size i++)
    {
        if(array[i]%2 != 0)
        {
            ++odd_count;
            odd += array[i];
        }
        odd_ave;
    }

    return odd_ave;
}


From 148 to the end Im getting weird errors like this

1
2
3
4
5
6
7
8
9
10
C.cpp: In function ‘int even_ave(int*, int)’:
C.cpp:160:16: error: invalid conversion from ‘int (*)(int*, int)’ to ‘int’ [-fpermissive]
         return even_ave;
                ^
C.cpp:164:1: error: a function-definition is not allowed here before ‘{’ token
 {
 ^
C.cpp:178:1: error: expected ‘}’ at end of input
 }
 ^


Can u explain?
lines 1/16: You're function is finding an average. Averages are often decimal numbers. You need to return a floating point data type like float or double, not int.

lines 7/22, I thought you wanted the average of elements with even or odd indices? You're summing even/odd elements here, not elements with even/odd indices.

line 13: Your return statement is inside your loop. Also, even_ave is not the name of any variable. It's the name of your function. You need to calculate the average and return it.

line 18: odd_count - 0? You mean odd_count = 0, right?

line 20: do you mean int i = 0?

line 25: Do you mean odd_sum += array[i]?

line 27: Not sure what's supposed to happen here

line 30: odd_ave is not the name of any variable.
closed account (SECMoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
///from brownie , double is the brst choice because it doesn't loose precision
double even_ave(int array[],int size)
{
    int even_count = 0, even_sum = 0;

    for(int i = 0; i < size; i++)
    {
        if(array[i]%2 == 0)
        {
            ++even_count;
            even_sum += array[i];
        }
     }
     ///after this loop you need to calculate the average, average = total sum / count;

      /// return even_ave; this is wrong even_ave is the name of this function, you can't do this.

    double average = (even_sum / even_count);///do something like this
    return average;
}

double odd_ave(int array[],int size)
{
    int odd_count = 0, odd_sum = 0;

    for(int i = 0; i < size; i++)
    {
        if(array[i]%2 != 0)
        {
            ++odd_count;
            odd_sum += array[i];
        }
    }

    double average = (odd_sum / odd_count);

    return average;
}
Last edited on
Thanks guys, everything works, except now it doesnt calculate decimals.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Enter 10 integers (separated by a space): : 
10 16 21 44 31 8 74 6 3 61
Array[0]: 10
Array[1]: 16
Array[2]: 21
Array[3]: 44
Array[4]: 31
Array[5]: 8
Array[6]: 74
Array[7]: 6
Array[8]: 3
Array[9]: 61

Average of elements with an even index: 26.00
Average of elements with an odd index: 29.00
Last edited on
It doesn't output decimals because when you're performing division on integer data types. The result of integer division always gets truncated. Someone correct me if I'm wrong, but I believe you only need to cast one of the operands to double, and it doesn't matter which one.

double average = odd_sum/double(odd_count);
or
double average = double(odd_sum)/odd_count;

Also, you are not taking the average of elements with even/odd indices. You are taking the average of even/odd elements. Yolanda's post does not answer to your question as you posed it.
Last edited on
closed account (SECMoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int countOdds(int array[], int size)
{

int count = 0;

for (int i = 0; i < size; i++)

if (array[i] % 2 != 0) ///the user clearly intends to check for odd values in the array , not odd indices  

count++;

return count;
}

int countEvens(int array[], int size)
{
    int count = 0;

    for (int i = 0, i < size; i++)

    if (array[i] % 2 = 0) //same here

    count++

    return count;
}


Last edited on
I am not assuming, you are. How can you guess OP's intentions based off of a couple lines of his code when it is riddled with errors like "for (int - 0)?"

OP explicitly stated that he was looking for the average of even/odd indices.

Warzombie3701 wrote:
Average of elements with an even index: 26.00
Average of elements with an odd index: 29.00

And in his other thread:
http://www.cplusplus.com/forum/beginner/246002/
If we're going to guess his intention based off of his code, what do you think this means?
1
2
3
for(int z = 0; z < x; z++)
   {
      if(array[z % 2 == 0]);

In that thread he explicitly stated that the output should be the average of numbers with even/odd indices, as well.
closed account (SECMoG1T)
the OP should simply clarify .
Last edited on
@Browni3141,

You wrote:


If we're going to guess his intention based off of his code, what do you think this means?
1
2
3
4
5
for(int z = 0; z < x; z++)
   {
      if(array[z % 2 == 0]);

	



I would guess it means that OP has no idea how to write code.

Andy
It works great thx so much you guys
Topic archived. No new replies allowed.