Marek_Z moderator
Dołączył: 08 Paź 2007 Posty: 59
|
Wysłany: Wto Maj 19, 2009 19:15 Temat postu: Ćwiczenia - inna wersja tabelki z pomiarem czasu wykonania |
|
|
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;
const int n=80;
int jest;
int i;
clock_t start, stop, startprog, stopprog;
int tab[n]={1,2,3,2,-7,44,5,1,0,-3,1,2,3,1,2,3,1,2,3,1,1,2,3,2,-7,44,5,1,0,-3,1,2,3,1,2,3,1,2,3,1,1,2,3,2,-7,44,5,1,0,-3,1,2,3,1,2,3,1,2,3,1,1,2,3,2,-7,44,5,1,0,-3,1,2,3,1,2,3,1,2,3,1};
void szukaj_for(int tab[n],int left,int right,int x) //petla for ********************************
{
start=clock();
jest=0;
for(i=left;left<=right;i++)
{
if (tab[left]==x)
{
cout<<"Znalazlem szukany element "<<x<<" jest na miejscu "<<(left)<<" tablicy "<<endl;
jest=jest+1;
}
left=left+1;
}
if (jest==0) cout<<"Element "<<x<<" nie zostal odnaleziony\n";
if (jest>0) cout<<endl<<"Element "<<x<<" wystapil razy: "<<jest<<endl;
stop=clock();
cout<<endl<<"Petla FOR wykonana w czasie: "<<(float)(stop-start)/(float)CLK_TCK<<endl<<endl;
} //end for ************************************
void szukaj_while(int tab[n],int left,int right,int x) //petla while =================================
{
start=clock();
jest=0;
while(left!=right)
{
if (tab[left]==x)
{
cout<<"Znalazlem szukany element "<<x<<" jest na miejscu "<<(left)<<" tablicy "<<endl;
jest=jest+1;
}
left=left+1;
}
if (jest==0) cout<<"Element "<<x<<" nie zostal odnaleziony\n";
if (jest>0) cout<<endl<<"Element "<<x<<" wystapil razy: "<<jest<<endl;
stop=clock();
cout<<endl<<"Petla WHILE wykonana w czasie: "<< (float)(stop-start)/(float)CLK_TCK <<endl<<endl;
} //end while ===================================
void szukaj_do(int tab[n],int left,int right,int x) //petla do -------------------------------------
{
start=clock();
jest=0;
do
{
if (tab[left]==x)
{
cout<<"Znalazlem szukany element "<<x<<" jest na miejscu "<<(left)<<" tablicy "<<endl;
jest=jest+1;
}
left=left+1;
}
while(left!=right);
if (jest==0) cout<<"Element "<<x<<" nie zostal odnaleziony\n";
if (jest>0) cout<<endl<<"Element "<<x<<" wystapil razy: "<<jest<<endl;
stop=clock();
cout<<endl<<"Petla DO WHILE wykonana w czasie: "<<(float)(stop-start)/(float)CLK_TCK<<endl<<endl;
} //end do ---------------------------------------
void szukaj_forbez(int tab[n],int left,int right,int x) //petla for bez parametrow ............................
{
start=clock();
jest=0;
for(;1;)
{
if (tab[left]==x)
{
cout<<"Znalazlem szukany element "<<x<<" jest na miejscu "<<(left)<<" tablicy "<<endl;
jest=jest+1;
}
left=left+1;
if (left==right) break;
}
if (jest==0) cout<<"Element "<<x<<" nie zostal odnaleziony\n";
if (jest>0) cout<<endl<<"Element "<<x<<" wystapil razy: "<<jest<<endl;
stop=clock();
cout<<endl<<"Petla FOR BEZ PARAMETROW wykonana w czasie: "<<(float)(stop-start)/(float)CLK_TCK<<endl<<endl;
} //end for bez parametrow ...............................
int main()
{
int y;
cout<<"wprowadz liczbe calkowita: ";
cin>>y;
startprog=clock();
cout<<"Petla FOR "<<endl<<endl;
szukaj_for(tab,0,n-1,y);
cout<<"Petla WHILE "<<endl<<endl;
szukaj_while(tab,0,n-1,y);
cout<<"Petla DO WHILE "<<endl<<endl;
szukaj_do(tab,0,n-1,y);
cout<<"Petla FOR BEZ PARAMETROW "<<endl<<endl;
szukaj_forbez(tab,0,n-1,y);
stopprog=clock();
cout<<endl;
cout<<endl<<"Wszystkie petle wykonane w czasie: "<<(float)(stopprog-startprog)/(float)CLK_TCK<<endl<<endl;
system("pause");
} _________________ Marek ZIN 3 (kiedyś 6) |
|