How do I get the users numbers stored in a array so that I can use the bubble so
ID: 3607645 • Letter: H
Question
How do I get the users numbers stored in a array so that I can use the bubble sort function to sort them? Also how could I include the function properly so that it works
Here is the code I have so far :
the .main file :
#include
using namespace std;
#include
#include "Header.h"
void main()
{
int numbers;
cout << "How many numbers do you want to enter?";
cin >> numbers;
int usernum(numbers);
cout << " Please enter the numbers: ";
for (int k = 0; k < numbers; k++)
cin >> usernum;
cout << "Unsorted numbers are " << usernum << endl;
Sort(usernum);
cout << "Sorted letters are " << usernum << endl;
system("pause");
}
then the .h file :
#ifndef SINKING_SORT_H
#define SINKING_SORT_H
void Sort(int[]);
#endif
And at last the other .cpp file :
#include
using namespace std;
#include
#include "Header.h"
void main()
{
int numbers;
cout << "How many numbers do you want to enter?";
cin >> numbers;
int usernum(numbers);
cout << " Please enter the numbers: ";
for (int k = 0; k < numbers; k++)
cin >> usernum;
cout << "Unsorted numbers are " << usernum << endl;
Sort(usernum);
cout << "Sorted letters are " << usernum << endl;
system("pause");
}
Explanation / Answer
Hello,
the following is the code that you had requested. In the CPP, we accept the user input and store them in an integer array. The array is passed to a bubble sort function and returned.
In the main method, the returned values are printed.
Hope this code helps to you understand. Please rate the answer.
// Header.h
#ifndef SINKING_SORT_H
#define SINKING_SORT_H
void Sort(int a[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (a[j] > a[j + 1])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
#endif
//SinkingSort.cpp
#include <iostream>
#include <cstdlib>
#include "Header.h"
using namespace std;
int main()
{
int n;
int numbers[50]; //maximum 50 numbers
cout << "How many numbers do you want to enter?";
cin >> n;
cout << " Please enter the numbers: ";
for (int k = 0; k < n; k++)
{
cin >> numbers[k];
}
cout << "Unsorted numbers are " << endl;
for (int k = 0; k < n; k++)
{
cout << numbers[k] << endl;
}
Sort(numbers, n);
cout << "Sorted numbers are " << endl;
for (int k = 0; k < n; k++)
{
cout << numbers[k] << endl;
}
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.