Array Returning nothing but 0000

I am trying to write an array with user input, read, sort, and print. I have the beginning done, and do not want any help with the rest, what i am looking for is to why my code is returning nothing but 0 for each index.

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

//GLOBAL Declarations

const int SIZE = 10; // array size
int numArray[SIZE]; // array
int count, temp; // variable for loops, and bubble sorts


int main()
{
cout << "Please enter 10 numbers in any order and i will sort them in ascending " <<
"order from lowest to highest" << endl;

for (count =0; count <= SIZE; count++)
{
cin >> numArray[SIZE];
}

cout << "This is the list as you have entered it: " << endl;

for(int j = 1; j < SIZE; j++)
{
cout << numArray[j] << endl;
}
return 0;
}
1
2
3
4
for (count =0; count <= SIZE; count++)
{
cin >> numArray[SIZE];
}


I think you may be using the wrong variable, "SIZE" = 10;

The tenth position of the array is 9.

EDIT - So you may want to change that for-loop to better represent what you want.

That's as much help as I can give without giving you code.

EDIT 2 - Chervils response is much better at explaining what you have done wrong exactly
Last edited on
The zeros are the initial values of the array. Its contents are never changed. However there is an out-of-bounds access to an element which is not part of the array.

Two errors here:
1
2
3
4
for (count =0; count <= SIZE; count++)
{
    cin >> numArray[SIZE];
}


<= means loop will repeat one time more than needed.
numArray[SIZE] is not a valid element, it is one past the end of the array.

It should be:
1
2
3
4
for (count =0; count < SIZE; count++)
{
    cin >> numArray[count];
}


closed account (E0p9LyTq)
You are putting your input into the same array element, the 11th element. Elements 0-9 are uninitialized so you output whatever value was in them when you output your array.

Also, you start your output with the 2nd element.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

const int SIZE = 10; // array size
int numArray[SIZE]; // array
int count, temp;  // variable for loops, and bubble sorts

int main()
{
   std::cout << "Please enter 10 numbers in any order and i will sort them in ascending\n"
      << "order from lowest to highest\n";

   for (count = 0; count < SIZE; count++)
   {
      std::cin >> numArray[count];
   }

   std::cout << "\nThis is the list as you have entered it: ";

   for(int j = 0; j < SIZE; j++)
   {
      std::cout << numArray[j] << " ";
   }
   std::cout << "\n";
}


Please enter 10 numbers in any order and i will sort them in ascending
order from lowest to highest
0 9 1 8 2 7 3 6 4 5

This is the list as you have entered it: 0 9 1 8 2 7 3 6 4 5
Thank you everyone. it is always the simple small stuff. IM just learning programming so i appreciate the rapid feedback.
We've all been there perkins, all been there. If you have any small questions you don't deem worthy of a post, PM me anytime and I'll see if I can help you out.
Last edited on
Topic archived. No new replies allowed.