Arrays

So the idea is I will input 10 numbers and the program will arrange it into ascending order. I know I have to use arrays and for loops. I just don't know how to apply it. I need help with this one. I'll check back here when I finish the code I'm working on then post it here and see if anyone decides to help me with this.

Here take a look at this thread:

http://www.cplusplus.com/forum/general/112188/
Post your code then!
Here it is, but it's not working

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
#include <iostream>
#define MAX_VALUE 10
using namespace std;

int main ()
{
	int num[9];
	int num_outpt[9];
	cout << "Enter " << MAX_VALUE << " numbers" << endl;
	for (int i=0; i<MAX_VALUE; i++)
	{
		cin >> num[i];
		if (num[i]<MAX_VALUE)
		{
			num_outpt[i]=num[i];
		}
		else 
		{
			num_outpt[i++]=num[i];
		}
	}
	cout << "Element value of array in ascending order" << endl;
	for (int i=0; i<MAX_VALUE; i++)
	{
		cout << num_outpt[i] << " ";
	}
	system ("pause>0");
	return 0;
}


Any suggestions?
line 13 looks wrong to me. Why can't they pick a number greater than 10?

try i<MAX_VALUE
It doesn't sort the numbers I inputted although this is way better than my last code. Also, what's this?

Run-Time Check Failure #2 - Stack around the variable 'num_outpt' was corrupted.
Should the user be able to enter in numbers higher than 9? How about negative values?
un-Time Check Failure #2 - Stack around the variable 'num_outpt' was corrupted.


This error is probably due to this else statement:
1
2
3
4
else 
		{
			num_outpt[i++]=num[i]; //Here we can try to access element i[10] on iteration 9 of the for loop. 
		}


Why is that else statement there? It means that if the user enters a value of 10 or more, the next position in the array becomes that value, which is then immediately over-written with the next choice, or is writing out of bounds of the array.
Hello @sanasuke15
I have a different approach,
to solve this problem, using
functional decomposition and
const variable instead of #define MAX_VALUE 10
check this example and tell me what you think.


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
//BubbleSort.cpp
//Program that arrange numbers in ascending order.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

void printArray(int a[],const int size); //function prototypes
void setArray(int a[],const int size);
void sortArray(int a[],const int size);

int main(){

const int MAX_VALUE=15;

int myArray[MAX_VALUE]={};

setArray(myArray,MAX_VALUE);
sortArray(myArray,MAX_VALUE);
printArray(myArray,MAX_VALUE);

return 0; //indicates success
}//end main

void printArray(int array[],const int size){

	for(int i=0;i<size;i++){
		cout<<array[i]<<' ';
	}//end for
cout<<endl;
}//end function printArray

void setArray(int array[],const int size){
	cout<<"Enter "<<size<<" numbers"<<endl;
	for(int i=0;i<size;i++){
		cin>>array[i];
	}//end for
}//end function setArray

void sortArray(int array[],const int size){
bool swap=true;
int number_swap=0;
while(swap){
	for(int i=0;i<size-1;i++){
		if(array[i+1]<array[i]){
			int tmp;
			tmp=array[i+1];
			array[i+1]=array[i];
			array[i]=tmp;
			number_swap++;	
		}//end if
	}//end for
	
	if(number_swap==0)
		swap=false;
	else{
		number_swap=0;
		//printArray(array,size); output array numbers after each iteration
	}//end if..else
}//end while
}//end function sortArray 



Eyenrique-MacBook-Pro:Help Eyenrique$ ./BubbleSort
Enter 10 numbers
10 9 3 -8 2 7 4 6 5 -1
-8 -1 2 3 4 5 6 7 9 10
Topic archived. No new replies allowed.