Docsity

CS1372 Fall 2006 Homework 02: Conditionals, For Loops, and Data Types, Assignments of Computer Science

Toggle sidebar
bg1
CS1372 Fall 2006
HW 02 – Conditionals and for loops
The aim of this introductory assignment is for you to familiarize yourself with data types,
conditional statements and for loops.
A basic Visual Studio Solution is accessible to you on the class web site as a zip file. Use this
for the environment for solving the following problems.
The Assignment
The assignment is for you to program one C program that does the following:
1. Demonstrates the behavior of C programs with different data structures
2. Converts a numerical grade as a percentage to a letter grade
3. Wraps the conditional code in a for loop.
The program must:
• Use one file only – name it basic.cpp
• Compile (very important – you get 0 if it doesn’t compile)
• Have a “main” function and have no other functions
• Use
printf to output text to the screen, NOT cout or fprintf.
• Implement all three parts of the assignment within the “main” function
• Return properly
Part 1 – Verify the behavior of C data types
• Create the integer variables i1 and i2 with values 5 and 8 respectively.
• Compute i2/i1, cast it to double and print the result with two decimal places of precision.
• Now cast I2 to double before doing the division, and print the result. Make sure you
understand the behavior. Write a block comment explaining the difference in output.
Part 2 – Convert a random numerical grade to a letter grade
• Use the code fragment below to understand how to generate a random number (an
integer in the range 0 to RAND_MAX.
• Modify the code fragment to generate a grade as a random number between 50 and 100.
pf2

Partial preview of the text

Download CS1372 Fall 2006 Homework 02: Conditionals, For Loops, and Data Types and more Assignments Computer Science in PDF only on Docsity!

CS1372 Fall 2006

HW 02 – Conditionals and for loops

The aim of this introductory assignment is for you to familiarize yourself with data types, conditional statements and for loops.

A basic Visual Studio Solution is accessible to you on the class web site as a zip file. Use this for the environment for solving the following problems.

The Assignment

The assignment is for you to program one C program that does the following:

  1. Demonstrates the behavior of C programs with different data structures
  2. Converts a numerical grade as a percentage to a letter grade
  3. Wraps the conditional code in a for loop.

The program must:

  • Use one file only – name it basic.cpp
  • Compile (very important – you get 0 if it doesn’t compile)
  • Have a “main” function and have no other functions

• Use printf to output text to the screen, NOT cout or fprintf.

  • Implement all three parts of the assignment within the “main” function
  • Return properly

Part 1 – Verify the behavior of C data types

  • Create the integer variables i1 and i2 with values 5 and 8 respectively.
  • Compute i2/i1, cast it to double and print the result with two decimal places of precision.
  • Now cast I2 to double before doing the division, and print the result. Make sure you understand the behavior. Write a block comment explaining the difference in output.

Part 2 – Convert a random numerical grade to a letter grade

  • Use the code fragment below to understand how to generate a random number (an integer in the range 0 to RAND_MAX.
  • Modify the code fragment to generate a grade as a random number between 50 and 100.
  • Write the code to convert this grade to a single character letter grade ‘A’, ‘B’, ‘C’, ‘D’, or ‘F’ and print the result in the form:

Grade: 78; letter grade: C

  • Test this code thoroughly. [Would it be wise to temporarily replace the random generator with a scanf(…) call to check the grade transition values?]

#include <stdio.h> #include <stdlib.h>

int main(void) { srand(0); // do this once at the top of your program int x = rand(); // use this for each random number printf("Random number %d; max is %d\n", x, RAND_MAX); getchar(); return 0; }

Part 3 – Iteration using a for loop

  • Now modify your code to compute the letter grades for 20 random numbers, using the same printout as above. [Hint: wrap the logic in part 2 in a for loop.]

Submission

To submit the assignment, complete the coding (all within basic.cpp) and submit only

basic.cpp to the WebCT Vista2 website (http://webct.gatech.edu/gtvista2).