4 Replies - 334 Views - Last Post: 07 November 2017 - 06:23 AM Rate Topic: -----

#1 hh38985  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • 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  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,318
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 hh38985  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • 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!
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,318
  • Joined: 20-March 10

Re: Help with displaying an array in ascending order

Posted 06 November 2017 - 08:38 PM

You're Welcome !
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5898
  • View blog
  • Posts: 20,136
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1