我正在尝试计算我的代码的执行时间(以毫秒为单位)但是当我运行它时没有任何结果也没有错误我不知道问题究竟在哪里尽管我之前提到的没有任何错误。请帮忙
这是我的代码:
#include "stdafx.h"
#include<iostream>
#include<fstream>
using namespace std ;
#include<time.h>
class sort{
public:
int read_file()
{
int x ;
char filename[50];
ifstream numbersfile ;
cout<<"Please enter the file name below"<<endl<<"_______________________________________________"<<endl;
cin.getline(filename,50);
cout<<"_______________________________________________"<<endl;
numbersfile.open(filename);
if(!numbersfile.is_open()){
cout<<"Failed to load requierd file"<<endl;
return 0 ;
}
int arr[13];
int n=sizeof(arr)/sizeof(*arr);
cout<<"The elements supposed to be sorted are:"<<endl<<endl;
for(int i =0;i<13;i++){
numbersfile>>arr[i];
cout<<arr[i]<<" ";
}
cout<<"\n-----------------------------------------------";
cout<<endl<<endl;
counting_sort(arr,n);
}
int counting_sort(int arr[],int size)
{
int n=size;
int max=arr[0];
for (int i=1;i<n;i++) {
if (arr[i]>max) {
max=arr[i];
}
}
int *output_array=new int[n];
for (int i=0;i<n;i++) {
output_array[i]=0;
}
int *count=new int[max+1];
for (int i=0;i<=max+1;i++) {
count[i]=0;
}
for (int i=0;i<n;i++){
count[arr[i]]=count[arr[i]]+1;
}
for (int i=1;i<max+1;i++) {
count[i]=count[i]+count[i-1];
}
for (int i=n-1;i>=1;i--) {
output_array[count[arr[i]]-1]=arr[i];
count[arr[i]]=count[arr[i]]-1;
}
cout<<"The sorted elements are:"<<endl<<endl;
for (int i=0;i<n;i++) {
cout<<output_array[i]<<" ";
}
cout<<"\n-----------------------------------------------"<<endl;
return 0;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
clock_t Start_Time = clock();
sort s1;
s1.read_file();
return 0;
cout <<"The time taken to execute this process is:\n"<< double( clock() - Start_Time ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
}
答案 0 :(得分:3)
主要的cout
声明是在您的退回声明之后。这意味着该代码行将永远不会运行,因为程序已经返回。你可以交换这两行,你就会得到你的输出。