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;
}


LVL 1
warriorfan808Asked:
Who is Participating?
 
List244Commented:
In full:

#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;
  }
end = clock() ;
total = end - start;
  printf("The clock time is %d\n",total);
}
0
 
List244Commented:
Warrior, couple problems:

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...

0
 
List244Commented:
Once you find the correct placement for those lines and you fix your printf statement
your program should run.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
List244Commented:
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;
}
0
 
List244Commented:
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);
}
0
 
warriorfan808Author Commented:
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.
0
 
List244Commented:
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.
0
 
warriorfan808Author Commented:
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.
0
 
List244Commented:
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.
0
 
warriorfan808Author Commented:
Yeah, that's exactly what I wanted.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.