d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Anyone Here Really Good With C (funct And Array) > Not C ++
12Next
Add Reply New Topic New Poll
Member
Posts: 8,858
Joined: Jun 17 2008
Gold: 17,150.50
Apr 7 2014 07:31pm
If you're really good with c and may be interested in helping me with someone assignments on Functions and Arrays.
I'll be willing to give some fg for help.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 7 2014 07:35pm
i suck at C, but i might still be able to help if you post (not pm) your problem.
Member
Posts: 8,858
Joined: Jun 17 2008
Gold: 17,150.50
Apr 7 2014 07:43pm
this is probably one of the easier ones on my to do list, lol.

Write a program that stores random numbers in an array, then sorts the integers by calling the selection_sort function that you will write.
Selection sort is passed an array with n elements, and the size n.
a. Search the array to find the largest element, then move it to the last position in the array by swapping places with the last element in the array.
b. Call itself recursively to sort the first n-1 elements of the array.
Your main function should test your selection sort with three different array sizes: 100, 1000, and 10000. Time the selection sort and display the execution times.
I suggest you test your program with a small size of 10 to get it working.
Display the unsorted data and the sorted data. Write a function called display_array_data to perform this work. Data should be right justified and field width should be used to display data neatly in a columnar format. You decide how many data elements to display per line. Each line should have a line number.
Display data in the following manner:
Unsorted Data
Line 001: 98 56 79 1 27
Line 002: 1007 1091 6 1000 567
Timing
One way to time code execution requires the time.h library.
// a struct is a user-defined data type that we have not yet studied
// declare two structs as shown below
struct timeval begin, current;
gettimeofday(&begin, 0);
// call your selection sort function
gettimeofday(&current, (struct timezone*) 0);
/* calculate difference */
double elapsed = (current.tv_sec - begin.tv_sec) + ((current.tv_usec
- begin.tv_usec) / 1000000.0F);

This post was edited by OnceUponADecember on Apr 7 2014 07:43pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 7 2014 07:46pm
what part of that do you need help with? if you dont understand selectionsort:
Member
Posts: 582
Joined: Mar 22 2014
Gold: 0.00
Apr 7 2014 08:00pm
Code
void selection_sort(int* arr, unsigned int n) {
if (n) {
unsigned int max = 0;
// Could iterate backwards from n but some architectures do worse iterating backwards
// Either way an optimizer will pick up these sorts of simple loops and optimize them
for(unsigned int i=1; i<n; i++)
max = arr[i] > arr[max] ? i : max;
// Could do xor swap, but this reads cleaner and any optimizer will pick it up and turn it into xor swap anyway
int temp = arr[max];
arr[max] = arr[n-1];
arr[n-1] = temp;
selection_sort(arr, n-1);
}
}


There's your function
feel free to dn8

This post was edited by poodaH on Apr 7 2014 08:00pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 7 2014 08:34pm
Quote (poodaH @ Apr 7 2014 09:00pm)
Code
void selection_sort(int* arr, unsigned int n) {
    if (n) {
        unsigned int max = 0;
        // Could iterate backwards from n but some architectures do worse iterating backwards
        // Either way an optimizer will pick up these sorts of simple loops and optimize them
        for(unsigned int i=1; i<n; i++)
            max = arr[i] > arr[max] ? i : max;
        // Could do xor swap, but this reads cleaner and any optimizer will pick it up and turn it into xor swap anyway
        int temp = arr[max];
        arr[max] = arr[n-1];
        arr[n-1] = temp;
        selection_sort(arr, n-1);
    }
}


There's your function
feel free to dn8



Maybe he wanted it in ANSI C.
Member
Posts: 582
Joined: Mar 22 2014
Gold: 0.00
Apr 7 2014 08:38pm
Quote (Minkomonster @ Apr 7 2014 10:34pm)
Maybe he wanted it in ANSI C.


fuck ansi c
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 7 2014 08:51pm
Quote (poodaH @ Apr 7 2014 09:38pm)
fuck ansi c


Well you just lost out on my dn8tion
Member
Posts: 582
Joined: Mar 22 2014
Gold: 0.00
Apr 7 2014 08:55pm
Quote (Minkomonster @ Apr 7 2014 10:51pm)
Well you just lost out on my dn8tion


What, I'll just take the i out of the loop or use n to reverse iterate, or increment the array pointer.
It's just a convenience, and the compiler will tell you what you did wrong anyway.
Code
void selection_sort(int* arr, unsigned int n) {
if (n) {
unsigned int i = 1, max = 0;
int temp;
// Could iterate backwards from n but some architectures do worse iterating backwards
// Either way an optimizer will pick up these sorts of simple loops and optimize them
for(; i<n; i++)
max = arr[i] > arr[max] ? i : max;
// Could do xor swap, but this reads cleaner and any optimizer will pick it up and turn it into xor swap anyway
temp = arr[max];
arr[max] = arr[n-1];
arr[n-1] = temp;
selection_sort(arr, n-1);
}
}


This post was edited by poodaH on Apr 7 2014 08:57pm
Member
Posts: 8,858
Joined: Jun 17 2008
Gold: 17,150.50
Apr 7 2014 09:23pm
ill test it all in a bit.
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll