Thread: Homework

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    16

    Post Homework

    Hi, I wanna ask about what should I do next to solve this case ?
    Homework-capture-png

    This is my code that I have made:
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <unistd.h>
    
    
    int main()
    {
    	int h,m;
    	int D = 1000;
    	int numTestCases;
    	
    	scanf("%d",&numTestCases);
    	for ( int i = 1 ; i <= numTestCases ; i++ ) {
        printf("Case #%d:\n", i);
    	scanf("%d %d",&h,&m);
    	if (h > 12){
    		printf("Error!\n"); exit(0);
    	}
    	if  (m > 60){
    		printf("Error!\n"); exit(0);
    	}while(1)
    	{
    		if(h > 12){
    			h = 1; m =  0;
    		}
    		if(m > 59){
    			h +=1; m=0;
    		}
    		printf("\n%02d:%02d",h,m);
    		sleep(D);
    		system("cls");
    		}
    	}
    }
    is there anything that I can add ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > scanf("%d %d",&h,&m);
    How does this correspond to a single integer N between 0 and 1439?

    > sleep(D);
    Nothing is mentioned in your requirements to actually sleep.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, you need to indent your code more consistently, e.g.,
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
     
     
    int main()
    {
        int h, m;
        int D = 1000;
        int numTestCases;
         
        scanf("%d", &numTestCases);
        for (int i = 1; i <= numTestCases; i++) {
            printf("Case #%d:\n", i);
            scanf("%d %d", &h, &m);
            if (h > 12) {
                printf("Error!\n"); exit(0);
            }
            if  (m > 60) {
                printf("Error!\n"); exit(0);
            }
            while(1) {
                if (h > 12) {
                    h = 1;
                    m =  0;
                }
                if (m > 59) {
                    h += 1;
                    m = 0;
                }
                printf("\n%02d:%02d", h, m);
                sleep(D);
                system("cls");
            }
        }
    }
    Other comments concerning your code:
    • These are poorly named variables:
      Code:
      int h, m;
      You probably should rename them to hours and minutes respectively.
    • This is a poorly named variable:
      Code:
      int D = 1000;
      You should declare it to be a constant and give it a descriptive name, e.g.,
      Code:
      const int TEST_CASE_DISPLAY_PAUSE_TIME = 1000;
      Actually, your instructions don't tell you to pause the display and then clear the screen, so I don't know why you want to do this.
    • Although it probably isn't necessary, it is good practice to check the return value of scanf just in case it fails, and also to check that indeed the value of numTestCases is in the given range.
    • This is obviously wrong:
      Code:
      scanf("%d %d", &h, &m);
      Your instructions say that "Each test case will contain an number N, the number displayed by the strange clock" and this is backed up by the example, so reading two numbers instead of one for each test case is undeniably wrong. Consequently, your input range checking is also wrong.
    • You don't need an infinite loop; you should be displaying one output line per test case, so your infinite loop is actually wrong. You don't even need a loop to compute the hours and minutes given the input for the test case.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2018
    Posts
    16

    Post Code Remedy

    I Have my code reevaluated and simplified :
    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int main()
    {
        int h,m,N;
        int c;
        
        scanf("%d",&c);
        for(int i=1;i<=c;i++);
        {
            scanf("%d",&N);
            h[i] = N[i]/60;
            m[i] = N[i]%60;
            printf("Case #%d: %02d:%02d\n");
        }
        return 0;
    }
    This is the simplified code, i hope you can help me with this.
    this thing is complaining about:
    Code:
    [Error] invalid types 'int[int]' for array subscript
    Please, I need your help

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    There is no need for any arrays in this problem.

    Also, watch the ; at the end of the for loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework help
    By f0fin in forum C Programming
    Replies: 7
    Last Post: 09-22-2013, 02:58 PM
  2. Homework Help
    By xxdustinxx in forum C Programming
    Replies: 14
    Last Post: 11-29-2011, 09:38 AM
  3. Homework help?
    By Peter Benzoni in forum C Programming
    Replies: 2
    Last Post: 10-13-2011, 11:59 PM
  4. Homework?
    By Cloud6 in forum C++ Programming
    Replies: 19
    Last Post: 07-13-2006, 07:51 PM
  5. I'll do your homework for you....
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 04-13-2006, 03:14 PM

Tags for this Thread