Thread: Homework help

  1. #16
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by @nthony View Post
    requesting obligatory posting of mac's signature and lock... in that order.
    You rang, sir?

  2. #17
    Registered User
    Join Date
    May 2008
    Posts
    12
    2. Be able to demonstrate and explain programs to your lecturer.

    Even if we create the code for you, do you think you can able to explain it?

  3. #18
    Registered User
    Join Date
    Jul 2008
    Posts
    38

    Smile I dont really understand subquestion L

    Okay I'm done with subquestions A, B, C, D, and I really don't understand what memory diagrams are. I am asked to trace the following C program and write down its output. Can you kindly assist me in this one?

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    char *B1, *B2, *B3, *B4, *B5;
    char BLUR[8] = {'M', 'O', 'R', 'E', 'B', 'L', 'U', 'R'};
    
    main()
    {
    clrscr();
    B5=B4=BLUR;
    ++B4;
    B3=&BLUR[6];
    B2=B5+2;
    B1=&BLUR[7]-3;
    
    printf ("%c\t%c\t%c\t%c\t%c",*B1,*B2,*B3,*B4,*B5);
    
    getch();
    return 0;
    }

  4. #19
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147
    B5=B4=BLUR


    1) B5 and B4 are pointers to characters

    2) BLUR = BLUR[0]

    I think that above two points will help you to calcuate the output.

  5. #20
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    BLUR = BLUR[0]
    not exactly

    Code:
     BLUR == &BLUR[0]
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #21
    Registered User
    Join Date
    Apr 2008
    Posts
    83
    Quote Originally Posted by mkdl750 View Post
    Okay I'm done with subquestions A, B, C, D, and I really don't understand what memory diagrams are. I am asked to trace the following C program and write down its output. Can you kindly assist me in this one?

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    char *B1, *B2, *B3, *B4, *B5;
    char BLUR[8] = {'M', 'O', 'R', 'E', 'B', 'L', 'U', 'R'};
    
    main()
    {
    clrscr();
    B5=B4=BLUR;
    ++B4;
    B3=&BLUR[6];
    B2=B5+2;
    B1=&BLUR[7]-3;
    
    printf ("%c\t%c\t%c\t%c\t%c",*B1,*B2,*B3,*B4,*B5);
    
    getch();
    return 0;
    }

    The output of following program is :- B R U O M

    Code:
    #include <stdio.h>
    #include <conio.h> 
    char *B1, *B2, *B3, *B4, *B5;
    char BLUR[8] = {'M', 'O', 'R', 'E', 'B', 'L', 'U', 'R'};
    
    int main()
    {
    B5=B4=BLUR;//Both Pointers are assigned to 'M' 
    ++B4;  //Now it will point to 'O'
    B3=&BLUR[6]; //'U' has been assigned
    B2=B5+2; //'R' has been assigned
    B1=&BLUR[7]-3; //'B' has been assigned
    
    printf ("%c\t%c\t%c\t%c\t%c",*B1,*B2,*B3,*B4,*B5); //B  R  U  O  M
    
    getch();
    return 0;
    }

  7. #22
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    Okay now please show the memory diagram in subquestion L.

  8. #23
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > Okay now please show the memory diagram in subquestion L.
    Negative. http://pages.cs.wisc.edu/~cs302/reso..._diagrams.html

  9. #24
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    Heres my draft solution for subquestion E.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int CountEvenASCII(char *str);
    
    main()
    {
    char *x[20];
    printf("Enter a string of letters: ");
    scanf("%c",x);
    CountEvenASCII(*x);
    printf("%d",CountEvenASCII(*x));
    getch();
    return 0;
    }
    
    int CountEvenASCII(char *str)
    {
     if(str[0] == '\0')
    	return 0;
     if((int)str[0] % 2 == 0)
    	return 1 + CountEvenASCII(str + 1);
     else
    	return CountEvenASCII(str + 1);
    }
    But how can I display the number of letters with even ASCII character values?

  10. #25
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    That code is horrible. If someone taught you to write that, they did you a disservice.....

  11. #26
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    any further assistance about my answer to subquestion E?

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. There is no such thing as "main()". There is "int main()", and there is "int main(int argc, char *argv[])".
    2. Indentation is something you should look into. Basically: everything inside curly braces should be moved over by the same amount (and the more braces, the further over you go).
    3. A string (which should probably not be called "x") can be either char *x, or char x[20], but not both. The declaration char *x[20] gives you 20 pointers-to-characters; each pointer-to-character could be a string one day, if you malloc'ed memory for those pointers to point to.
    4. %c only reads one character from standard input. %s will read in a string, provided it has no spaces, but needs work to be safe. There is such a thing as fgets.
    5. There's no need to call the function twice -- once inside the print statement should be sufficient.

  13. #28
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Code:
     
    char Dec(char a){
       return 64 + (((63 & a)+ 1) &#37; 26);
       }
    do I win a cookie ?
    Last edited by abachler; 07-11-2008 at 10:56 AM.

  14. #29
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    Can you help me correct this code?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int CountEvenASCII(char *str);
    
    main()
    {
    char *x[20];
    printf("Enter a string of letters: ");
    scanf("&#37;c",x);
    CountEvenASCII(*x);
    printf("%d",CountEvenASCII(*x));
    getch();
    return 0;
    }
    
    int CountEvenASCII(char *str)
    {
     if(str[0] == '\0')
    	return 0;
     if((int)str[0] % 2 == 0)
    	return 1 + CountEvenASCII(str + 1);
     else
    	return CountEvenASCII(str + 1);
    }

  15. #30
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by mkdl750 View Post
    Can you help me correct this code?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int CountEvenASCII(char *str);
    
    main()
    {
    char *x[20];
    printf("Enter a string of letters: ");
    scanf("&#37;c",x);
    CountEvenASCII(*x);
    printf("%d",CountEvenASCII(*x));
    getch();
    return 0;
    }
    
    int CountEvenASCII(char *str)
    {
     if(str[0] == '\0')
    	return 0;
     if((int)str[0] % 2 == 0)
    	return 1 + CountEvenASCII(str + 1);
     else
    	return CountEvenASCII(str + 1);
    }
    Your algorithm is correct but the way you are calling it is wrong.
    This should be done just like the following (check out the changes in CountEvenASCII)
    Code:
    int CountEvenASCII(const char *str) // since you are not making any changes to the string, it's supposed to be a constant string.
    {
     if(str[0] == '\0')
    	return 0;
     if((int)str[0] % 2 == 0)
    	return 1 + CountEvenASCII(str + 1);
     return CountEvenASCII(str + 1); // no 'else' is needed.
    }
    
    int main(void)
    {
        const char *abc = "AB";
        printf("%d", CountEvenASCII(abc));
        return 0;   
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  3. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  4. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  5. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM

Tags for this Thread