{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

exercises1 - 2 CONDITIONALS Answers to select problems are...

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

View Full Document Right Arrow Icon
2 CONDITIONALS Answers to select problems are at the end. 1 Statements and printf() 1. printf() has a very large number of format specifiers. There are a number of options that can be used with the format specifiers for altering the way that information is displayed. Run the following program with different values for the format specifiers (sorry about using loops before discussed in class, but it makes this much simpler). #include <stdio.h> int main(void) { int i; double x; for(i = 1; i < 10000; i *= 10) printf("%d\n", i); for(x = 1.234; x < 10000; x *= 10) printf("%lf\n", x); } For the first printf() , some values to try are %2d and %4d . For the second printf() , some values to try are %2lf , %4.2lf , and %8.5lf . 2. Write a program that prompts a user for an integer and then evaluates the following mathematical function for that integer: f(x) = 10 - 18x + 6x^2 2 Conditionals 1. Compare the output from the following programs. Why is it different? #include <stdio.h> int main(void) { int x = 8; 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
2 CONDITIONALS if(x < 6) printf("%d < 6\n", x); else if(x < 8) printf("%d < 8\n", x); else if(x < 10) printf("%d < 10\n", x); else if(x < 12) printf("%d < 12\n", x); else if(x < 14) printf("%d < 14\n", x); } and #include <stdio.h> int main(void) { int x = 8; if(x < 6) printf("%d < 6\n", x); if(x < 8) printf("%d < 8\n", x); if(x < 10) printf("%d < 10\n", x); if(x < 12) printf("%d < 12\n", x); if(x < 14) printf("%d < 14\n", x); } 2. The following program doesn’t print anything. Why? Using only a set of curly braces, change the program such that the second printf() statement will print. #include <stdio.h> int main(void) { int age = 40; if(age > 50) 2
Image of page 2
3 LOOPS if(age < 75) printf("first printf\n"); else printf("second printf\n"); } 3 Loops 1. Rewrite the following program, replacing the for loop with a while loop. #include <stdio.h> int main(void) { int i; for(i = 8; i > 5; i--) printf("%d\n", i); } 2. Rewrite the following program, replacing the do-while loop with a for loop. This may require more than just changing the loop. #include <stdio.h> int main(void) { int x = 5; do { printf("%d\n", x); x++; } while(x < 5); } 3. Write the code to sum the integers 7 through 11 inclusive (i.e., include 7 and 11). Use a loop to do it. 4. You have a program that prompts a user for a positive integer, which is stored in the variable stop . Write the code that will sum the integers from 1 to stop and then print 3
Image of page 3

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

View Full Document Right Arrow Icon
4 FUNCTIONS out the answer. For example, if the user entered 3, the answer printed out would be 6 since 1 + 2 + 3 = 6. 5. You have a program that prompts a user for two integers, which are stored in the variables start and stop . Write the code that will go from start to stop , printing “x is Even” if the number is an even number and “x is Odd” if the number is odd, replacing x with the integer value. 4 Functions 1. Write a function declaration for a function with the name calcAge that will receive a variable of type int (i.e., function parameter) and has a return type of float . 2. The following program makes a function call to a function called multNums . multNums takes two arguments, multiplies them together, and then returns the result. Write the function declaration and function definition for multNums . The return type and variable types that you write for multNums should match those of the variables involved in the function call of the sample program.
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the 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