{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

quiz2_answers - else printf"%d is prime\n" n 3[35 p...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Programming Using C Quiz 2 Name: 1. [35 p ] Given the following declarations int i=0; char c=’a’; double a[3]={1.0, 1.0, 1.0}; specify whether the following C expressions are syntactically legal, and if they are, their type and value. Consider each expression separately. For character type expression, you can give the character literal or the numerical value, whichever is more convenient. (a) [7 p ] c+=a[i] Answer: legal / char / ’b’ (b) [7 p ] a[3] Answer: legal / double / undefined (c) [7 p ] a[i++]=0 Answer: legal / double / 0.0 (d) [7 p ] a[i]++=0 Answer: illegal (e) [7 p ] (char)++a[i] Answer: legal / char / 2 2. [30 p ] In class we discussed the following code for determining if the number n is prime. Write equivalent code without using a break statement. for (d = 2; d < n; d++) if(n % d == 0) break; if (d < n) printf("%d is divisible by %d\n", n, d); else printf("%d is prime\n", n); 1
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Answer: for (d = 2; d < n && n % d != 0; d++); if (d < n) printf("%d is divisible by %d\n", n, d);
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: else printf("%d is prime\n", n); 3. [35 p ] Write a program that converts a number from decimal to binary. Recall that to convert a decimal number to any base we repeatedly divide the number to the base, replacing the number with the quotient each time, until the quotient is 0. To get the result we read the remainders in reversed order. Hint: use an array to store the remainders. To minimize memory usage you may want to use an array of chars. You may assume that the binary number will not be longer that 100 digits. Answer: #include <stdio.h> #define BASE 2 #define MAX_DIGITS 100 int main() { char bin[MAX_DIGITS]; int i=0, k, n; printf("Enter decimal number "); scanf("%d", &n); while (n > 0) { bin[i] = n % BASE; n = n / BASE; i++; } printf("Number in binary: "); for (k = i - 1; k >= 0; k--) printf("%d", (int)bin[k]); printf("\n"); return 0; } 2...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern