Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7089365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:54:46+00:00 2026-05-28T07:54:46+00:00

Ok, so I am trying to implement the Collatz problem in C, and record/print

  • 0

Ok, so I am trying to implement the Collatz problem in C, and record/print the time it takes for the while loop to execute. I am supposed to report both the number of “ticks” and the time in seconds. However, I am getting some seemingly simple errors from my code but for whatever reason, I am not sure how to correct them.

This is my code

#include <stdio.h>
#include <time.h>

void main() {
    int n, c = 0;
    printf("Please enter an integer...\n");
    scanf("%d", &n);
    clock_t start;   /* Line 8 */
    clock_t finish;  /* Line 9 */

    start = clock();
    while (n != 1) {
        if (n%2 == 0)
            n = n/2;
        else
            n = (3*n)+1;
        c++;
        printf("n=%d\n", n);
    }
    finish = clock() - start;
    double interval = finish / (double)CLOCKS_PER_SEC; 

        printf("%d iterations\n", c);
    printf("%f clock cycles", finish); 
    printf("%f seconds elapsed", interval);
}

These are the errors Visual Studio is reporting

Line 8 and 9 Errors

‘clock_t’ : illegal use of this type as an expression

syntax error : missing ‘;’ before identifier ‘start’

‘start’ : undeclared identifier

I am also getting an ‘undeclared identifier’ error for all lines in which ‘start’ or ‘finish’ show up

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:54:47+00:00Added an answer on May 28, 2026 at 7:54 am

    Try moving the variables to the top before any statements…

    void main() {
        int n, c = 0;
        clock_t start;   /* Line 8 */
        clock_t finish;  /* Line 9 */
        printf("Please enter an integer...\n");
        scanf("%d", &n);
    

    The rest of your errors are just consequences of these not being declared properly.

    In a later C standard (C99) declarations can be mixed in anywhere. Often it’s just easier to make it compliant… Another approach is to introduce a block { } like so:

    void main() {
        int n, c = 0;
        printf("Please enter an integer...\n");
        scanf("%d", &n);
        {
          clock_t start;   /* Line 8 */
          clock_t finish;  /* Line 9 */
    
          start = clock();
          while (n != 1) {
              if (n%2 == 0)
                  n = n/2;
              else
                  n = (3*n)+1;
              c++;
              printf("n=%d\n", n);
          }
          finish = clock() - start;
          double interval = finish / (double)CLOCKS_PER_SEC;
    
          printf("%d iterations\n", c);
          printf("%f clock cycles", finish);
          printf("%f seconds elapsed", interval);
        }
    }
    

    In this case, it’s not an ideal solution as it’s simple enough to rearrange your code. But sometimes this is useful when trying to make C99 code compile in C89 without rearranging things too much.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While trying to implement an MVC file upload example on Scott Hanselman's blog. I
I have the following problem. Background I'm trying to implement an autocomplete selector with
Trying to implement NSCopying for the first time, and I have a question about
When trying to implement a simple OpenGL application, I was surprised that while it
I'm trying to implement the code from showing dialog while loading layout by setContentView
Im trying to implement pagination using multiple searching criteria. Supposed I Have student table.
The Problem : I am trying to implement a search algorithm that shows the
Trying to implement a simple print in SL4. I have a DataGrid that I
All I am currently trying implement something along the lines of dim l_stuff as
trying to implement a dialog-box style behaviour using a separate div section with all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.