P: n/a

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_SIZE1; 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[i];
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_SIZE1]);
}
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[i];
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 [i] = 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[i] == search_for_number)
cout << endl << search_for_number << " is at index " << array[i];
found_number = 1;
if (i == ARRAY_SIZE1 && 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[i];
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;
}  
Share this Question
P: n/a

<in*****@yahoo.com> wrote: 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 swap = 0;
The way you have it, if swap gets set it can never get cleared in the do
loop
<snip>  
P: n/a

<in*****@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com... 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.
You need to specify what is going wrong with your code: whether you're
getting compiler errors, or runtime errors, or what behavior it is
exhibiting that is incorrect. Try running in a debugger and you should
easily see what the problems are.
Just because you're written some code does not relieve you of the burden of
finishing your work yourself. But I'll see if I can spot anything
obvious...
(By the way, it would help if you posted code that showed the indentation
properly. Using TABs in Outlook Express, for example, causes everything to
be leftaligned, making this very hard to read. Using spaces, or a
different tool, would help there.)
int median(int array[]) { int median = ARRAY_SIZE/2; return (array[median]); }
Look ok to me (assuming the array is sorted and ARRAY_SIZE is odd). Are you
getting the wrong answer? What's your data? What does it look like after
sorting? What result do you get? float mean(int array[]) { int total = 0, i; float mean; for (i = 1; i < ARRAY_SIZE; i++) { total += array[i];
mean = total / ARRAY_SIZE; return (mean);
You're still inside the loop here. Shouldn't you wait until you've finished
adding up the values before you try to compute and return the average?
} }
int mode(int array[]) { int what_number; int count, mode = check[0][0], j, i, h;
Where is check filled? You're assigning from check[0][0]here. Is that a
valid value? for (j = 0; j < ARRAY_SIZE; j++) { what_number = numbers [j]; count = 0; for (i = 0; i < ARRAY_SIZE; i++) { if (numbers [i] = what_number)
Do you mean ==?
{ count++; check [j][1] = count; } } }
for (h = 0; h < ARRAY_SIZE; h++) { if (check [h+1] [1] > check [h] [1])
Think: What will check[h+1][1] refer to when h == ARRAY_SIZE1?
mode = check [h+1] [0]; }
Any particular reason you're making use of check[][0] and check[][1], but
you've defined the second dimension of check as ARRAY_SIZE? Comments in
your code would help a great deal in understanding what you're trying to
accomplish.
As I said earlier, you need to tell us what you're seeing, and what you
expected to see. And your debugger will likely provide much better help
than waiting for us to try to guess what your problems are. Use it. It
should show you the problems right away. Also, go back to your notes on how
to do these tasks (i.e., algorithm descriptions or pseudocode), and then
check carefully that that's what you're actually doing in the code. return (mode);
}
Howard  
P: n/a
 in*****@yahoo.com wrote: I need help figuring out what is wrong with my code.
if (numbers [i] = what_number)
I am guessing you meant to use == here, rather than assignment.
Your insertion sort looks wrong, but I'm too dopey this morning to test
or prove it.
In this loop you have an error with the first if:
for (i = 0; i < ARRAY_SIZE; i++) { if (array[i] == search_for_number) cout << endl << search_for_number << " is at index " << array[i]; found_number = 1; if (i == ARRAY_SIZE1 && found_number == 0) cout << endl << search_for_number << " is not in the list of numbers."; }
found_number = 1 always happens, since you haven't used {}, just
indentation. Also, put the second if outside the loop and remove the i
== ARRAY_SIZE1 condition. (This is style and efficiency rather than
correctness.)
That's all I can pick up from a quick eyeballing of the code. GL.   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 3388
 replies: 3
 date asked: Nov 28 '05
