Thread: First Ever Programming Course. In need of quick help

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    2

    First Ever Programming Course. In need of quick help

    Hey all. I need some quick help on some homework. The question is as follows:
    "Write a C function definition for a function called printKSpacesNXs that takes two integer parameters, k and x. The function should pring k spaces, and then n lowercase x characters on standard output. It should NOT print a new line after it is finished."

    I am utterly confused. When he explains it in class, it makes complete sense. But when I try it on my own, it doesn't.

    Can anyone help me out?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well the first question is:
    "The function should pring k spaces, and then n lowercase x characters on standard output"
    What is n? The function only takes 2 integer parameters k and x, so where did n come from?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    2
    n is just a number, nothing specific.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by QueNerdo View Post
    n is just a number, nothing specific.
    Okay so I guess your function should start with
    Code:
    int n = "just a number, nothing specific";
    since n is not supplied to the function.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by QueNerdo View Post
    n is just a number, nothing specific.
    That makes no sense from a programming standpoint. n is a variable. That variable must be equal to a value. That value is the number of times that you print out x.

    Anyways, as for your original issue...
    You need to perform some action a specific number of times. This is how you do that in C:
    Code:
    int i;
    for(i = 0; i < number_of_times; i++)
        printf("I'm doing an action!\n");
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Code:
    #include <stdio.h>
    
    int printKSpacesNXs(unsigned k, unsigned n)
    {
        return putchar(k?0x20:n?0x58:0x00)&&(k?printKSpacesNXs(--k,n):n?printKSpacesNXs(k,--n):0);
    }
    
    int main(void)
    {
        printKSpacesNXs(4,1);
        printf("\n");
        printKSpacesNXs(0,7);
        printf("\n");
        return 0;
    }
    Last edited by NeonBlack; 10-14-2009 at 10:52 AM.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  3. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  4. Quick Sort Help
    By NavyBlue in forum C Programming
    Replies: 1
    Last Post: 03-02-2003, 10:34 PM
  5. Replies: 0
    Last Post: 04-30-2002, 07:24 PM