Arrays

I need to find a way to showcase inputed numbers and their categories from 1 - 10 for an average of the even and odd elements. it should look like this:
Array[1]: 10
Array[2]: 10
Array[3]: 10
Array[4]: 10
Array[5]: 10
Array[6]: 10
Array[7]: 10
Array[8]: 10
Array[9]: 10
Array[10]: 10
But now it wont show up at all. The program still runs but the arrays are absent. 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
#include <cstdlib>
#include <iostream>

using namespace std;

int main () {
   int j;
   int x;
   cout << "Enter 10 numbers.\n";
   cin >> j;
   int array[x];

   for(int i = 0; i < x; i++)
   {
      cout << "Array[" << i + 1 << "]: " << x << endl;
   }
   void classifyNumber(int num, int& zeroCount, int& odd_average, int& even_average);
   int even_average = 0;
   int odd_average = 0;
   for(int z = 0; z < x; z++)
   {
      if(array[z % 2 == 0]);
      {
         even_average = even_average / array[z];
      }

      if(array[z % 1 == 0]);
      {
         odd_average = odd_average / array[z];
      }
   }
   cout << "Average of the elements with even index is: " << even_average  << endl;
   cout << "Average of elements with odd index is: " << odd_average << endl;

   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(2);

   return 0;
}


Please help if u can.
Last edited on
Click Edit, select all your code and click the button on the side that looks like <>.
That's called code tags, and makes your program readable if it's formatted very well.
and will look like Your code here
thx
1) Line 10 is illegal C++. When delcaring an array on the stack, you can only use a constant value for the size.

(And even if it were legal, you haven't actually specified a value for x.)

2) What is j for? You expect the user to input a value into it, but it never gets used anywhere.

3) Nowhere do you ever populate array with any values, so when you're reading values from it, you're reading nonsense.

4) The expression z % 2 == 0 can only ever evaluate to false or true. When converted to integers, this means it can only ever evaluate to 0 or 1. So at line 22, you're only ever looking at the first two elements of the array.

The same is true for line 27.

5) At line 22, the semicolon at the end of the line terminates the entire if statement. This means that your code is the same as:

1
2
3
4
5
      if(array[z % 2 == 0])
      {
         ;
      }
      even_average = even_average / array[z];


The same is true for line 27.

You should remove the semicolons at the ends of those lines.

6) At lines 35 - 37, what is the point of changing the output format on cout after you've finished outputting any text? Shouldn't you have done that before you output anything?
When I make Line 10 constant, it just give me WAY more errors.

J is what the user actually inputs while X is supposed to be the numbered array, like Array[1], Array, [2], Array [3].

What do I change Line 22 and 27 too?
what errors does it give? should look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdlib>
#include <iostream>

using namespace std;

int main () {
   int j;
   const int x = 10;
   cout << "Enter 10 numbers.\n";
   cin >> j;
   int array[x];
  cout << "Enter 10 numbers.\n";
   for(int i = 0; i < x; i++)
     cin >> array[i];
Last edited on
Did that now it gives this.

1
2
3
4
5
Enter 10 numbers.
10 16 21 44 31 8 74 6 3 61
Array[1]: 4197153
Average of the elements with even index is: 0
Average of elements with odd index is: 0
The size needs to be constant, not the array itself.

1
2
3
4
5
6
7
8
9
const int x = 10;
int array[x];
cout << "Enter 10 numbers.\n";
for (int i = 0; i  < x; i++)
{
    int j;
    cin >> j;
    array[i] = j;
}


If the size of the array is not known at compile time, use new/delete for dynamic memory allocation, or preferably use std::vector.

line 15 you're outputting x, which is the size of your array. If you're trying to output the value of the array at index 'i', that's array[i].

Line 22/27 you want to check if the index is even/odd. Your index is z, so
1
2
3
4
if (z%2 == 0)
//index is even
else
//index is odd 


What your code says right now at line 22 is, "check if the index is divisible by two, if it is, check if array[0] is not zero, otherwise, check if array[1] is not zero."

Do you know how to compute an average? Your code is not computing an average. I'm guessing you do know how but just don't know how to write code for it.

You should find the sum of elements with an even index and divide by the number of elements with an even index. Do the same for odd indexes.
Last edited on
Thats what I was doing with

 
even_average = even_average / array[z];
Topic archived. No new replies allowed.