warriorfan808
asked on
Having trouble with clock function on my lexicographic program
I'm not sure what it wants. Compiler is saying, "missing storage class or type identifier"
#include <stdio.h>
#include <time.h>
clock_t start, end, total;
clock_t totaln = 0;
start = clock() ;
void print(const int *v, const int size)
{
if (v != 0) {
for (int i = 0; i < size; i++) {
printf("%4d", v[i] );
}
printf("\n");
}
} // print
void swap(int *v, const int i, const int j)
{
int t;
t = v[i];
v[i] = v[j];
v[j] = t;
}
void rotateLeft(int *v, const int start, const int n)
{
int tmp = v[start];
for (int i = start; i < n-1; i++) {
v[i] = v[i+1];
}
v[n-1] = tmp;
} // rotateLeft
void permute(int *v, const int start, const int n)
{
print(v, n);
if (start < n) {
int i, j;
for (i = n-2; i >= start; i--) {
for (j = i + 1; j < n; j++) {
swap(v, i, j);
permute(v, i+1, n);
} // for j
rotateLeft(v, i, n);
} // for i
}
} // permute
void init(int *v, int N)
{
for (int i = 0; i < N; i++) {
v[i] = i+1;
}
} // init
end = clock() ;
total = end - start;
void
main()
{
char buf[80];
int N;
printf("Enter your Elements: ");
fgets(buf, sizeof(buf), stdin );
sscanf(buf, "%d", &N);
if (N > 0 && N <= 6)
{
int *v = new int[N];
init(v, N);
permute(v, 0, N);
delete [] v;
}
printf("The clock time is"<< total <<endl;
}
#include <stdio.h>
#include <time.h>
clock_t start, end, total;
clock_t totaln = 0;
start = clock() ;
void print(const int *v, const int size)
{
if (v != 0) {
for (int i = 0; i < size; i++) {
printf("%4d", v[i] );
}
printf("\n");
}
void swap(int *v, const int i, const int j)
{
int t;
t = v[i];
v[i] = v[j];
v[j] = t;
}
void rotateLeft(int *v, const int start, const int n)
{
int tmp = v[start];
for (int i = start; i < n-1; i++) {
v[i] = v[i+1];
}
v[n-1] = tmp;
} // rotateLeft
void permute(int *v, const int start, const int n)
{
print(v, n);
if (start < n) {
int i, j;
for (i = n-2; i >= start; i--) {
for (j = i + 1; j < n; j++) {
swap(v, i, j);
permute(v, i+1, n);
} // for j
rotateLeft(v, i, n);
} // for i
}
} // permute
void init(int *v, int N)
{
for (int i = 0; i < N; i++) {
v[i] = i+1;
}
} // init
end = clock() ;
total = end - start;
void
main()
{
char buf[80];
int N;
printf("Enter your Elements: ");
fgets(buf, sizeof(buf), stdin );
sscanf(buf, "%d", &N);
if (N > 0 && N <= 6)
{
int *v = new int[N];
init(v, N);
permute(v, 0, N);
delete [] v;
}
printf("The clock time is"<< total <<endl;
}
Once you find the correct placement for those lines and you fix your printf statement
your program should run.
your program should run.
Perhaps something along the lines of:
#include <stdio.h>
#include <time.h>
void print(const int *v, const int size)
{
if (v != 0) {
for (int i = 0; i < size; i++) {
printf("%4d", v[i] );
}
printf("\n");
}
} // print
void swap(int *v, const int i, const int j)
{
int t;
t = v[i];
v[i] = v[j];
v[j] = t;
}
void rotateLeft(int *v, const int start, const int n)
{
int tmp = v[start];
for (int i = start; i < n-1; i++) {
v[i] = v[i+1];
}
v[n-1] = tmp;
} // rotateLeft
void permute(int *v, const int start, const int n)
{
print(v, n);
if (start < n) {
int i, j;
for (i = n-2; i >= start; i--) {
for (j = i + 1; j < n; j++) {
swap(v, i, j);
permute(v, i+1, n);
} // for j
rotateLeft(v, i, n);
} // for i
}
} // permute
void init(int *v, int N)
{
for (int i = 0; i < N; i++) {
v[i] = i+1;
}
} // init
void main()
{
clock_t start, end, total;
clock_t totaln = 0;
start = clock() ;
char buf[80];
int N;
printf("Enter your Elements: ");
fgets(buf, sizeof(buf), stdin );
sscanf(buf, "%d", &N);
if (N > 0 && N <= 6)
{
int *v = new int[N];
init(v, N);
permute(v, 0, N);
delete [] v;
}
printf("The clock time is %D\n",total);
end = clock() ;
total = end - start;
}
#include <stdio.h>
#include <time.h>
void print(const int *v, const int size)
{
if (v != 0) {
for (int i = 0; i < size; i++) {
printf("%4d", v[i] );
}
printf("\n");
}
void swap(int *v, const int i, const int j)
{
int t;
t = v[i];
v[i] = v[j];
v[j] = t;
}
void rotateLeft(int *v, const int start, const int n)
{
int tmp = v[start];
for (int i = start; i < n-1; i++) {
v[i] = v[i+1];
}
v[n-1] = tmp;
} // rotateLeft
void permute(int *v, const int start, const int n)
{
print(v, n);
if (start < n) {
int i, j;
for (i = n-2; i >= start; i--) {
for (j = i + 1; j < n; j++) {
swap(v, i, j);
permute(v, i+1, n);
} // for j
rotateLeft(v, i, n);
} // for i
}
} // permute
void init(int *v, int N)
{
for (int i = 0; i < N; i++) {
v[i] = i+1;
}
} // init
void main()
{
clock_t start, end, total;
clock_t totaln = 0;
start = clock() ;
char buf[80];
int N;
printf("Enter your Elements: ");
fgets(buf, sizeof(buf), stdin );
sscanf(buf, "%d", &N);
if (N > 0 && N <= 6)
{
int *v = new int[N];
init(v, N);
permute(v, 0, N);
delete [] v;
}
printf("The clock time is %D\n",total);
end = clock() ;
total = end - start;
}
Sorry, make that:
void main()
{
clock_t start, end, total;
clock_t totaln = 0;
start = clock() ;
char buf[80];
int N;
printf("Enter your Elements: ");
fgets(buf, sizeof(buf), stdin );
sscanf(buf, "%d", &N);
if (N > 0 && N <= 6)
{
int *v = new int[N];
init(v, N);
permute(v, 0, N);
delete [] v;
}
end = clock() ;
total = end - start;
printf("The clock time is %d\n",total);
}
void main()
{
clock_t start, end, total;
clock_t totaln = 0;
start = clock() ;
char buf[80];
int N;
printf("Enter your Elements: ");
fgets(buf, sizeof(buf), stdin );
sscanf(buf, "%d", &N);
if (N > 0 && N <= 6)
{
int *v = new int[N];
init(v, N);
permute(v, 0, N);
delete [] v;
}
end = clock() ;
total = end - start;
printf("The clock time is %d\n",total);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Didn't notice that printf mix up. I should have done something like printf( "time = %u\n", total ) ;
I'll go through the code you just posted. man, you're fast.
I'll go through the code you just posted. man, you're fast.
Yeah, and if you want to ignore the time it takes to input the data which I have included, you should move your start of the clock
to after they enter the data. I don't know if you meant for that time to be included.
to after they enter the data. I don't know if you meant for that time to be included.
ASKER
Oh ok, I see it now. I guess it would clock it within the main. I was trying to see how long...nm.. I guess it's better your way. Let me see if I can get it to run.
Take my last example, replace the function: main with:
void main()
{
clock_t start, end, total;
clock_t totaln = 0;
char buf[80];
int N;
printf("Enter your Elements: ");
fgets(buf, sizeof(buf), stdin );
sscanf(buf, "%d", &N);
start = clock() ; ///NOTICE HERE -- Start the clock AFTER they give input
if (N > 0 && N <= 6)
{
int *v = new int[N];
init(v, N);
permute(v, 0, N);
delete [] v;
}
end = clock() ;
total = end - start;
printf("The clock time is %d\n",total);
}
That would make it so that the time it take them to type their input is ignored.
void main()
{
clock_t start, end, total;
clock_t totaln = 0;
char buf[80];
int N;
printf("Enter your Elements: ");
fgets(buf, sizeof(buf), stdin );
sscanf(buf, "%d", &N);
start = clock() ; ///NOTICE HERE -- Start the clock AFTER they give input
if (N > 0 && N <= 6)
{
int *v = new int[N];
init(v, N);
permute(v, 0, N);
delete [] v;
}
end = clock() ;
total = end - start;
printf("The clock time is %d\n",total);
}
That would make it so that the time it take them to type their input is ignored.
ASKER
Yeah, that's exactly what I wanted.
First, you have lines like:
clock_t totaln = 0;
start = clock() ;
end = clock() ;
total = end - start;
floating in the middle of nowhere, they need a function, they need scope.
Second, you are using printf:
printf("The clock time is"<< total <<endl;
Yet you seem to try switching to cout style halfway through...