i need help with array of searching and sorting

Implement a Bubble Sort.

In main, declare an array of 10 integers.

Create 3 function to inputArray, sortArray, displayArray

In main,

call inputArray

call displayArray

call sortArray (implement the Bubble Sort)

call displayArray

im new to c++ and i cannot code to save my life if someone could help me and explain what they are doing it would help so much!
> im new to c++ and i cannot code to save my life
So have you ever written any code, or have you just been getting by by getting others to write your homework for you?

I mean, if you've been paying attention and practising up to now, you should at least be able to
- declare an array of 10 integers.
- write a loop to input 10 integers.
- write a loop to print 10 integers.

Maybe even to move said loops into separate functions called inputArray and displayArray.
If you can manage that much, we can talk sorting.
If you can't manage that much, other things need fixing before you even need to worry about sorting.

If you're serious about wanting to learn (and this is not just some elective you need to 'pass and forget'), then you need to start applying yourself with more vigour. It's going to get much harder than this.
Don't start a new thread on this topic! Keep posting in this thread.
#include <iostream>
using namespace std;

#include <iomanip>
using std::setw;

void bubbleSort(int[], int);
void swap(int &, int &);

int main()
{
const int SIZE = 10;
int values [SIZE] ={}


system("pause");
return 0;
}
Forget about the bubbleSort (and swap) for now.
Can you do this?

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
    const int SIZE = 10;
    int values[SIZE];

    // Write a for loop to read SIZE ints into values


    system("pause");
}


And make sure you use [code] [/code] tags for posting code.
i know how to do some of it but what goes in the for ()<---- what goes inside this?
would this work for(maxElement = size - 1; maxElement > 0; maxElement--)
or no?
Last edited on
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
#include <iostream>
using namespace std;

void bubbleSort(int[], int);
void swap(int &, int &);


int main() 
{
	
	const int SIZE = 10;
	int values[SIZE] = { 6, 1, 2, 4, 5, 8, 7, 9, 10, 3 };

	cout << "the unsorted values: \n";
	for (auto element : values)
		cout << element << " ";
	cout << endl;

	bubbleSort(values, SIZE);

	cout << "the sorted values: \n";
	for (auto element : values)
		cout << element << " ";
	cout << endl;




	system ("pause");
	return 0;
}
void bubbleSort(int array[], int size)
{
	int maxElement;
	int index;

	for (maxElement = size - 1; maxElement > 0; maxElement--)
	{
		for (index = 0; index < maxElement; index++)
		{
			if (array[index] > array[index + 1])
			{
				swap(array[index], array[index + 1]);
			}
		}
			
	}
}

void swap(int &a, int &b)
{
	int temp = a;
	a = b;
	b = temp;
}

i did this and it works but i don't know how to add the other functions into this part...
> but i don't know how to add the other functions into this part
I'm not surprised you can't.

If you had written the code yourself, then adding the other functions would be self evident.

But as always, you've googled your way to half an answer without understanding anything at all.
Your bubble sort directly lifted from this year old thread.
https://www.reddit.com/r/cpp_questions/comments/7crm75/error_help/

The printing loops you lifted from here
https://www.coursehero.com/file/28370026/Pr8-5cpp/

Look, if you're not prepared to actually start writing your own code from scratch, then frankly you're wasting everyone's time (including your own).

There's no point pretending that at some future time that you'll magically 'know' C++, you won't.
Sure, you might be able to con your educational institution for long enough to get that piece of paper affirming your competence.
It may even be enough for you to get a job as a programmer, but for sure it won't be enough for you to keep that job.





To write your own code from scratch, just follow naively your assignment.

>>Implement a Bubble Sort.

>>In main, declare an array of 10 integers.

 
int array[10];


>>Create 3 function to inputArray, sortArray, displayArray

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void inputArray()
{
    //write here some statement for input
    //e.g. for(int i=0; i<10)
    //         cin>>array[i];
}

void sortArray()
{
    //write here Bubble Sort to sort array[]  
}

void displayArray()
{
    //write here output code
    //e.g. for(int i=0; i<10)
    //         cout<<array[i]<<' ';
}


>>In main,

>>call inputArray

>>call displayArray

>>call sortArray (implement the Bubble Sort)

>>call displayArray

1
2
3
4
5
6
7
8
9
int main()
{
    inputArray();
    displayArray(); //unsorted
    sortArray();
    displayArray(); //sorted
	
    return 0;
}




Summarize...

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

int array[10];

void inputArray()
{
    //write here some statement for input
    //e.g. for(int i=0; i<10)
    //         cin>>array[i];
}

void sortArray()
{
    //write here Bubble Sort to sort array[]  
}

void displayArray()
{
    //write here output code
    //e.g. for(int i=0; i<10)
    //         cout<<array[i]<<' ';
}

int main()
{
    inputArray();
    displayArray(); //unsorted
    sortArray();
    displayArray(); //sorted
	
    return 0;
}



You can see that your task is easy.
What you need to do is to complete 3 functions (inputArray, sortArray and displayArray).

Show me what's your can!
Topic archived. No new replies allowed.