d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Anyone Here Really Good With C (funct And Array) > Not C ++
Prev12
Add Reply New Topic New Poll
Member
Posts: 8,858
Joined: Jun 17 2008
Gold: 17,150.50
Apr 7 2014 11:55pm
This is what I have so far, but i'm doing something wrong
#include <stdio.h>


int main (void )
{


int arr[];
int solution[5];
unsigned int n;

solution[5] = selection_sort(arr, n);

printf("%d",solution[5]);

return(0);
}
void selection_sort(int* arr, unsigned int n) {
if (n){
unsigned int max = 0;

unsigned int i;

for(i=0; i<n; i++)

max = arr[i] > arr[max] ? i : max;

int temp = arr[max];
arr[max] = arr[n-1];
arr[n-1] = temp;
selection_sort(arr, n-1);

}
}
Member
Posts: 582
Joined: Mar 22 2014
Gold: 0.00
Apr 8 2014 12:16am
Quote (OnceUponADecember @ Apr 8 2014 01:55am)
This is what I have so far, but i'm doing something wrong
Code
#include <stdio.h>


int main (void )
{


int arr[];
int solution[5];
unsigned int n;

solution[5] = selection_sort(arr, n);

printf("%d",solution[5]);

return(0);
}
void selection_sort(int* arr, unsigned int n) {
if (n){
        unsigned int max = 0;

        unsigned int i;

        for(i=0; i<n; i++)

            max = arr[i] > arr[max] ? i : max;

        int temp = arr[max];
        arr[max] = arr[n-1];
        arr[n-1] = temp;
        selection_sort(arr, n-1);

    }
}
    -The sorting is done in-place, meaning the sorted result will be in the original array itself. Therefore, solution is not needed.

    -Even if the sorted result was returned in an array, you would not assign it like that, nor would you return it like that. "Arrays" are returned from functions as pointers, and you must assign them to a pointer type. Attempting to assign a pointer to an array variable gives a compiler error.

    -The return type of selection_sort is void, so it will not return anything. Attempting to assign it to solution does nothing (though if solution were a pointer variable instead, I believe you would just get a null pointer assigned).

    -The arr array has nothing in it. You are attempting to sort nothing (or whatever happened to be left on the stack at that point). Similarly, the n variable is uninitialized, therefore the selection_sort function will not know how many variables are in the array and how many should be sorted. Most likely, your OS will give you a clean stack with 0s, so most likely the selection_sort function will just exit out immediately without doing anything.

    -You don't print arrays using the %d format symbol. You have to iterate through the array and print out each element. %d is a signed int format symbol, which is the type of each array element.

    -Your return is incorrect. The keyword "return" is not a function, so you don't put parentheses around what you're returning like that. However, what you did happens to still work, because the compiler will assume that the (0) is a mathematical expression and evaluate it to 0.
I am not going to help you fix this. You should be learning on your own. What you wrote is indicative that you not only do not know the basics of C, but also programming in general.

This post was edited by poodaH on Apr 8 2014 12:22am
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 8 2014 01:41pm
Quote (poodaH @ Apr 8 2014 01:16am)
I am not going to help you fix this. You should be learning on your own. What you wrote is indicative that you not only do not know the basics of C, but also programming in general.


You had no problems depriving him of a learning experience when you blatantly posted solution code before on the hopes of receiving forum gold, why stop now?
Member
Posts: 582
Joined: Mar 22 2014
Gold: 0.00
Apr 8 2014 03:01pm
Quote (Minkomonster @ Apr 8 2014 03:41pm)
You had no problems depriving him of a learning experience when you blatantly posted solution code before on the hopes of receiving forum gold, why stop now?


Good point; I am at fault there for giving him part of the solution in my greed for FG.
I guess I realized with his last response that I felt disgusted doing this for someone so incompetent.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 8 2014 05:46pm
Quote (poodaH @ Apr 8 2014 04:01pm)
Good point; I am at fault there for giving him part of the solution in my greed for FG.
I guess I realized with his last response that I felt disgusted doing this for someone so incompetent.


Quote
What you wrote is indicative that you not only do not know the basics of C, but also programming in general.


I'd guess the majority of people who ask questions here don't know much programming in general. when people say "i need help with C", it's kinda like the people who ask for calculus help because they can't figure out how to factor their 2nd degree polynomial. it's for their calculus class, so it must be a calculus problem!

This post was edited by carteblanche on Apr 8 2014 05:54pm
Member
Posts: 8,858
Joined: Jun 17 2008
Gold: 17,150.50
Apr 8 2014 06:05pm
Quote (poodaH @ Apr 8 2014 02:01pm)
Good point; I am at fault there for giving him part of the solution in my greed for FG.
I guess I realized with his last response that I felt disgusted doing this for someone so incompetent.


Just so you know, this is my first course on embedded systems, also first course ever with any sort of coding. Of course I don't know what I'm doing. 80% of the class didnt even finish the assignment for todays class, and the teacher said it was expected. Next time you shouldn't be helping others especially with your attitude. I asked for help, it's not like I wanted the code written for me. Did you not get the PMs where I ask specific questions on what does what. I don't look for just the solution.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Apr 8 2014 06:30pm
Quote (OnceUponADecember @ Apr 8 2014 07:05pm)
Just so you know, this is my first course on embedded systems, also first course ever with any sort of coding. Of course I don't know what I'm doing. 80% of the class didnt even finish the assignment for todays class, and the teacher said it was expected. Next time you shouldn't be helping others especially with your attitude. I asked for help, it's not like I wanted the code written for me. Did you not get the PMs where I ask specific questions on what does what. I don't look for just the solution.


A handy trick I learned early on: Save FG by using google. Especially now, more and more people are taking a stab at CS and the various degrees surrounding it. There is a plethora of guides/help etc on the net. Just know how to phrase your queries.

You will thank me later.

(If your issue is something google can't help with, ask the professor. That is what they are there for. If they refuse to help, get money back from dean).
Member
Posts: 8,858
Joined: Jun 17 2008
Gold: 17,150.50
Apr 8 2014 08:07pm
Quote (Eep @ Apr 8 2014 05:30pm)
A handy trick I learned early on: Save FG by using google. Especially now, more and more people are taking a stab at CS and the various degrees surrounding it. There is a plethora of guides/help etc on the net. Just know how to phrase your queries.

You will thank me later.

(If your issue is something google can't help with, ask the professor. That is what they are there for. If they refuse to help, get money back from dean).


yeah, I ask questions all the time, stop by office hours an such, but sometimes when she explains it, its hard to figure it out by just listening. I'm more of a visual/hands on learner.
Member
Posts: 582
Joined: Mar 22 2014
Gold: 0.00
Apr 9 2014 02:11am
Quote (OnceUponADecember @ Apr 8 2014 08:05pm)
Just so you know, this is my first course on embedded systems, also first course ever with any sort of coding. Of course I don't know what I'm doing. 80% of the class didnt even finish the assignment for todays class, and the teacher said it was expected. Next time you shouldn't be helping others especially with your attitude. I asked for help, it's not like I wanted the code written for me. Did you not get the PMs where I ask specific questions on what does what. I don't look for just the solution.


Quote (OnceUponADecember @ Apr 8 2014 01:54am)
Quote (poodaH @ Apr 7 2014 09:51pm)
Quote (OnceUponADecember @ Apr 8 2014 12:48am)
you have an if (n) written in the code. what does that do? when i try compiling your code, it tells me undefined reference to WinMain@16


if (n) is equivalent to if (n != 0)

and all I gave you is the function to sort, I didn't give you a main function that your program requires to execute.


Another question. Howcome you're using the int* i believe its the pointer. I'm trying to write the main fuction also, but it doesn't seem to be working out.
I posted the code in my topic


These are some very specific questions on what does what for sure.
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll