473,386 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Help with array medians, modes, and searching

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[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_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[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_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[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;
}

Nov 28 '05 #1
3 3711
<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>
Nov 28 '05 #2

<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 run-time 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 left-aligned, 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_SIZE-1?
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 pseudo-code), and then
check carefully that that's what you're actually doing in the code.

return (mode);

}


-Howard


Nov 28 '05 #3

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_SIZE-1 && 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_SIZE-1 condition. (This is style and efficiency rather than
correctness.)
That's all I can pick up from a quick eyeballing of the code. GL.

Nov 29 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
5
by: Andrew Poulos | last post by:
If I'm searching for an occurance of a value in a multi-dimensional array how can I get it's index returned as an array, if found? For example, if: foo = new Array(); foo = , 5, , 9, 10]; ...
24
by: Don | last post by:
Hi I have an array of unsigned chars, like: MyArray = {0x00}; For memory-mapping purposes I need to store this array at a specific address (0xFFFF1199) How do I declare this? I cant't do...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
36
by: Eric Laberge | last post by:
Hi! I'm working on automatically generated code, and need to assign arrays. memcpy is an obvious solution, but it becomes complicated to use in the context I'm working on, ie.: I could use it...
8
by: Don | last post by:
I have a third party C++ DLL that I am trying to use from C#. The specific function I am trying to use is declared in C++ as follows: ladybugConvertToMultipleBGRU32( LadybugContext ...
10
by: datta.abhirup | last post by:
Hi How can I find out the modal value in an array. That is the value which occurs maximum time in the sequence .. e.g. if my array has values like definitely the maximum time 2 occurs in...
3
by: Rebeus87 | last post by:
Hi i am new to Perl and i was wondering if you could look at my scripts and give me advice and feedback on how i could improve my scripts. I am using /etc/fb.modes to write Perl scripts. ...
3
by: jac130 | last post by:
the program runs, and user is prompted via inputbox to enter an integer-this is the size of the array, then the user fills the array with that many values...but as the user enters the values, i need...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.