Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

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


0
warriorfan808
Asked:
warriorfan808
  • 7
  • 3
1 Solution
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now