Hey, we just started learning functions in class. We were given a project to complete. I'm trying to work out the errors, but I'm stuck with
error C2084: function 'void bubblesort(int [],int)' already has a body
error C2084: function 'void print(int [],int)' already has a body
error C2084: function 'void printH(int [],int)' already has a body
How do I call the functions differently to get them to not conflict?
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
void bubblesort(int [], int );
void bubblesort(int *, int ); // function overloading to sort characters
void print(int[], int );
void printH(int[], int );
void print(int *, int ); // function overloading to print characters
void printH(int *, int ); // function overloading to print histogram for characters
int main()
{
int i, magic, size;
int high;
int low;
int a[100]; // declare an integer array named a with size 100
int b[100]; // declare a character array named b with size 100
cout << "how many random numbers do you want to create? (no more than 100)\n";
cin >> size; // read user input into variable size
if (size > 100)
{
cout << "too many, program ends.\n";
return -1;
}
cout <<"enter the smallest and largest random number you want to create:\n";
cin >> low >> high; // read user input into variables low and high
srand(time(NULL));
for ( int i = 0; i < size; i++) //write a for loop to run the code inside { }. The total loops = size.
{
magic = rand() % (high-low+1) + low;
a[i] = magic;
srand(time(NULL)+i*i+3);
}
cout << "\nthe random numbers generated are: \n";
cout << a << endl; //write a statement to call print function to print a
bubblesort (a, size);
cout << "\nthe random numbers after sort:\n";
cout << a << endl; //write a statement to call print function to print a
cout << "\nthe histogram of the numbers:\n";
printH(a, size);
cout << "\n\nhow many random lower case characters do you want to create? (no more than 100)\n";
cin >> size;
if (size > 100)
{
cout << "too many, program ends.\n";
return -1;
}
high = 122;
low = 97;
srand(time(NULL));
for (i = 0; i < size; i++)
{
magic = rand() % (high-low+1) + low;
b[i] = magic;
srand(time(NULL)+i*i+3);
}
cout << "\nthe characters generated are: \n";
print(b, size);
bubblesort (b, size);
cout << "\nthe characters after sort are: \n";
print(b, size);
cout << "\nthe histogram of the charaters:\n";
printH(b,size);
return 0;
}
void bubblesort( int x[], int len )
{
//write function definition to bubble sort the integer array x[], len is the size of x[]
for(int i=i; i<len; i++){
for(int j=len-1; j>=i; j--) {
if(x[j-1] > x[j]) {
j = x[j-1];
x[j-1] = x[j];
x[j] = j;
}
}
}
}
void bubblesort( int x, int size )
{
//write function definition to bubble sort a character array
for(int i = 0; i < size; i++){
for(int j = i + 1; j < size - 1; j++){
if(x[j-1] > x[j]){
char temp = x[j];
x[j] = x[j-1];
x[j-1] = temp;
}
}
}
}
void print (int a[], int len)
{
//write function definition to print an integer array
for(int i=0; i<len; i++){
cout << a[i] << " ";
}
}
void printH (int a[], int len)
{
//function definition to print histogram of a sorted integer array
int current, shows;
current = -1;
shows = 0;
for (int i = 0; i < len; i++)
{
if (current != a[i] )
{
current = a[i];
cout << "\n" << a[i] << " *";
}
else
cout << "*";
}
cout << endl;
}
void print (int *a, int len)
{
//write function definition to print a character array
for(int i=0; i<len; i++){
cout << a[i] << " ";
}
}
void printH (int *a, int len)
{
//write function definition to print histogram of a sorted character array
char temp;
for(int i = 0; i < len; i++){
for(int j = i + 1; j < len - 1; j++){
if(a[j-1] > a[j]){
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
}