Thread: Homework

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

    Exclamation Homework

    Hi guys ! could you help me to resolve my problem of this quiz ?prob-12.pdf

    this is my code :

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    int main()
    {
        int counter;
    int num;
    
    scanf("%d", &counter);
    printf("Case #1");
    while(counter != 0)
        {
            printf("\n%d", counter);
    counter --;
    sleep(1);
    }
        for(num=5)
        {
            printf("test");
    }
        return 0;
    }
    I've search about this article in youtube, google and my university's site, but it doesn't help, I couldn't find the answer.

    the code's result say this :
    Code:
    /Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/audynaufal/CLionProjects/untitled/cmake-build-debug --target untitled -- -j 2Scanning dependencies of target untitled
    [ 50%] Building C object CMakeFiles/untitled.dir/main.c.o
    /Users/audynaufal/CLionProjects/untitled/main.c:15:9: warning: implicit declaration of function 'sleep' is invalid in C99 [-Wimplicit-function-declaration]
            sleep(1);
            ^
    /Users/audynaufal/CLionProjects/untitled/main.c:17:14: error: expected ';' in 'for' statement specifier
        for(num=5)
                 ^
    /Users/audynaufal/CLionProjects/untitled/main.c:17:14: error: expected ';' in 'for' statement specifier
    1 warning and 2 errors generated.
    make[3]: *** [CMakeFiles/untitled.dir/main.c.o] Error 1
    make[2]: *** [CMakeFiles/untitled.dir/all] Error 2
    make[1]: *** [CMakeFiles/untitled.dir/rule] Error 2
    make: *** [untitled] Error 2
    please help me guys.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,608
    Correct me if I'm wrong, but it looks like you are compiling for the macintosh. You can fix the error with sleep(1); by including the Mac header for it: #include <unistd.h>. More here.

    Review how the for loop is written. The compiler tells you what is wrong with it, but any lesson review will help.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    3,471
    Review how one uses a for loop For, While and Do While Loops in C - Cprogramming.com

    Find out what header you need to include when using the sleep function.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

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

    Post

    This is my case that I can't solve :

    Homework-untitled-jpg

    I've searched about it please help me.
    the problem is to replace some number with X Second Till New Year !
    for the timer it's self, I've already made it.
    Last edited by Audy Naufal; 10-09-2018 at 10:39 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,142
    Quote Originally Posted by Audy Naufal
    the problem is to replace some number with X Second Till New Year !
    Just check for the special numbers and print different output then.
    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

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,745
    Read this.
    A development process

    This is your assignment.
    Format Input
    The first line will be an integer T, the number of test cases.
    Each test case will contain an integer N, the starting number.

    Format Output
    Print “Case #X:” on the first line of each test case.
    Then print the countdown each on their own line.
    Look at each statement in turn, and just write the code for that.
    So.
    Code:
    // The first line will be an integer T, the number of test cases.
    scanf("%d",&numTestCases);
    for ( int i = 1 ; i <= numTestCases ; i++ ) {
        // Print “Case #X:” on the first line of each test case.
        printf("Case #%d:\n", i);
    }
    Then it becomes say
    Code:
    // The first line will be an integer T, the number of test cases.
    scanf("%d",&numTestCases);
    for ( int i = 1 ; i <= numTestCases ; i++ ) {
        // Print “Case #X:” on the first line of each test case.
        printf("Case #%d:\n", i);
        // Each test case will contain an integer N, the starting number.
        scanf("%d",&startNumber);
        // Then print the countdown each on their own line.
        for ( int c = startNumber ; c >= 1 ; c-- ) {
            printf("%d\n", c);
        }
    }
    Each step of the way, you add one more requirement to the program.
    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.

  7. #7
    Registered User
    Join Date
    Oct 2018
    Posts
    13
    This is my real problem with that case : replacing some number with "X SECONDS TILL NEW YEAR!!"

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,745
    What's so hard about adding
    if ( c == 10 )

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

  9. #9
    Registered User
    Join Date
    Oct 2018
    Posts
    13

    Post

    Quote Originally Posted by Salem View Post
    What's so hard about adding
    if ( c == 10 )

    ?
    where should I place it ?

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    int main() {
        int numTestCases;
    int startNumber;
    scanf("%d", &numTestCases);
    for (int i = 1; i <= numTestCases; i++) {
            printf("Case #%d:\n", i);
    scanf("%d", &startNumber);
    for (int c = startNumber; c >= 1; c--) {
                printf("%d\n", c);
    {
                    if (c == 10);
    printf("damn");
    }
            }
        }
        return 0;
    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,142
    Quote Originally Posted by Audy Naufal
    where should I place it ?
    What is it for? Answer that and where to place it should become obvious. Presently, it sounds like you have abdicated your thinking processes and so all you're hoping for is the final answer served on a plate. No, think about what the program is supposed to do. Come up with an algorithm to do it. Draw a flow chart or write pseudocode to help you frame your thoughts about the algorithm more clearly, and then you can implement your algorithm and test it.
    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

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,745
    > where should I place it ?
    Did you actually run that code?
    What did you observe?

    Something happened for sure, but what exactly?

    You need to learn how to match your observations to the code you wrote.
    You also need to learn how to predict what certain code patterns will result in.

    Between those things, you'll learn how to write code which has a high chance of doing what you want first time.
    Even when it doesn't work, you'll have the tools to enable you to figure out what caused the difference between expectation and reality, and fix it.

    Sure, we could give you the one-line of extra code which enables you to run off to teacher feeling all smug about your 'success', but you won't have learnt anything about how to create a program. Plus you'll be back in exactly the same situation (ie clueless) when you get your next assignment.
    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.

  12. #12
    Registered User
    Join Date
    Oct 2018
    Posts
    13

    Post

    Hi, this is my last question.
    here is the output of my code:

    Code:
    Case #1:9
    9
    8
    7
    6
    SECONDS TILL NEW YEAR!!5
    4
    3
    2
    1
    Case #2:
    7
    7
    6
    SECONDS TILL NEW YEAR!!5
    4
    3
    2
    1
    Program ended with exit code: 0
    how do I swap the position of SECONDS TILL NEW YEAR!!5 into 5SECONDS TILL NEW YEAR!!?

    this is my code:
    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int main() {
        int numTestCases;
        int startNumber;
        scanf("%d", &numTestCases);
        for (int i = 1; i <= 2; i++) {
            printf("Case #%d:\n", i);
            scanf("%d", &startNumber);
            for (int c = startNumber; c >= 1; c--) {
                if (c == 5)
    printf("SECONDS TILL NEW YEAR!!");
                if (c == 10)
    printf("SECONDS TILL NEW YEAR!!");
                if (c ==30)
    printf("SECONDS TILL NEW YEAR!!");
                if (c == 60)
    printf("SECONDS TILL NEW YEAR!!");
                printf("%d\n", c);
                }
            }
        }

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,142
    That's literally just a matter of changing where you print the number, and separating the printing of the newline from the printing of the number.
    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

  14. #14
    Registered User
    Join Date
    Oct 2018
    Posts
    13

    Post

    Quote Originally Posted by laserlight View Post
    That's literally just a matter of changing where you print the number, and separating the printing of the newline from the printing of the number.
    How ?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,142
    What does this do?
    Code:
    printf("%d\n", c);
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework help please
    By Feras Kakish in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2015, 11:06 AM
  2. C homework
    By wilson5182004 in forum C Programming
    Replies: 6
    Last Post: 03-01-2009, 02:21 PM
  3. help with homework
    By abhiii in forum C Programming
    Replies: 2
    Last Post: 02-13-2009, 01:48 PM
  4. Help on Homework
    By Sentiax in forum C Programming
    Replies: 13
    Last Post: 02-04-2009, 12:33 AM
  5. Homework Help Please
    By mars4bb in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2005, 05:16 PM

Tags for this Thread