stuck real bad .......HELPPPPPPPP!!!!!!

S

sashi

guys this is my end of term assignment and i am stuck in a bad waymy
grade is gonna suck if i dont turn this assignment in please help me

the assignment is :

Write a program (or a series of programs) which divides an array of 4M
random
numbers into two parts, sorts each part separately, and combines them
to form a sorted
list.
You should implement a variety of communication schemes to divide the
work in
half, so that different processes (or threads) each sort half the
list. Time the execution for
each communication method to form a table and brief report showing the
communication
overhead for each method. Note that since the machines in EMCS 202
have only one
processor, the proposed splits will not actually improve performance.
Complete method 1 and any three others for full credit.
1: No splits; just a single process to sort the numbers.
2: Unnamed pipe
3: Sockets
4:Multi-threading
 
S

santosh

sashi said:
guys this is my end of term assignment and i am stuck in a bad waymy
grade is gonna suck if i dont turn this assignment in please help me

the assignment is :

Write a program (or a series of programs) which divides an array of 4M
random numbers into two parts, sorts each part separately, and
combines them to form a sorted list.

You should implement a variety of communication schemes to divide the
work in half, so that different processes (or threads) each sort half
the list. Time the execution for each communication method to form a
table and brief report showing the communication overhead for each
method. Note that since the machines in EMCS 202 have only one
processor, the proposed splits will not actually improve performance.

Complete method 1 and any three others for full credit.

1: No splits; just a single process to sort the numbers.
2: Unnamed pipe
3: Sockets
4: Multi-threading

Try either quicksort or mergesort for method one. For achieving the
other methods you need to ask in a group for your system since Standard
C has no means to do pipes, sockets and multi-threading by itself. For
Windows ask in <and for UNIX
in <
<http://www.google.com/search?hl=en&q=Quicksort>
<http://www.google.com/search?hl=en&q=Mergesort>

I suggest that you abandon the effort for full credit and instead look
into doing a good sort in a single process.
 
S

sashi

Try either quicksort or mergesort for method one. For achieving the
other methods you need to ask in a group for your system since Standard
C has no means to do pipes, sockets and multi-threading by itself. For
Windows ask in <and for UNIX
in <
<http://www.google.com/search?hl=en&q=Quicksort>
<http://www.google.com/search?hl=en&q=Mergesort>

I suggest that you abandon the effort for full credit and instead look
into doing a good sort in a single process.

thanks for the reply... i am stuck at the random number generation
part..
the rest is all fine...how do i generate 4 million rand nos and split
it is wat i have not been able to figure out
 
S

santosh

sashi said:
thanks for the reply... i am stuck at the random number generation
part..
the rest is all fine...how do i generate 4 million rand nos and split
it is wat i have not been able to figure out

#include <stdio.h>
#include <stdlib.h> /* for rand(), EXIT_FAILURE and malloc() */

#define NO_ELEMENTS 4000000UL

int main(void) {
int *rand_arr = malloc(NO_ELEMENTS * sizeof *rand_arr);
unsigned long ctr;

if (!rand_arr) {
puts("No memory");
return EXIT_FAILURE;
}

for (ctr = 0; ctr < NO_ELEMENTS; ctr++) {
rand_arr[ctr] = rand();
}

/* proceed to sorting */
/* ... */
return 0;
}

This should be successful on 32 bit systems. Under systems like MS DOS
it will fail.
 
S

santosh

santosh said:
thanks for the reply... i am stuck at the random number generation
part..
the rest is all fine...how do i generate 4 million rand nos and split
it is wat i have not been able to figure out

#include <stdio.h>
#include <stdlib.h> /* for rand(), EXIT_FAILURE and malloc() */

#define NO_ELEMENTS 4000000UL

int main(void) {
int *rand_arr = malloc(NO_ELEMENTS * sizeof *rand_arr);
unsigned long ctr;

if (!rand_arr) {
puts("No memory");
return EXIT_FAILURE;
}

for (ctr = 0; ctr < NO_ELEMENTS; ctr++) {
rand_arr[ctr] = rand();
}

/* proceed to sorting */
/* ... */
return 0;
}

This should be successful on 32 bit systems. Under systems like MS DOS
it will fail.

Forgot to mention to "split" this array, you have two choices. You can
either allocate two different arrays with two separate calls to
malloc(), totalling to 4 million elements, or you can simply set a
pointer halfway into one large array and "treat" them as conceptually
separate.

Please note that if you follow the second method you need to be aware
that any resizing of the array will affect the "entire" array, since as
far as the system is concerned it is one array and not two. A
deallocation request will also destroy the entire array.
 
T

Tomás Ó hÉilidhe

sashi:
guys this is my end of term assignment and i am stuck in a bad waymy
grade is gonna suck if i dont turn this assignment in please help me

the assignment is :

Write a program (or a series of programs) which divides an array of 4M
random
numbers into two parts, sorts each part separately, and combines them to
form a sorted
list.


Well if you want 4 million integers, then do:

#define AMOUNT ((size_t)4000000u)

int *const palloc = malloc(AMOUNT * sizeof(int));

(I suggest dynamic allocation because I've never heard of anyone putting
16 or so megabytes on the stack)

Then use a loop to give them random values:

int *p = palloc;
int const *const pend = palloc + AMOUNT;

do *p++ = rand();
while (pend != p);

I haven't done much sorting in my programming career, but I think the C
standard library provides sorting functions.
 
J

Jack Klein

sashi:



Well if you want 4 million integers, then do:

#define AMOUNT ((size_t)4000000u)

Including an unneeded and redundant cast in a macro is just silly.
Using an unsigned int conversion specifier on a value not guaranteed
to fit into an unsigned int produces an inplementation-defined result,
and a different value than intended, it UINT_MAX is less than
4,000,000. It is certainly poor coding and a portability issue
regardless of the range of unsigned int.

#define AMOUNT 4000000L /* or UL */

....makes sense.
int *const palloc = malloc(AMOUNT * sizeof(int));

Your (size_t) cast it totally unnecessary in the expansion of AMOUNT
in the line above. If you have failed to include <stdlib.h>, the call
to malloc() causes undefined behavior no matter what type of argument
you pass. If you have included <stdlib.h>, the conversion of the
argument to size_t is automatic.

Finally, as is frequently recommended on this group, using the result
pointer in the size expression is preferred, because if the type
allocated ever changes, the sizeof() expression will not need to be
changed.

int * const palloc = malloc(AMOUNT * sizeof *palloc);
(I suggest dynamic allocation because I've never heard of anyone putting
16 or so megabytes on the stack)

Then use a loop to give them random values:

int *p = palloc;
int const *const pend = palloc + AMOUNT;

do *p++ = rand();

The terminating semicolon means that the loop will be executed exactly
once...
while (pend != p);

....and this will generate a diagnostic from the compiler.
I haven't done much sorting in my programming career, but I think the C
standard library provides sorting functions.

Of course a single call to srand() would come in handy if one wants
different values on different executions of the program.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
C

CBFalconer

sashi said:
guys this is my end of term assignment and i am stuck in a bad waymy
grade is gonna suck if i dont turn this assignment in please help me

I hear loud sucking noises.
 
S

sashi

I hear loud sucking noises.

thanks a lot guys specially tomas santosh and jack.. for all the help
guys..
i used the srand and used time for getting rand nos...
but thanks a lot for ur immediate response guys....

finally my i can get that "A" i was hoping for.... :)
 
K

Kaz Kylheku

Note that since the machines in EMCS 202 have only one
processor, the proposed splits will not actually improve performance.

Where does that tuition money go? It used to be that you went to
school to get your hands on far better than consumer-grade hardware.
 
M

Mark McIntyre

Kaz said:
Where does that tuition money go? It used to be that you went to
school to get your hands on far better than consumer-grade hardware.

You're kidding right? At Oxford in the late eighties, I learned to
programme for teletype and Apple Lisa which were the two main computers
used in physics teaching labs.

When I became a postgrad I got to buy an Atari ST for my lab. Whoo, that
was exciting. I even got permission to buy an 8086 addin board so we
could run a few BASIC apps.

And later, as a postdoc in the mid nineties, I was allowed to play with
a Vax 11/780 and a Sparc 1. I think they both had around 64 MB of memory.

Meanwhile back at home I had a Pentium Pro based PC with a 196MB memory.

Admittedly the university cluster was four Vax 8800s (and I think a
couple of microvaxes) so there was some sensible computing around.
 
S

santosh

Mark said:
You're kidding right? At Oxford in the late eighties, I learned to
programme for teletype and Apple Lisa which were the two main
computers used in physics teaching labs.

When I became a postgrad I got to buy an Atari ST for my lab. Whoo,
that was exciting. I even got permission to buy an 8086 addin board so
we could run a few BASIC apps.

Indeed. You have to see the creaky, rusty old boxes in
supposedly "premier" institutes of learning to believe that the "tution
money" is falling down a "black hole".

Colleges that make money in hundreds of thousands every semester are
still beating boxes from the early 90s, loaded with "state of the art"
MS DOS and Windows 95.

:)

<snip>
 
B

Ben Bacarisse

Jack Klein said:
The terminating semicolon means that the loop will be executed exactly
once...

I can't see why. The syntax for 'do' is:

do statement while ( expression ) ;

The ; after 'rand()' terminates the statement and is followed by

which looks fine to me. I *prefer* to write:

do {
*p++ = rand();
} while (pend != p);

but that is for clarity, not correctness.
...and this will generate a diagnostic from the compiler.

Am I missing something?

Aside: I prefer 'while' in almost all cases. If this code is wrapped
in a function and AMOUNT made a parameter, it will eventually be
called with a zero argument.
 
D

David Thompson

On Sat, 01 Dec 2007 18:57:33 GMT, Tomás Ó hÉilidhe


Including an unneeded and redundant cast in a macro is just silly.
Using an unsigned int conversion specifier on a value not guaranteed
to fit into an unsigned int produces an inplementation-defined result,
and a different value than intended, it UINT_MAX is less than
4,000,000. It is certainly poor coding and a portability issue
regardless of the range of unsigned int.
If by 'conversion specifier' you mean the cast, see below. If you mean
the suffix 'u', no. Integer literals always have a type 'wide' enough
to represent them, but at least as wide as int. In C90 4000000 with no
suffix will be (signed) int if that is wide enough, or if not (signed)
long which always must be wide enough. For (other) values exceeding
LONG_MAX, unsigned long will be tried else error. For C99 the sequence
goes from (signed) long to (signed) long long, and then ends (no ull).

With the U suffix, the sequence goes unsigned int, unsigned long, and
in C99 only unsigned long long, then error. It never truncates.

The cast may truncate if size_t is too small to represent the value --
but in that case the implementation doesn't support an object
(allocated or otherwise) of the desired size anyway. And it will be at
SIZE_MAX or (size_t)-1, which is not necessarily UINT_MAX.
Your (size_t) cast it totally unnecessary in the expansion of AMOUNT
in the line above. If you have failed to include <stdlib.h>, the call
to malloc() causes undefined behavior no matter what type of argument
you pass. If you have included <stdlib.h>, the conversion of the
argument to size_t is automatic.
It's true the cast is unnecessary here. In the normal case where
SIZE_MAX > INT_MAX if the left operand is (signed) int it will be
promoted to size_t and the multiplication done in size_t, which is
safe if the resulting (desired) size is allocable at all.

In the rare case where size_t has lower rank than int and SIZE_MAX <=
INT_MAX, then the right operand will be promoted to int, the left
operand either with or without the cast ditto, and the multiplication
done in int, possibly unsafely i.e. could overflow and U.B., and then
truncated to size_t, possibly wrongly.
The terminating semicolon means that the loop will be executed exactly
once...


...and this will generate a diagnostic from the compiler.
Nope. Try it. (Well, not a standard-required diagnostic. It could say
"warning: pend is a confusing variable name". <G>)

<snip other points>

- formerly david.thompson1 || achar(64) || worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top