d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Make Me A Better Programmer - From Step 1
Prev167891056Next
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 24 2012 12:19am
we just started arrays today and covered the difference between values/references. Still haven't learned about pointers yet.

I got a new assignment, will work on it tomorrow and post it up here.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 25 2012 01:28am
Code
#include <iostream>
#include <iomanip>

using namespace std;

void selection(double x[], int len);
void swap(double& x,double& y);
int findMax(double x[],int len);
int getArray(double x[],int max);
void printResults(double x[], int len);
int numPos(double x[], int len);
int numTween(double x[], int len, double a, double b);
bool inOrder(double x[], int len);

const int N = 100;

int main()
{
   double x[N];
   double a = -5.5, b = 5.0;
   int len = getArray(x,N);
   if (len > 0)
   {
       selection(x,len);
       printResults(x,len);
       cout << endl << "Number of positive elements: " << numPos(x,len) <<endl;
       cout << endl << "There are "<<numTween(x,len,a,b)<<" elements between "
       <<a<<" & "<<b;
   }
   return 0;
}

//print the first len doubles of a

void printResults(double a[], int len)
{
   cout<<"Forwards: ";
   for (int i = 0; i < len; i++) {
       if (i % 5 == 0) cout <<endl;
       cout <<"   a["<<setw(2)<<i<<"]: "<<setw(3)<<a[i];
   }
   cout<<endl;
}

//fill x up to max elements (from kbd)

int getArray(double x[],int max)
{
   cout <<"Please type some doubles, end with control-d\n";
   int i = 0; //i points to first array position
   while (cin >> x[i]){
       i++;  //count it, point to next avail position
       if (i == max) break;
   }
   return i;
}

//return position of the biggest element in array x

int findMax(double x[],int len)
{
   int current = 0;
   for (int i = 1; i < len; i++)
   {
       if (x[i] > x[current])
       {
           current = i;
       }
   }
   return current;
}

void swap(double& x, double& y)
{
   double temp = x;
   x = y;
   y = temp;
}

int numPos(double x[], int len)
{
   int p = 0;
   for (int i = 0; i < len; i++)
   {
       if (x[i] > 0)
       {
           p++;
       }
   }
   return p;
}

int numTween(double x[], int len, double a, double b)
{
   int t = 0;
   for (int i = 0; i < len; i++)
   {
       if (x[i] > a && x[i] < b)
       {
           t++;
       }
   }
   return t;
}

void selection(double x[], int len)
{
   for (int i = len - 1; i > 0; i--)
   {
       int j = findMax(x,i);
       swap(x[j],x[i]);
       if (inOrder(x, len))
       {
           break;
       }
   }
}

bool inOrder(double x[], int len)
{
   int current = 0;
   for (int i = 1; i < len; i++)
   {
       if (x[i] < x[current])
       {
           return false;
       }
   current = i;
   }
   return true;
}


Need some help with this one. The function (void selection) is having some troubles.

selection is supposed to sort the array.

It seems to work OKAY with positive numbers, but for some reason if I type:

-1
-2
-3
-4
-5

(in that order)

(use ctrl + d to exit the loop)

I get this:

Code
Forwards:
  a[ 0]:  -3   a[ 1]:  -5   a[ 2]:  -4   a[ 3]:  -2   a[ 4]:  -1


any help you guys can provide? Still c++ as always.

I tried running through the program myself on paper, and once 'i' decrements to '2' in selection(), the array is already in order, but still continues on anyway. I think that might be the issue. Not sure though.

Should I add a statement like

Code
if (x[4] > x[3] && x[3] > [2] && x[2] > x[1] && x[1] > x[0])
{
   break;
}


at the end of the for loops block?

??

edit: derp. Obviously only works for 'n' sized arrays ~_~

Hrm....maybe I should make a function to check to see if the array is in order?

And then do something like

Code
if (inOrder(x,len)
{
   break;
}


and inOrder could be something like

Code
bool inOrder(double x[], int len)
{
   int current = 0;
   for (int i = 1; i < len, i++)
   {
       if (x[i] < x[current]
       {
           return false;
       }
   current = i;
   }
   return true;
}



edit: it worked! fixed ;D

just critique in general now pls.

This post was edited by Eep on Jul 25 2012 01:49am
Member
Posts: 279
Joined: Apr 11 2010
Gold: 1,486.00
Jul 25 2012 03:21am
I have never been very familiar with C++, so this is mostly based on my knowledge of other languages and trying to translate, but...

It mostly looks okay, I think. The main problem that I have is that it's hard to just read your code and know what you're trying to do.

Something that takes some getting used to is realizing that there is more to be done even after the code "works". You can (almost) always improve something; the names of your functions/variables, the structure and size of your functions, etc. Most code is first written to work, and then either left at that or, for lack of a better word, beautified. Beautifying your code will not only make it easier for other people to read, in the real world it leads to code that is easier to maintain over the long term.

Obviously that's sort of a long-term thing, and not something you worry about when you're just learning a language. But it's something to keep in mind. Being a perfectionist is not a bad thing.

This post was edited by PumblesMumbles on Jul 25 2012 03:27am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 26 2012 02:39am
Okay guys I got conceited. My program sucks and so do I.

I am having a problem.


If I type something like, 1, 1, 2


My program sorts it as:

1, 2 , 1 (obviously not in order)

I believe the problem lies within the relationship between the selection function and the findmax function.

Can someone give me an idea of what the problem is?
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 28 2012 01:23am
Can't figure this out for the life of me :/

I know the selection function is garbage as it is because it has no checks...but everytime I add checks to it, I get something like


-314.0e2032323 as my x[0] position.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 28 2012 02:00am
well I got it working...but I am still mind fucked

basically I ditched the inOrder function because it is for bubble sorting afaik

All I did was add a simple 'if' to the SELECTION function and it works fine now?

I am so confused. I have NO IDEA when/how the program EVER checks to see what the element position of x[len] is.

I have NO IDEA how/when the program changes the position of the max element TO x[len]


Like I tried sitting down and writing each step down

but everytime I do it in my head it appears as though whatever is in the last element box never gets touched

so confused.


edit:

nvm

fuck

I figured it out.

I am so dumb.

This post was edited by Eep on Jul 28 2012 02:08am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jul 28 2012 02:03am
just write out every comparison/index/etc in a chart and trace it line by line if you already have the code.

if you don't have the code, then perform the algorithm by hand and build the same chart, then write code based on that chart
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 28 2012 02:11am
Quote (carteblanche @ Jul 28 2012 03:03am)
just write out every comparison/index/etc in a chart and trace it line by line if you already have the code.

if you don't have the code, then perform the algorithm by hand and build the same chart, then write code based on that chart


Yeah I did that. Initially I knew there was a problem with one function (after having wrote down what the code does by hand) and fixed it. I solved the problem. (added an 'if' to selection)

Then I couldn't figure out what made the program work. Thought the answer lied in the SELECTION function, then went back to findMax() and found that the answer was actually there.

I assumed, for some reason, that if I passed (length of the array minus 1) to findMax(), it would only check the first (0 thru length-1) positions. That was not true.

It just keeps RUNNING (length of array minus 1) times.

So yeah. Figured it all out now. I can sleep better tonight.

But man our Prof REALLY skimmed out on his lectures over Arrays and Pointers. I still barely feel comfortable with arrays, and other then the fact that arrays and pointers are so similar, I don't remember much else.

The biggest thing I learned is that for loops work really nicely with arrays.


Edit: my only problem now is, given an assignment to figure out selection sorting on my own, I don't know if I would have been able to figure out the first part

namely,

Code
void selection(double x[], int len)
{
   for (int i = len - 1; i > 0; i--)
   {
       int j = findMax(x,i);


knowing myself I would have just done the usual i = 0, i < len, i++

This post was edited by Eep on Jul 28 2012 02:15am
Member
Posts: 125
Joined: Jun 19 2012
Gold: 0.00
Jul 28 2012 08:15am
Ohhh man, data structures. They make you do the hard parts, but iRL you get to use the methods inside libraries for sorting and others (like Collections.sort(array)) in Java.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jul 28 2012 11:21am
Learn how to do unit testing or test driven development, then you can ensure each function works correctly by itself.
Go Back To Programming & Development Topic List
Prev167891056Next
Add Reply New Topic New Poll