beginner bubble sort .

im trying to write this program but i feel like i an too stupid to know what the hell i am doing. I dont know if im on the right track of i totally messed up.


The primary purpose of this program is exercising your knowledge of complex function calls and
nested looping statements. You are to read in a series of numbers into an array of fixed length not to
exceed 100 numbers. Your program will stop accepting input when a negative number is input. Once
the negative number is input, your program will sort the numbers using a unique sorting algorithm
called the Hamming Sort and output the sorted array and the average (mean).


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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
using namespace std;

void hamming_sort(int* numbers, int size)
{
	 int numb[10];
	int i, j;

 for(i=0;i<=9;i++)
 {
  
  cin >> numb[i];
 }

 for(i=0;i<=9;i++)
 {
  for(j=i+1;j<=9;j++)
  {
   int temp;

   if(numb[i] > numb[j])
   {
    temp = numb[i];
    numb[i] = numb[j];
    numb[j] = temp;
   }
 
  }
  
 }
 cout<<"sorted list"<<endl;

 for(i=0;i<=9;i++)
 { 
	 
  cout << endl <<  numb[i] << endl;
 }

int get_input(int* numbers)  
{
	int size = 0;         //counts numbers entered
	cout << "Enter numbers : ";

	while(size>=0 || size <=100)
	{
	for (int num; cin >> num && size < 100 ; ++size)
	{
		if (num < 0) break; //exit loop if negative number is entered

		else 
			numbers[size] = num; //else write number you got into the array
	}

	return size; //return number of integers entered
}
}

void print_array(int* numbers, int size)
{
	cout << "{ ";
	for (int i = 0; i < size; ++i)
	{
		cout << numbers[i];
		if (i + 1 < size) cout << ", ";
	}
	cout << " }\n";
}


int main()
{
	int number_array[100]; //create build in array for 100 integers

    //build in array implicitly converts to pointer to its 1st element
	int size = get_input(number_array);  

	//sort array
	hamming_sort(number_array, size); 

	//print out array
	print_array(number_array, size);
	//calculate the mean

	//and the most important part :D
	return 0;
}
Last edited on
Topic archived. No new replies allowed.