I
inkexit
I need help figuring out what is wrong with my code. I posted here a
few weeks ago with some code about creating self similar melodies in
music. The coding style I'm being taught is apparently a lot different
from what the pros around here use. I really need help with debugging
some program errors more than anything, even though my coding style
might not be perfect.
Anyway here is my code. About the only things that work right are the
max, min, and insertion sort functions. I can't figure out where I
went wrong. If you are wondering, yes, this is homework, but I've
gotten as far as I could by myself with this and won't be able to ask
my teahcer for help until after the assignment is due this week.
Hopefully I could still get some help here as I have written a lot of
code here and am not simply asking somebody to go out and write
everything for me.
Thanks all.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void bubble_sort(int []);
void print_array(int []);
const int ARRAY_SIZE = 11;
int numbers[ARRAY_SIZE];
int search_for_number;
int check[ARRAY_SIZE][ARRAY_SIZE];
void bubble_sort(int array[])
{
int swap = 0;
do
{
int tmp;
for(int j=0; j< ARRAY_SIZE-1; j++)
{
if(array[j] > array[j+1])
{
tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
swap = 1;
}
}
}
while( swap == 1 );
}
void insertion_sort(int array[])
{
int i, j;
for(i = 1; i < ARRAY_SIZE; i++)
{
int value = array;
for (j = i - 1; j >= 0 && array[j] > value; j--)
{
array[j + 1] = array[j];
}
array[j + 1] = value;
}
}
int min(int array[])
{
return (array[0]);
}
int max(int array[])
{
return (array[ARRAY_SIZE-1]);
}
int median(int array[])
{
int median = ARRAY_SIZE/2;
return (array[median]);
}
float mean(int array[])
{
int total = 0, i;
float mean;
for (i = 1; i < ARRAY_SIZE; i++)
{
total += array;
mean = total / ARRAY_SIZE;
return (mean);
}
}
int mode(int array[])
{
int what_number;
int count, mode = check[0][0], j, i, h;
for (j = 0; j < ARRAY_SIZE; j++)
{
what_number = numbers [j];
count = 0;
for (i = 0; i < ARRAY_SIZE; i++)
{
if (numbers = what_number)
{
count++;
check [j][1] = count;
}
}
}
for (h = 0; h < ARRAY_SIZE; h++)
{
if (check [h+1] [1] > check [h] [1])
mode = check [h+1] [0];
}
return (mode);
}
void search(int array[], int search_for_number)
{
int i, found_number = 0;
for (i = 0; i < ARRAY_SIZE; i++)
{
if (array == search_for_number)
cout << endl << search_for_number << " is at index " << array;
found_number = 1;
if (i == ARRAY_SIZE-1 && found_number == 0)
cout << endl << search_for_number << " is not in the list of
numbers.";
}
}
void print_array(int array[])
{
cout << setiosflags(ios::right);
for(int i=0; i < ARRAY_SIZE; i++)
cout << endl << array;
cout << endl;
cout << setiosflags(ios::left);
}
int main()
{
int Count = 0, search_for_number;
char sort_method;
sort_method = 'f';
cout << "Enter 11 integer numbers.\n\n";
do
{
cout << "Enter Number " << (Count + 1) << ":";
cin >> numbers[Count];
Count++;
}
while ( Count < 11 );
do
{
cout << "Sort with bubble sort (b) or insertion sort (i)?";
cin >> sort_method;
}
while ( sort_method != 'i' && sort_method!= 'b' );
cout << "The numbers sorted in ascending order are: ";
if (sort_method == 'i')
{
insertion_sort(numbers);
print_array(numbers);
}
if (sort_method == 'b')
{
bubble_sort(numbers);
print_array(numbers);
}
cout << "\nThe minimum of the numbers is: " << min(numbers);
cout << "\nThe maximum of the numbers is: " << max(numbers);
cout << "\nThe mode of the numbers is: " << mode(numbers);
cout << "\nThe mean of the numbers is: " << mean(numbers);
cout << "\nThe median of the numbers is: " << median(numbers);
cout << "\nEnter a number from the list to find: ";
cin >> search_for_number;
search(numbers, search_for_number);
return 0;
}
few weeks ago with some code about creating self similar melodies in
music. The coding style I'm being taught is apparently a lot different
from what the pros around here use. I really need help with debugging
some program errors more than anything, even though my coding style
might not be perfect.
Anyway here is my code. About the only things that work right are the
max, min, and insertion sort functions. I can't figure out where I
went wrong. If you are wondering, yes, this is homework, but I've
gotten as far as I could by myself with this and won't be able to ask
my teahcer for help until after the assignment is due this week.
Hopefully I could still get some help here as I have written a lot of
code here and am not simply asking somebody to go out and write
everything for me.
Thanks all.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void bubble_sort(int []);
void print_array(int []);
const int ARRAY_SIZE = 11;
int numbers[ARRAY_SIZE];
int search_for_number;
int check[ARRAY_SIZE][ARRAY_SIZE];
void bubble_sort(int array[])
{
int swap = 0;
do
{
int tmp;
for(int j=0; j< ARRAY_SIZE-1; j++)
{
if(array[j] > array[j+1])
{
tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
swap = 1;
}
}
}
while( swap == 1 );
}
void insertion_sort(int array[])
{
int i, j;
for(i = 1; i < ARRAY_SIZE; i++)
{
int value = array;
for (j = i - 1; j >= 0 && array[j] > value; j--)
{
array[j + 1] = array[j];
}
array[j + 1] = value;
}
}
int min(int array[])
{
return (array[0]);
}
int max(int array[])
{
return (array[ARRAY_SIZE-1]);
}
int median(int array[])
{
int median = ARRAY_SIZE/2;
return (array[median]);
}
float mean(int array[])
{
int total = 0, i;
float mean;
for (i = 1; i < ARRAY_SIZE; i++)
{
total += array;
mean = total / ARRAY_SIZE;
return (mean);
}
}
int mode(int array[])
{
int what_number;
int count, mode = check[0][0], j, i, h;
for (j = 0; j < ARRAY_SIZE; j++)
{
what_number = numbers [j];
count = 0;
for (i = 0; i < ARRAY_SIZE; i++)
{
if (numbers = what_number)
{
count++;
check [j][1] = count;
}
}
}
for (h = 0; h < ARRAY_SIZE; h++)
{
if (check [h+1] [1] > check [h] [1])
mode = check [h+1] [0];
}
return (mode);
}
void search(int array[], int search_for_number)
{
int i, found_number = 0;
for (i = 0; i < ARRAY_SIZE; i++)
{
if (array == search_for_number)
cout << endl << search_for_number << " is at index " << array;
found_number = 1;
if (i == ARRAY_SIZE-1 && found_number == 0)
cout << endl << search_for_number << " is not in the list of
numbers.";
}
}
void print_array(int array[])
{
cout << setiosflags(ios::right);
for(int i=0; i < ARRAY_SIZE; i++)
cout << endl << array;
cout << endl;
cout << setiosflags(ios::left);
}
int main()
{
int Count = 0, search_for_number;
char sort_method;
sort_method = 'f';
cout << "Enter 11 integer numbers.\n\n";
do
{
cout << "Enter Number " << (Count + 1) << ":";
cin >> numbers[Count];
Count++;
}
while ( Count < 11 );
do
{
cout << "Sort with bubble sort (b) or insertion sort (i)?";
cin >> sort_method;
}
while ( sort_method != 'i' && sort_method!= 'b' );
cout << "The numbers sorted in ascending order are: ";
if (sort_method == 'i')
{
insertion_sort(numbers);
print_array(numbers);
}
if (sort_method == 'b')
{
bubble_sort(numbers);
print_array(numbers);
}
cout << "\nThe minimum of the numbers is: " << min(numbers);
cout << "\nThe maximum of the numbers is: " << max(numbers);
cout << "\nThe mode of the numbers is: " << mode(numbers);
cout << "\nThe mean of the numbers is: " << mean(numbers);
cout << "\nThe median of the numbers is: " << median(numbers);
cout << "\nEnter a number from the list to find: ";
cin >> search_for_number;
search(numbers, search_for_number);
return 0;
}