for loop and while loop

This is a discussion on for loop and while loop within the C++ Programming forums, part of the General Programming Boards category; I need help for this code i tried but it could give me what i want what does the following ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    8

    Thumbs up for loop and while loop

    I need help for this code
    i tried but it could give me what i want

    what does the following function do?

    int fx(char *s) {
    int i;
    while (*s!= '\0') {
    s++;
    i++;
    }
    rturn
    }

    what is wrong with this code of mine?

    int a[10], i, *p;
    *p=10;
    for(i=10; i>0; i--) a[i] = i*i;
    p = a;
    printf(%dn\,*(p+4));

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Neither has code tags - see the intro threads, edit your post.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    8
    what is mean by code tag?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    You write this [code]int main(void)[/code] and then it looks like this
    Code:
    int main(void)

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    8
    I need help for this code
    i tried but it could give me what i want

    what does the following function do?

    int main(void)
    int fx(char *s) {
    int i;
    while (*s!= '\0') {
    s++;
    i++;
    }
    rturn
    }

    what is wrong with this code of mine?

    int a[10], i, *p;
    *p=10;
    for(i=10; i>0; i--) a[i] = i*i;
    p = a;
    printf(%dn\,*(p+4));

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Close :P. Now make sure the code is WITHIN the code tags, in stead of before it. Also, since you have two piece of code, you need two pairs of code tags.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    what does the following function do?
    Nothing. It doesn't even compile.

    what is wrong with this code of mine?
    'p' is uninitialized - it has to point to an actual variable in memory (heap or stack) before it can be used, naturally. Moreover, if it can't be assigned to one at the point of initialization, then set it to NULL.



    ITSA
    Socket Library!

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Gabi View Post
    I need help for this code
    i tried but it could give me what i want

    what does the following function do?
    Code:
    int main(void) //what is this line doing here??
    int  fx(char *s) { //this is the first line of a function which returns an int
    int i; //this creates an integer (hence 'int') but you must initialize it to 0 by adding "= 0" //before the semicolon
    while (*s!= '\0') { //this is the first line of a while loop (and says loop while whatever is //being pointed at by the char pointer 's' does not equal a null-terminating character
    s++; //this increments the 's' pointer to the next char location in memory
    i++; //this increments i
    }
    rturn //this is supposed to be "return i;" without the quotes
    }
    what is wrong with this code of mine?
    Code:
    int a[10], i, *p; //what is this????
    //Maybe you were trying to do something like this:
    //int a[10]; //int array of 10 elements (0 -9)
    //int i = 0; //integer initialized to 0
    //int *p = &i; //get a pointer to 'i'
    *p=10; //dereference the pointer and assign 'i' to 10
    for(i=10; i>0; i--) a[i] = i*i; //this is WRONG...
    //change to:
    //for (i = 9; i > 0; i--) a[i] = i*i; //now you're not trying to access an element of the array
    //which doesn't exist
    p = a; //assign the pointer the address of the array
    printf(%dn\,*(p+4)); //i don't think this is correct...
    Read my comments.

Popular pages Recent additions subscribe to a feed

Tags for this Thread


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