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?
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; }
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.
not exactlyBLUR = BLUR[0]
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
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; }
Okay now please show the memory diagram in subquestion L.
> Okay now please show the memory diagram in subquestion L.
Negative. http://pages.cs.wisc.edu/~cs302/reso..._diagrams.html
Heres my draft solution for subquestion E.
But how can I display the number of letters with even ASCII character values?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); }
That code is horrible. If someone taught you to write that, they did you a disservice.....
any further assistance about my answer to subquestion E?
- There is no such thing as "main()". There is "int main()", and there is "int main(int argc, char *argv[])".
- 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).
- 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.
- %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.
- There's no need to call the function twice -- once inside the print statement should be sufficient.
do I win a cookie ?Code:char Dec(char a){ return 64 + (((63 & a)+ 1) % 26); }
Last edited by abachler; 07-11-2008 at 10:56 AM.
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("%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; }