# Help with displaying an array in ascending order

Page 1 of 1

## 4 Replies - 435 Views - Last Post: 07 November 2017 - 06:23 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=407436&amp;s=21fbcc374ca18bdf3f8d0872809c53d2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hh38985

Reputation: 0
• Posts: 12
• Joined: 06-November 17

# Help with displaying an array in ascending order

Posted 06 November 2017 - 07:28 PM

So I'm having some problems with the output of my code, so far it's displaying the numbers from user input in ascending order, but my problem is that it's also displaying 0 before the numbers. (Displaying all the elements of the array) How do I fix this?

```int main() {
const int SIZE = 100;
int y[SIZE] = {};

int sort;

for (int i = 0; i <= 100;++i) {
cout << "Enter a value between 1-100 (Press -99 to stop): " << endl;
cin >> y[i];

if (y[i] == -99)
break;

}

for (int n = 0;n < SIZE - 1; n++) {
for (int j = 0; j < SIZE - 1;j++)
if (y[j] > y[j + 1]) {
sort = y[j];
y[j] = y[j + 1];
y[j + 1] = sort;
}
}

cout << "The numbers in ascending order\n ";
for (int p = 0; p <= SIZE;p++)
cout << y[p] << endl;
return 0;
}
```

Is This A Good Question/Topic? 0

## Replies To: Help with displaying an array in ascending order

### #2 snoopy11

• Engineering ● Software

Reputation: 1467
• Posts: 4,726
• Joined: 20-March 10

## Re: Help with displaying an array in ascending order

Posted 06 November 2017 - 08:21 PM

In your first for loop have a counter variable lets call it 'counter' after the

```cin >> y[i];

```

increment counter ie

```counter++;

```

in your second and third for loop replace all instances of SIZE with 'counter'

This post has been edited by snoopy11: 06 November 2017 - 08:21 PM
Reason for edit:: typo

### #3 hh38985

Reputation: 0
• Posts: 12
• Joined: 06-November 17

## Re: Help with displaying an array in ascending order

Posted 06 November 2017 - 08:33 PM

Thank you! That worked! My only problem now is that the last for loop is reading the -99 that was used to break out of the loop and displaying it. Why is it doing that and how could I fix it?

NVM i fixed it, my mistake it wasn't the for loop, but just the placement of the counter increment. Thank you so much for your help!

### #4 snoopy11

• Engineering ● Software

Reputation: 1467
• Posts: 4,726
• Joined: 20-March 10

## Re: Help with displaying an array in ascending order

Posted 06 November 2017 - 08:38 PM

You're Welcome !

### #5 Skydiver

• Code herder

Reputation: 6216
• Posts: 21,452
• Joined: 05-May 12

## Re: Help with displaying an array in ascending order

Posted 07 November 2017 - 06:23 AM

Also beware of this:
```const int SIZE = 100;
int y[SIZE] = {};
:
for (int i = 0; i <= 100;++i) {
:
for (int p = 0; p <= SIZE;p++)
:

```

Arrays in C and C++ are zero based. So if you have SIZE elements, the valid indices are only from 0 to SIZE-1. Your comparison using <= puts you out of bounds. The C/C++ idiom is simply to use the < operator.