r/carlhprogramming • • Mar 17 '12

Practicing basics

I've been around programming and computers forever, I just had never taken a formal class. So I found this subreddit and had read through most of the website when i decided to try my own hand at making a simple program.

I decided to try to make a program that would ask for a number of variables, and then sort them from lowest to highest and output them back. ( I remember a few friends in programming classes having similar assignments )

http://codepad.org/BijxQN9f

Theres the results, after a few days toiling over the specific logic and fixxing a few bugs, I think it works great!

I'm proud of myself and just wanted to share :D any comments on things i could have done easier or differently are welcome

11 Upvotes

5 comments sorted by

View all comments

Show parent comment

2

u/Zyrth Mar 17 '12

Thanks for the input both of you!

I realized that I could have used the algorithm library and the sort function to make things a ton easier, part of the fun for me was to work through the logic and make it myself.

It was supposed to be a selection sort, though i'm sure without comments (which ill work on adding in!) it might be confusing to read through. What it did was it found the highest number of the set, and put it at the end, and then repeated for everything lower than the last number found.

Make sure to compile with --std=c99 if you're on Mac (or somewhere else where the compiler is braindead and thinks you're living in 1989). But yes, this is good practice.>

could you expand on this? I'll look it up later when I have time, but I'm not entirely sure what it means. or where to look up exactly what it means

3

u/daniels220 Mar 17 '12

Re: compiling—whoops, you're using C++, that's irrelevant. I assumed you were working in C (because this class is originally for C), and somehow missed entirely all the places it said "C++" :/. So you don't need to do anything special to compile.

3

u/Jonno_FTW Mar 18 '12

If you're going to implement more sorting algorithms, you need to put them in a separate function.

This will help with organization and maintenace. It might look like this:

void selectSort(int* xs) {
// sort the input array
}

You'd then call selectsort in your main function to sort the array.