Like Tree2Likes

C Program beginner

This is a discussion on C Program beginner within the C Programming forums, part of the General Programming Boards category; So I need help with my homework, please help if you can; I recently started a class that requires I ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    13

    Question C Program beginner

    So I need help with my homework, please help if you can;

    I recently started a class that requires I know C, but the prerequisite classes that I took for that class taught C++, weird, but anyway since I don't understand C, I was wondering if you guys can help out.

    I've tried learning C, but I can't get some of the stuff.
    The first question asks:
    a.1) to create a size n array of integer items by reading the values from the
    keyboard
    and..this is my attempt..
    Code:
    #include<stdio.h>
    
    
    
    
    
    int main()
    {
        int n;
        printf("how many keys?\n");
        getchar();
        n=getchar();
        int keys[n];
        scanf("%d",&n);
        printf(n);
        getchar();
        return 0;
    }
    Not sure what's happening, but the program crashes after enter two chars.

    P.S I'm not trying to get an easy answer, I want to be able to master this stuff so I can return the favor

    Thanks for reading~ Hopefully you guys can help.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    Let's start with this: What compiler are you using? You're attempting to use variable-length arrays, which may not be supported by the compiler.

    And, have you learned about pointers and dynamic memory allocation using malloc?
    stahta01 likes this.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    I'm using bloodshed

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    I've learned pointers, but not dynamic memory allocation and nothing with malloc.

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    OK, that's an old buggy unsupported IDE. Note that it's not a compiler; the compiler included is GCC, which is likely an old version and not capable of offering variable-length arrays. Therefore, please let us know the answer to the second question I edited in.

    Consider moving to a newer, supported development environment like Visual C++ Express, wxDev-C++ (the more up-to-date version of "Bloodshed"), or Code::Blocks. If you're just doing C, then Pelles C is another good option.

    EDIT: If you've not learned malloc, then you can only declare an array of a known size and read values into that (as long as you have space to store them). I'm guessing you're supposed to know about malloc at this point.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    As far as I know in C you cannot create an array of a lenght an integer read either on a file or introduced by the user, so the instruction
    Code:
    int key[n];
    should give you compilation problems.

    Another command line that should give you problems is
    Code:
    printf(n);
    Should say
    Code:
    printf("%d",n);
    if what you want to print is the so-called array dimension.

    If you can explain more about you assignment I could help, but the only way I know to do it is by using dynamic memory allocation (i.e. malloc). For what I understand is being able to create an array of any size, entering the size by the user. In general should be something like:
    Code:
    #include<malloc.h>
    int main(){
    int n;
    int *key;
    printf("How many keys? ");
    getchar();
    n=getchar();
    key = (int*) malloc(n *sizeof(int));
    for(i=0;i<n;i++)
    {
    key[i] = i;
    printf("%d ", key[i]);
    }
    printf("\n");
    return 0;
    }
    This program should create an array of size n and fill it with the first n naturals (starting at 0).

    Hope this help.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    84
    Quote Originally Posted by rags_to_riches View Post
    EDIT: If you've not learned malloc, then you can only declare an array of a known size and read values into that (as long as you have space to store them). I'm guessing you're supposed to know about malloc at this point.
    Not true. Any compiler with C99 specs can create a variable length array or VLA.

    What's the first part of the question? It seems bit off.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by striderx240 View Post
    Code:
    int main()
    {
        int n;
        printf("how many keys?\n");
        getchar();
        n=getchar();
        int keys[n];
        scanf("%d",&n);
        printf(n);
        getchar();
        return 0;
    }
    Not sure what's happening, but the program crashes after enter two chars.
    n is an int. getchar gets a char. (Technically it is an int, but not in the sense that you want.) If I type 9, the value of the character 9 will be returned to n, which is like 57 as an integer value. So n is going to be 57, giving you an array of 57 integers. Not the 9 integers you wanted. But you need to be asking for the value of n before you make the array, not afterward.
    Code:
        scanf("%d",&n);
        int keys[n];
    Otherwise, since you haven't actually initialized n, it's got some random value, and it could be trying to make an array of 2000000 integers or something.

    Another crash is likely that line though. The printf function expects a formatting string as its first argument, not an integer value. (Yes, you can use VLAs in C99 as previously mentioned.)


    Quzah.
    Last edited by quzah; 01-26-2012 at 06:09 PM.
    whiteflags likes this.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    Quote Originally Posted by MartaC View Post
    As far as I know in C you cannot create an array of a lenght an integer read either on a file or introduced by the user, so the instruction
    Code:
    int key[n];
    should give you compilation problems.

    Another command line that should give you problems is
    Code:
    printf(n);
    Should say
    Code:
    printf("%d",n);
    if what you want to print is the so-called array dimension.

    If you can explain more about you assignment I could help, but the only way I know to do it is by using dynamic memory allocation (i.e. malloc). For what I understand is being able to create an array of any size, entering the size by the user. In general should be something like:
    Code:
    #include<malloc.h>
    int main(){
    int n;
    int *key;
    printf("How many keys? ");
    getchar();
    n=getchar();
    key = (int*) malloc(n *sizeof(int));
    for(i=0;i<n;i++)
    {
    key[i] = i;
    printf("%d ", key[i]);
    }
    printf("\n");
    return 0;
    }
    This program should create an array of size n and fill it with the first n naturals (starting at 0).

    Hope this help.

    I tried out the code you gave me, but it shot out
    |10|error: 'for' loop initial declarations are only allowed in C99 mode|
    |10|note: use option -std=c99 or -std=gnu99 to compile your code|
    ||=== Build finished: 1 errors, 0 warnings ===|


  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    Quote Originally Posted by FloridaJo View Post
    Not true. Any compiler with C99 specs can create a variable length array or VLA.

    What's the first part of the question? It seems bit off.
    The question is verbatim from the homework. :/

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by striderx240 View Post
    I tried out the code you gave me, but it shot out
    |10|error: 'for' loop initial declarations are only allowed in C99 mode|
    |10|note: use option -std=c99 or -std=gnu99 to compile your code|
    ||=== Build finished: 1 errors, 0 warnings ===|
    Compile as C99 mode as described then. You will need C99 mode for VLAs anyway.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    Quote Originally Posted by quzah View Post
    Compile as C99 mode as described then. You will need C99 mode for VLAs anyway.


    Quzah.
    Sorry, how would I go about doing that?

  13. #13
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    Sorry, how would I go about doing that?
    I believe that would be right in your error message???
    note: use option -std=c99 or -std=gnu99 to compile your code

  14. #14
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    Never mind, figured it out, thanks anyway.

  15. #15
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    Quote Originally Posted by MartaC View Post
    As far as I know in C you cannot create an array of a lenght an integer read either on a file or introduced by the user, so the instruction
    Code:
    int key[n];
    should give you compilation problems.

    Another command line that should give you problems is
    Code:
    printf(n);
    Should say
    Code:
    printf("%d",n);
    if what you want to print is the so-called array dimension.

    If you can explain more about you assignment I could help, but the only way I know to do it is by using dynamic memory allocation (i.e. malloc). For what I understand is being able to create an array of any size, entering the size by the user. In general should be something like:
    Code:
    #include<malloc.h>
    int main(){
    int n;
    int *key;
    printf("How many keys? ");
    getchar();
    n=getchar();
    key = (int*) malloc(n *sizeof(int));
    for(i=0;i<n;i++)
    {
    key[i] = i;
    printf("%d ", key[i]);
    }
    printf("\n");
    return 0;
    }
    This program should create an array of size n and fill it with the first n naturals (starting at 0).

    Hope this help.
    So I ran this program and It does not seem to work, If I entered '2', it would print out 10 arrays and then when I enter '10' it prints out 48 arrays. Why is this happening?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help beginner with program please
    By bubbles56 in forum C Programming
    Replies: 1
    Last Post: 03-23-2011, 10:37 PM
  2. Beginner, cant get my program to run :-(
    By leahknowles in forum C Programming
    Replies: 5
    Last Post: 03-23-2011, 03:44 PM
  3. Beginner C++ Help. My very first program.
    By Jechob in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2010, 01:34 PM
  4. Beginner C++ Program Help
    By jensklemp in forum C++ Programming
    Replies: 4
    Last Post: 01-28-2010, 03:36 PM
  5. Please Help Me with this beginner C++ Program!
    By ClearSights in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2008, 10:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21