Sorting of arrays.

Why it do not sort array.It print only list of same numbers, which is last appear in the list..

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

void takeInput(int [] , int &);
void bubbleSort(int [] , int );
void printResult(int [] , int );

int main()
{
		int size;

	int a[size];
	

	takeInput(a, size);
	bubbleSort(a, size);
	printResult(a, size);
	
	system("pause");
    return 0;
}

void takeInput(int b[], int &SIZE)
{
		cout << "How many numbers you want to sort? \nJust enter the numbber and press ennter!\n";
        cin >> SIZE;
	cout << "Enter "<< SIZE << "  numbers:\n";
	for(int i=0; i<SIZE; i++)
	{
		cin >> b[SIZE];
	}
}

void bubbleSort(int c[], int Size)
{
	for(int pass=0; pass < Size-1; pass++)
	{
		for(int i=0; i< Size-1; i++ )
		{
			if(c[i] > c[i+1])
			{
				int hold;
				hold=c[i];
				c[i]=c[i+1];
				c[i+1]=hold;
			}
		}
	}
}

void printResult(int d[], int sizE)
{
	for(int j=0; j<sizE; j++)
	{
		cout << d[sizE] << endl;
	}
}

line 10 you declare size which in uninitialised, you then use size to set your array.

May be other issues but thats an important one :)
I want to take input from user I also wrote the statements fot taking size in the function
Main but still the program have same problem.

You cant initialise a array in this way, it expects a static numerical value; if you don't know the size needed then you have several options, you can either use the new operator to allocate your array, use a vector or create a array of a suitable size that you feel would be enough for the purpose of your program.



You're reading in the wrong values in your for loops.
You create and array of integers and give it a size.
Then you read in each integer and assign them a position in the array.

This code works but the user has to hit enter after each number.
Also I made your bubblesort a little more efficient.

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

const int SIZE=100; // creates a constant variable

void takeInput(int [] , int& );
void bubbleSort(int [] , int );
void printResult(int [] , int );

int main()
{
	int a[SIZE]; // creates an array with 100 locations
	int n;  // variable used for counting actual values in the array
	

	takeInput(a, n); // reads in the array and size of the array
	bubbleSort(a, n);
	printResult(a, n);
	
	system("pause");
    return 0;
}

void takeInput(int b[], int& n)
{
	cout << "How many numbers do you want to sort? \nJust enter the number and press enter!\n";
    cin >> n;
    cout << "Enter "<< n << " numbers:\n";
	for (int i =0; i < n; i++)
	{
	cin>> b[i];
	}
}

void bubbleSort(int c[], int n)
{
    int hold;
	for(int pass=0; pass < n-1; pass++)
		for(int i=0; i< n-(pass+1); i++ )
			if(c[i] > c[i+1])
			{
				hold=c[i];
				c[i]=c[i+1];
				c[i+1]=hold;
			}
}

void printResult(int d[], int n)
{
	for(int j=0; j<n; j++)
	{
		cout << d[j] << endl;
	}
}
Last edited on
Thanks @Momothegreat it works.
I have one more question,I limited your program to only taking values and printing out put it also works fine,but I have also made a similar program that does not work,I thik it is simmilar to yours one but not works like yours.......

Your modified program is:



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
63
64
#include <iostream>
using namespace std;

//const int SIZE=100; // creates a constant variable

void takeInput(int [] , int );
//void bubbleSort(int [] , int );
//void printResult(int [] , int );

int main()
{
	int c=5;
		//int b;  // variable used for counting actual values in the array

//	cout << "How many numbers do you want to sort? \nJust enter the number and press enter!\n";
    //cin >> b;
	int a[c]; // creates an array with 100 locations
	
 
	takeInput(a, c); // reads in the array and size of the array
	
	cout << "Numbers before sorting:\n";
	for(int m=0; m<c ; m++)
	cout << a[m]<< "   " ;
	
//	bubbleSort(a, n);
//	printResult(a, n);
	
	system("pause");
    return 0;
}

void takeInput(int b[], int n)
{
	//cout << "How many numbers do you want to sort? \nJust enter the number and press enter!\n";
    //cin >> n;
    cout << "Enter "<< n << " numbers:\n";
	for (int i =0; i < n; i++)
	{
	cin>> b[i];
	}
}

/*
void bubbleSort(int c[], int n)
{
    int hold;
	for(int pass=0; pass < n-1; pass++)
		for(int i=0; i< n-(pass+1); i++ )
			if(c[i] > c[i+1])
			{
				hold=c[i];
				c[i]=c[i+1];
				c[i+1]=hold;
			}
}

void printResult(int d[], int n)
{
	for(int j=0; j<n; j++)
	{
		cout << d[j] << endl;
	}
}*/



And mine one is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
void assignValuesToArray(int [], int);

int main()
{
	const int size =5;
		int a[size];


assignValuesToArray(a, size);

	for(int i=0; i<size; i++)         //information is hidden for main function, that what values are given to subscripted variales in function assignValuesToArray
	cout << a[i] << "  " << endl;    
	
return 0;
}
void assignValuesToArray(int array[], int SIZE)
{
	cout << "Enter five values to assign to indexed variables:\n";
	for(int j=0; j<SIZE ; j++)
	cin >> array[SIZE];
}
Is my comment is alegal reasoning or not?
I think you're a little confused about how arrays work.

For arrays, you create it with a set number of memory locations BUT that is different than it's actual size.

For example:
int array[25];
This creates an array of integers for at least 25 memory locations.

But I could only read data into only one memory location or up to 25.

Creating an array does not automatically determine the size of the array but simply allocates memory.

The size of the array is determined by input.

Also, another way of creating an array is declaring a variable as an integer constant and using that named variable.

For example:
1
2
3
4
5
const int MAX=25;

int main()
{
int array[MAX];


Constant declarations are made outside of the main function.
They are global to your whole program.

Now let's look at your new program.

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

const int size =5; // Constant declarations are made before main() 

void assignValuesToArray(int [], int); // Created a void function that passes an array and the size of the array. 

int main()
{
int a[size]; // Create an array of integers called 'a' for 5 memory locations. This is the same as "int a[5];". However 5 is not the size of the array, it is simply the amount of memory locations that it will write to!

assignValuesToArray(a, size); // Function call that sends the array and should send the size. However size is 5. So this doesn't really make sense.

for(int i=0; i<size; i++)     // 
cout << a[i] << "  " << endl;    
	
return 0;
}

void assignValuesToArray(int array[], int SIZE) // "SIZE" and "size" are different name is C++; lower case and upper case matters.
{
	cout << "Enter five values to assign to indexed variables:\n";
	for(int j=0; j<SIZE ; j++)
	cin >> array[j]; // replace "SIZE" with "j" .
}


The values read into the assignvaluestoarray are local to that function, but if were to pass by reference then you can use them again in your main function.

For more information, read this:
http://www.docdroid.net/117vu/arrays-ch8.pdf.html
Last edited on
thank you very much Momothegreat
Topic archived. No new replies allowed.