RUSES
Would you like to react to this message? Create an account in a few clicks or log in to continue.
May 2024
MonTueWedThuFriSatSun
  12345
6789101112
13141516171819
20212223242526
2728293031  

Calendar Calendar


[C++] algorithm help

4 posters

Go down

[C++] algorithm help Empty [C++] algorithm help

Post  bgthor Fri Feb 24, 2012 5:20 am

right chaps and chapettes I am in a wee bit of a pickle

dave gave some huge help to sort out my algorithm code but I still seem to be hitting walls. to check wether my code can in fact accept the various elements like nill and infinity I simply plug them into the float array.
well sadly it doesn't seem to be recognising them at all. any help will be greatly appreciated.!

#include<iostream>
#include<time.h>
using namespace std;
const int size = 100;

void GenData(float* List);
double BubbleSort(float* List);
void PrintData(float* Lift);

int main()
{
const int Num = 1;
double ExecTimes[Num];
float AvgTime=0;
float x[size];
float z=0;

x[0]= -1/z;
x[1]= 1/z;
x[2]= -0.0;
x[3]= 0.0;

for(int i=0;i<Num;i++)
{
GenData(x);
ExecTimes[i]=BubbleSort(x);
PrintData(x);
cout<<"Run "<<i+1<<" "<<ExecTimes[i]<<endl;
AvgTime += ExecTimes[i];
}
AvgTime /= Num;
cout<<"Average "<<AvgTime<<endl;

}

void PrintData(float* List)
{
for(int i=0;i<size;i++)
{cout<<List[i]<<", ";}
cout<<endl;
}

void GenData(float* List)
{
srand(time(NULL));
for (int i=0; i<size; i++)
{
List[i]=(float)rand()/(float) RAND_MAX;
}
}

double BubbleSort(float* List)
{
bool Sorted;
float t;
int ExecTime;
time_t startTime,endTime;
time(&startTime);
do
{
Sorted=true;
for(int i=0; i<size-1;i++)
{
if(List[i] > List[i+1])
{
t=List[i];
List[i]=List[i+1];
List[i+1]=t;
Sorted=false;
}
}
}while(!Sorted);
time(&endTime);
ExecTime = difftime(endTime,startTime);
return ExecTime;
}

bgthor

Posts : 7
Join date : 2012-02-24

Back to top Go down

[C++] algorithm help Empty Re: [C++] algorithm help

Post  Alex Fri Feb 24, 2012 5:24 am

int zero = 0;
int one = 1;
float INFINITY = one/ (one - 1.0);
float MINUS_INFINITY = -INFINITY;
float NAN = (one - 1.0) / (one - 1.0);
float NEG_ONE = -1.0;
float MINUS_ZERO = NEG_ONE*zero;

That's what I used, obviously you'll need to rename things, but it works Smile
Alex
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

[C++] algorithm help Empty Re: [C++] algorithm help

Post  bgthor Fri Feb 24, 2012 5:29 am

can I just plug those straight into the array or would I have to use a refferal pointer?

bgthor

Posts : 7
Join date : 2012-02-24

Back to top Go down

[C++] algorithm help Empty Re: [C++] algorithm help

Post  Alex Fri Feb 24, 2012 5:29 am

Plug them in
i.e.
x[0] = INFINITY etc
Alex
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

[C++] algorithm help Empty Re: [C++] algorithm help

Post  bgthor Fri Feb 24, 2012 5:31 am

awesome will give it a go

bgthor

Posts : 7
Join date : 2012-02-24

Back to top Go down

[C++] algorithm help Empty Re: [C++] algorithm help

Post  Mr E Bear Fri Feb 24, 2012 5:39 am

AlexDiru wrote:
float MINUS_INFINITY = -INFINITY;

Interested how this works? Doubtless it does, I got mine by doing -1/0, so this is new to me Embarassed

Mr E Bear

Posts : 1
Join date : 2012-02-24

Back to top Go down

[C++] algorithm help Empty Re: [C++] algorithm help

Post  bgthor Fri Feb 24, 2012 5:42 am

[C++] algorithm help New_bi10

sadly the same thing happens. it may be that I am using express as it seemed to work fine on the full version on uni computers.

bgthor

Posts : 7
Join date : 2012-02-24

Back to top Go down

[C++] algorithm help Empty Re: [C++] algorithm help

Post  Alex Fri Feb 24, 2012 6:04 am

Can you post your new code, cos I have Pro version so I can test this, I can also test with g++ if I turn my netbook on
Alex
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

[C++] algorithm help Empty Re: [C++] algorithm help

Post  bgthor Fri Feb 24, 2012 6:12 am

#include<iostream>
#include<time.h>
using namespace std;
const int size = 8;

void GenData(float* List);
double BubbleSort(float* List);
void PrintData(float* Lift);

int main()
{
const int Num = 1;
double ExecTimes[Num];
float AvgTime=0;
float x[size];

int zero = 0;
int one = 1;
float inf = one/ (one - 1.0);
float minusinf = -inf;
float nan = (one - 1.0) / (one - 1.0);
float neg = -1.0;
float neg_zero = neg*zero;


//x[0]= minusinf;
//x[1]= inf;
//x[2]= neg_zero;
//x[3]= nan;
//x[4]= 0.0;
//x[5]= neg;
//x[6]= 4;
//x[7]= 4;

for(int i=0;i<Num;i++)
{
//GenData(x);
ExecTimes[i]=BubbleSort(x);
PrintData(x);
cout<<"Run "<<i+1<<" "<<ExecTimes[i]<<endl;
AvgTime += ExecTimes[i];
}
AvgTime /= Num;
cout<<"Average "<<AvgTime<<endl;

}

void PrintData(float* List)
{
for(int i=0;i<size;i++)
{cout<<List[i]<<", ";}
cout<<endl;
}

void GenData(float* List)
{
srand(time(NULL));
for (int i=0; i<size; i++)
{
List[i]=(float)rand()/(float) RAND_MAX;
}
}

double BubbleSort(float* List)
{
bool Sorted;
float t;
int ExecTime;
time_t startTime,endTime;
time(&startTime);
do
{
Sorted=true;
for(int i=0; i<size-1;i++)
{
if(List[i] > List[i+1])
{
t=List[i];
List[i]=List[i+1];
List[i+1]=t;
Sorted=false;
}
}
}while(!Sorted);
time(&endTime);
ExecTime = difftime(endTime,startTime);
return ExecTime;
}

bgthor

Posts : 7
Join date : 2012-02-24

Back to top Go down

[C++] algorithm help Empty Re: [C++] algorithm help

Post  Alex Fri Feb 24, 2012 6:28 am

Hmmm doesn't work me, post the code on pastebin (to keep formatting) and I'll have a look

http://pastebin.com/
Alex
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

[C++] algorithm help Empty Re: [C++] algorithm help

Post  daveclarke Fri Feb 24, 2012 6:50 am

Your calling GenData after setting those values, and therfore they are getting overwritten.

Deleted my other post cause it didnt appear to be sorting correctly -

This is close, but not 100% working...I've gotta go now, enjoy!

Code:

#include<iostream>
#include<time.h>

using namespace std;
#define size 15

float zero = 0.0;
#define INFINITY 1.0/zero
#define MINUS_INFINITY -1.0/zero
#define MINUS_ZERO 1.0/(MINUS_INFINITY)
#ifndef NAN
static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff};
#define NAN (*(const float *) __nan)
#endif


void GenData(float* List);
double BubbleSort(float* List);
void PrintData(float* Lift);

int main()
{
srand(time(NULL));
const int Num = 1;
double ExecTimes[Num];
float AvgTime=0;
float x[size];
float *xptr = x;


//GenData(xptr);
for(int x=0;x<size;x++) { xptr[x] = (((float) rand()) / (float) RAND_MAX)*1000000.0 - 500000.0; }
xptr[5] = MINUS_INFINITY;
xptr[2] = INFINITY;
xptr[7] = NAN;
xptr[12] = MINUS_ZERO;


int i=0;
ExecTimes[i]=BubbleSort(xptr);
PrintData(xptr);
cout <<"Run "<<i+1<<" "<<ExecTimes[i]<<endl;
AvgTime += ExecTimes[i];

AvgTime /= Num;
cout<<"Average "<<AvgTime<<endl;


}

void PrintData(float* List)
{
for(int i=0;i<size;i++)
{cout<<List[i]<<"\n";}
cout<<endl;
}

void GenData(float* List)
{

for (int i=0; i<size; i++)
{
List[i]=((float)rand()/(float) RAND_MAX)*10000;
}
}

double BubbleSort(float *List)
{
bool Sorted;
float t;
int ExecTime;
time_t startTime,endTime;
time(&startTime);
do
{
Sorted=true;
for(int i=0; i<size-1;i++)
{
if(List[i] > List[i+1])
{
swap(List[i], List[i+1]);
Sorted=false;
}
}
}while(!Sorted);
time(&endTime);
ExecTime = difftime(endTime,startTime);
return ExecTime;
}

daveclarke

Posts : 8
Join date : 2012-02-24

Back to top Go down

[C++] algorithm help Empty Re: [C++] algorithm help

Post  bgthor Fri Feb 24, 2012 6:53 am

sorts the generated data but not too sure about the inf.

thank you very much guys I am now most of the way there with the rest of the paper work so I need to get somebody a drink. mostly myself but if you two are around tomorrow at the mini social then demand alcamahol.

bgthor

Posts : 7
Join date : 2012-02-24

Back to top Go down

[C++] algorithm help Empty Re: [C++] algorithm help

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum