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