473,405 Members | 2,171 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,405 software developers and data experts.

Just a question

sj
Hi;
I am trying to find the position of the maximum element of and array using
divide-and-conquer method. Following is the code I am trying.

#include <stdio.h>
#define MAX 7

int msort(char list[], int n)
{
int i, half1, half2;
char arr1[MAX/2+1];
char arr2[MAX/2+1];
if(n ==1) return 0;
else if (n==2){
if(list[0]>list[1]) return 0;
else return 1;
}else

{
half1 = n / 2;
half2 = n - half1;

for(i = 0; i < half1; i++)
arr1[i] = list[i];

for(i = 0; i < half2; i++)
arr2[i] = list[half1 + i];

int x1 = msort(arr1, half1);

int x2 = msort(arr2, half2);

if( list[x1] > list[x2]) return x1;
else
return x2;
}
}

int main()
{
int i, n;
char array[MAX];
n = 7;

array[0] = 'A';
array[1] = 'B';
array[2] = 'C';
array[3] = 'D';
array[4] = 'E';
array[5] = 'F';
array[6] = 'X';

int x = msort(array, n);

printf("%d ", x );

printf("\n");
return 0;
}

what am i doing wrong here?
thanks for any help

sj
Nov 14 '05 #1
3 1495


sj wrote:
Hi;
I am trying to find the position of the maximum element of and array using
divide-and-conquer method. Following is the code I am trying.

#include <stdio.h>
#define MAX 7

int msort(char list[], int n) I would rather call this function index_of_max or something
like that and use size instead of n. {
int i, half1, half2;
char arr1[MAX/2+1];
char arr2[MAX/2+1]; (*) C90 style declarations if(n ==1) return 0;
else if (n==2){
if(list[0]>list[1]) return 0;
else return 1;
}else

{
half1 = n / 2;
half2 = n - half1;

for(i = 0; i < half1; i++)
arr1[i] = list[i];

for(i = 0; i < half2; i++)
arr2[i] = list[half1 + i];

int x1 = msort(arr1, half1); C99 style declarations (not at the beginning of the block);
if you really use C99, then make (*) rather
char arr1[n/2+1], arr2[n/2+1];
to save memory. See below for a better solution.
int x2 = msort(arr2, half2); This is the relative index with respect to arr2. The
index w.r.t list would be x2+half1.
if( list[x1] > list[x2]) return x1; (**)
Here you compare the wrong list-element. Either set
x2 = msort(arr2, half2) + half1;
above or compare list[x1] with list[x2+half1] here. else
return x2;
}
}

int main() Rather use
int main (void) {
int i, n;
char array[MAX];
n = 7; This would be n=MAX;
array[0] = 'A';
array[1] = 'B';
array[2] = 'C';
array[3] = 'D';
array[4] = 'E';
array[5] = 'F';
array[6] = 'X';

int x = msort(array, n);

printf("%d ", x ); For better control, I'd output x and array[x].
printf("\n");
return 0;
}

what am i doing wrong here?
thanks for any help


It is not necessary to copy all the stuff from list to arr1 and
arr2. If you insist, rather use memcpy().
I would propose that you pass on &list[0] and &list[half1] directly
because list is not modified in msort().
In order to document this, use the const qualifier for the declaration
of list.

However, only the changes suggested in (**) are necessary, to make
your program run.
As I had some time on my fingers, I have rewritten parts of your
program and did some things deliberately different; maybe this is
useful for you.
Note: I use size_t instead of int because then I am sure that
the range of the indices fits every char array which can be stored
in memory.

Cheers
Michael

--------

#include <stdio.h>
#define MAX 7

size_t index_of_max (const char *list, size_t listsize)
{
size_t half, index1, index2;

switch (listsize) {
case 0:
fprintf(stderr, "%s: invalid size of list (0)\n",
__func__);
return 0;
case 1:
return 0;
case 2:
index1 = 0; index2 = 1;
break;
default:
half = listsize / 2;
index1 = index_of_max(list, half);
index2 = half + index_of_max(&list[half], listsize-half);
}

if (list[index1] > list[index2])
return index1;
else
return index2;
}

int main (void)
{
size_t n, index;
char array[MAX];

n = MAX;

array[0] = 'A';
array[1] = 'B';
array[2] = 'C';
array[3] = 'D';
array[4] = 'E';
array[5] = 'F';
array[6] = 'X';

index = index_of_max(array, n);

printf("index of maximal element: %zu (--> %c)\n",
index, array[index]);

return 0;
}

--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #2
On 6 Nov 2004 07:39:24 -0800, kp******@yahoo.com (sj) wrote:
Hi;
I am trying to find the position of the maximum element of and array using
divide-and-conquer method. Following is the code I am trying.

#include <stdio.h>
#define MAX 7

int msort(char list[], int n)
{
int i, half1, half2;
char arr1[MAX/2+1];
char arr2[MAX/2+1];
if(n ==1) return 0;
else if (n==2){
if(list[0]>list[1]) return 0;
else return 1;
}else

{
half1 = n / 2;
half2 = n - half1;

for(i = 0; i < half1; i++)
arr1[i] = list[i];

for(i = 0; i < half2; i++)
arr2[i] = list[half1 + i];

int x1 = msort(arr1, half1);

int x2 = msort(arr2, half2);

if( list[x1] > list[x2]) return x1;
else
return x2;
You copied the array, so arr2[x2] is actually
list[half2+x2]. Therefore this return should
be:

return half2+x2;
}
}


Nick.

Nov 14 '05 #3
sj wrote:
Hi;
I am trying to find the position of the maximum element of and array using
divide-and-conquer method. .... what am i doing wrong here?


This is an odd algorithm to use unless:
a) It's homework (okay to ask since you have shown us some effort)
b) You are practising your skills or some similar thing (good)
c) You are going to adapt it to run in several threads (i.e. you are
splitting the task into unordered subtasks so that it will run faster
(probably O(log(n)))
I you have a good reason to use this algorithm (like those) then ignore
the rest is this.

* If the array is sorted (I assume it isn't here), then the maximum is
at one end.
* If the array is unsorted, then the best algorithm is simply a linear
search (unless condition "c)" above applies). This is because the
maximum could be any of the elements, so you have to compare all of them
to something, and that is an O(n) algorithm.
* Just about anything else will be slower, unless the array is known to
have some strange pattern without being sorted. If the maximum element
is likely to be in a certain place, then start there.

For algorithm discussion, check out comp.programming (follow-ups set).

--
Simon Richard Clarkstone
s.************@durham.ac.uk / s***************@hotmail.com
Nov 14 '05 #4

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

Similar topics

10
by: Conax | last post by:
Hi there, My boss is hoping that I can come up with a page that displays some information. The information will always be displayed on specific part of the page, with auto refresh. But he...
8
by: Sims | last post by:
Hi, I have some small questions that have never been any problems, (for my compiler?), but always make me curious. So here goes... what does the standard sday about the 'if' statement? for...
0
by: chan | last post by:
how to apply online update function into program (the effect just like Norton system work live update) The situation is below: I want to develop a program that contains some product...
13
by: Lumpierbritches | last post by:
I'm curious as to why some questions posted here get results and solutions, while others are answered in a seemingly foreign language and I can't begin to comprehend or understand the answers that...
25
by: Tim | last post by:
Dear Developers, Firstly, I'm not sure where this post should go so I apologise if this is in the wrong group or area. I'm currently interviewing for a vb.net developer who doesn't mind...
4
by: Richard | last post by:
In normal asp i used --------- objRS.Open "tbl_Nieuws", objConn, 1, 3 objRS.AddNew objRS.Fields("N_Datum") = FormatDateTime(Now(),2) objRS.Fields("N_Title") =...
12
by: wickwire | last post by:
I have created a class and used it to further overload ostream: class drum { ... friend ostream& operator<< ( ostream&, drum const& ); } ostream& operator<< ( ostream& out, drum const& od )...
11
by: sterling.mckay | last post by:
Just started to teach myself C C++ programming... I have been very interested with computers for a while now and have a nac or so I thought for how they work ... hardware I am ok with ... I can...
20
by: kwikius | last post by:
As I understand it posts to comp.std.c++ shouldnt contain personal attacks. Since several of my posts on this to comp.std.c++ on this subject have now been simply ignored with out any reply by...
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.