Thread: Need some help with these two questions

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    3

    Need some help with these two questions

    1) The program should output the following: 2 3 4 1
    But output this instead: 3 4 -1806417064

    The
    Code:
     :
        #include <stdio.h>
    void swap(int *a, int *b) {
    int tmp = a;
    *a = *b;
    *b = tmp;
    }
    int main() {
    int array[] = {1, 2, 3, 4};
    int i;
    for (i = 1; i < sizeof(array) / sizeof(int) - 1; i++) {
    swap( &array[i], array + i + 1);
    printf("%d ", array[i]);
    }
    printf("%d\n", array[i]);
    return 0;
    }

    2) The program should output the following: *H*I*
    But output this instead: .U>u<.U>s<.i<>s<

    The
    Code:
     :
    #include <stdio.h>
    int main() {
    char *orders = "suhsuis";
    for (int i = 0, j = 0 ; i < 10; i++, j++) {
    switch (orders[i]) {
    case 's':
    printf(".");
    case 'u':
    printf("%c", orders[++i] - 'a' + 'A');
    break;
    default:
    printf(">%c<", orders[j]);
    }
    }
    printf("\n");
    return 0;
    Can anyone please help me fix these codes?
    Last edited by darrenarvin; 12-07-2017 at 04:18 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    For the second one re-write as a while or do/while loop.

    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

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    3
    So I have to just change the 'for' in line 5 to a while loop?
    I'm really new to programming so I'm not very sure how to do it.

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    from this,
    Code:
    (i = 1; i < sizeof(array) / sizeof(int) - 1; i++)
    to this,
    Code:
    // either here
    size_t i = 0;
    ( ; i < sizeof(array) / sizeof(array[0]); i++)
    //or
    (size_t i = 0; i < sizeof(array) / sizeof(array[0]); i++)
    should be a good thing too.

    run this and take a good look at all of your output to see where your 1 went to, then try to figure out what to do with lastOne to get your 1 back into your array before printing it out.
    Code:
        #include <stdio.h>
    int swap(int *a, int *b) {
        printf("one: a %d, b %d\n", *a,*b);
    int tmp = *a;
    printf("!:: a %d, b %d tmp %d\n", *a,*b, tmp);
    *a = *b;
    *b = tmp;
    
    printf("a %d, b %d tmp %d\n", *a,*b, tmp);
    return tmp;
    }
     
    int main() {
    int array[] = {1, 2, 3, 4};
    int lastOne = 0;
    
    for (size_t i = 0; i < sizeof(array) / sizeof(array[0]); i++) 
    printf("i=%ld %d ",i,    array[i]);
    printf("\n");
    size_t i = 0;
    for (; i < sizeof(array) / sizeof(array[0]); i++) {
    
     lastOne = swap( &array[i], &array[i + 1]); 
    printf("i==%ld lastOne %d\n", i,lastOne);
    
    }
    printf("outside loop\nlastOne %d i= %ld\n",lastOne, i);
     
     
    for (size_t i = 0; i < sizeof(array) / sizeof(array[0]); i++) 
    printf("%d ",    array[i]);
        
    printf("\n");
    return 0;
    }
    for your 2dn one you want something like this?
    Code:
    *U*U*à
    look at your switch and what you're really doing.
    Last edited by userxbw; 12-07-2017 at 05:32 PM.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by userxbw View Post
    Code:
    size_t i = 0;
    
    for (; i < sizeof(array) / sizeof(array[0]); i++) {
    You do this (or something effectively the same) in nearly every program you write and I find it extremely annoying.
    What annoys me mostly is that you seem to have a compulsion to initialise variables when you declare them. At the least it's annoying, at the most it's a waste of resources if the variable doesn't need initialisation (you do it anyway... why?)
    The above should be written in the normal way:
    Code:
    size_t i;
    
    for (i = 0; i < sizeof(array) / sizeof(array[0]); i++) {
    and not how you did it and certainly not like the way you do it other times... :
    Code:
    size_t i = 0;
    
    for (i = 0; i < sizeof(array) / sizeof(array[0]); i++) {

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Hodor View Post
    You do this (or something effectively the same) in nearly every program you write and I find it extremely annoying.
    What annoys me mostly is that you seem to have a compulsion to initialize variables when you declare them. At the least it's annoying, at the most it's a waste of resources if the variable doesn't need initialization (you do it anyway... why?)
    The above should be written in the normal way:
    Code:
    size_t i;
    
    for (i = 0; i < sizeof(array) / sizeof(array[0]); i++) {
    and not how you did it and certainly not like the way you do it other times... :
    Code:
    size_t i = 0;
    
    for (i = 0; i < sizeof(array) / sizeof(array[0]); i++) {
    that is your idea of normal, what is your basis for comparison?

    because you have no idea why the for loop is written like this,

    Code:
    size_t i = 0;
     
    for (; i < sizeof(array) / sizeof(array[0]); i++) {



    so you get extremely annoyed at me when you should be extremely annoyed at your own ignorance, and / or for having a ridge personalty trait that does not allow you to have that flexibility so you become annoyed whenever you see something that strays from what you decided to believe is a norm then project that feeling onto that other instead.

    because that is a valid way to write a for loop.
    1. the first part of the for loop is for what?
    2. the second part of the for loop is for what?
    3. the third part of the for loop is for what?

    it is not my fault you do not fully understand how to use a for loop in all of the ways it can be used.
    this is perfectly valid as well.

    Code:
    int i = 0;
    for ( ; i < 10;   )
    {
         i += 2;
          printf("%d\n", i)
    }
    What annoys me mostly is that you seem to have a compulsion to initialize variables when you declare them.
    like that is a bad thing to do, your argument is not even valid. Just because you have issues with me initializing my variables when I declare them is your problem, not mine.

    Variables can be initialized (assigned an initial value) in their declaration. The initializer consists of an equal sign followed by a constant expression as follows −
    this is just one source.
    C Variables

    sorry you're so ridged
    Last edited by userxbw; 12-07-2017 at 06:21 PM.

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by userxbw View Post
    so you get extremely annoyed at me when you should be extremely annoyed at your own ignorance, and / or for having a ridge personalty trait that does not allow you to have that flexibility so you become annoyed whenever you see something that strays from what you decided to believe is a norm then projected it onto that other instead.
    That's correct

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Hodor View Post
    That's correct
    then you need to go seek professional help for your anxiety issues to hopefully learn how to deal with your abnormal personalty traits better instead of projecting them on to me and others.
    Last edited by userxbw; 12-07-2017 at 06:27 PM.

  9. #9
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by userxbw View Post
    then you need to go seek professional help for your anxiety issues to hopefully learn how to deal with your abnormal personalty traits better instead of projecting them on to me and others.
    Ok, I'll look into it.

    I wasn't annoyed at the construction of your loops by the way. I am annoyed at the way you initialise variables when not needed (although in the above case it is needed because of the abnormal for loop)

  10. #10
    Registered User
    Join Date
    Dec 2017
    Posts
    3
    Quote Originally Posted by userxbw View Post
    from this,
    Code:
    (i = 1; i < sizeof(array) / sizeof(int) - 1; i++)
    to this,
    Code:
    // either here
    size_t i = 0;
    ( ; i < sizeof(array) / sizeof(array[0]); i++)
    //or
    (size_t i = 0; i < sizeof(array) / sizeof(array[0]); i++)
    should be a good thing too.

    run this and take a good look at all of your output to see where your 1 went to, then try to figure out what to do with lastOne to get your 1 back into your array before printing it out.
    Code:
        #include <stdio.h>
    int swap(int *a, int *b) {
        printf("one: a %d, b %d\n", *a,*b);
    int tmp = *a;
    printf("!:: a %d, b %d tmp %d\n", *a,*b, tmp);
    *a = *b;
    *b = tmp;
    
    printf("a %d, b %d tmp %d\n", *a,*b, tmp);
    return tmp;
    }
     
    int main() {
    int array[] = {1, 2, 3, 4};
    int lastOne = 0;
    
    for (size_t i = 0; i < sizeof(array) / sizeof(array[0]); i++) 
    printf("i=%ld %d ",i,    array[i]);
    printf("\n");
    size_t i = 0;
    for (; i < sizeof(array) / sizeof(array[0]); i++) {
    
     lastOne = swap( &array[i], &array[i + 1]); 
    printf("i==%ld lastOne %d\n", i,lastOne);
    
    }
    printf("outside loop\nlastOne %d i= %ld\n",lastOne, i);
     
     
    for (size_t i = 0; i < sizeof(array) / sizeof(array[0]); i++) 
    printf("%d ",    array[i]);
        
    printf("\n");
    return 0;
    }
    for your 2dn one you want something like this?
    Code:
    *U*U*à
    look at your switch and what you're really doing.

    The output for the second one should be *H*I*
    I fixed one of the mistakes in line 8 where the printf was '.'. And I've already changed it to '*'

    PS: Thanks a ton for your help on the first question

  11. #11
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Please don't use that code. Declaring 'i' three times is just silly

  12. #12
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Then comes the rationalization and justification to try and elevate the stress of being wrong. in order to make something right so hopefully that feeling will go away. My suggestion stands.

    Quote Originally Posted by Hodor View Post
    Ok, I'll look into it.

    I wasn't annoyed at the construction of your loops by the way. I am annoyed at the way you initialize variables when not needed (although in the above case it is needed because of the abnormal for loop)
    that is called a pet peeve,
    Pet Peeves – Experience Life


    because your justification is not correct. that loop is not abnormal by no means, that just you trying to deflect. One could even say project your self image onto that for loop.

    it does not really matter over all if one initialized the variable before or within the for loop, as long as it gets initialized before the conditional part of the for loop. That is all that matters pertaining to for loops.
    Last edited by userxbw; 12-07-2017 at 07:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Two Questions In One
    By xmltorrent in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2008, 01:53 PM
  3. A few questions
    By cgod in forum Game Programming
    Replies: 4
    Last Post: 12-16-2004, 12:38 PM
  4. C++ Questions
    By ThA jAcKaL in forum C++ Programming
    Replies: 17
    Last Post: 04-12-2004, 07:42 AM
  5. PM - questions
    By dbaryl in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-09-2002, 12:45 AM

Tags for this Thread