CIS 330 Home Page Last updated 2008/01/07 13:03:25

CIS 330 Assignment 1

Basic C/C++ Programming

Use the web service e-turnin to submit your work electronically. You may turn in revisions of your homework up to the time it is due.
Here is a simple programming guide to get you started.

Each of the following programs should be coded in a single source file. Your solution must work as described in the problem statement. It is also important that your code be readable and well written. You will lose points on the problem if your logic is too convoluted or your code is unnecessarily complex. Coding style is also an important factor in the grading and don't forget to comment your code appropriately.


  1. Write a program that produces a list of the first fifty multiples of three, starting with 3. The list should be printed ten numbers per line, and evenly spaced. Your code can use only one loop and one printf statement. The output must look like:
        3    6    9   12   15   18   21   24   27   30
       33   36   39   42   45   48   51   54   57   60
       63   66   69   72   75   78   81   84   87   90
       93   96   99  102  105  108  111  114  117  120
      123  126  129  132  135  138  141  144  147  150
    


  2. Recode the previous problem using the C++ ostream operator instead of printf.


  3. Write a program named hexToDec that takes an integer in hexadecimal format on the command line and prints the value in decimal format. Your program must use a function called hexStringToInt, that takes the hexadecimal string from the command line and converts it to an integer value which is returned. You can use the following main function. You cannot use sscanf or any istream operator to do the conversion of the input string - you must code the logic to convert from the character string of a hexadecimal representation to an int. However you can use sscanf and printf to check your results.
    // A number in hexadecimal is given on command line
    int main(int argc, char *argv[]) {
        if (argc > 1) {
            int num = hexStringToInt(argv[1]);
            cout << "In decimal, " << argv[1] << " is " << num << endl;
        }
        return 0;
    }
    


  4. Write a program that computes the monthly payment for a loan and prints a schedule of the payments and amount of each payment that is interest. The program must take three command line arguments: The formula for calculating the payment amount for a loan of amount P with montly interest r and n payments is:
    P * r * (1 + r)n / ( (1 + r)n - 1)
    The amount of a monthly payment that would be interest would be the monthly interest rate times the remaining loan amount. The rest would go toward paying down the loan balance.

    Remember there is no exponential operator in C/C++, just as in Java. You can use the pow method of the math library. To use this library, #include math.h in your code, and add the option -lm at the end of the compile command. To convert string arguments to numbers, you can use atof and atoi, or scanf, or iostream operators.

    Output should be formatted as shown in this example. You can use printf or iostream operators for formatting.

    
    % payment 1000 6 12
    Monthly payment for a $1000.00 6.00% loan of 12 monthly payments is $86.07
    Payment Interest  Principal  Balance
    1       $5.00     $81.07     $918.93
    2       $4.59     $81.47     $837.46
    3       $4.19     $81.88     $755.58
    4       $3.78     $82.29     $673.29
    5       $3.37     $82.70     $590.59
    6       $2.95     $83.11     $507.48
    7       $2.54     $83.53     $423.95
    8       $2.12     $83.95     $340.01
    9       $1.70     $84.37     $255.64
    10      $1.28     $84.79     $170.85
    11      $0.85     $85.21     $85.64
    12      $0.43     $85.64     $-0.00
    


  5. Code a program that takes an integer on the command line and prints the number of one bits in the integer. Your program should have a function named countOnes that takes the integer as an argument and returns the number of one bits. Your program should work for negative numbers as well as non-negative values. In the case of negative numbers, the number of one bits will reflect the actual storage representation. For example, on the department server ix, the number of one bits in the value -2 is 31 (which reflects twos complement representations on a 32 bit machine).
  6. Write a program that reads standard input and writes to standard output all of the input, but with C and C++ style comments removed. Your program may only use getchar() and putchar() to do input and output, so input is read one character at a time. For extra points, your program should handle quotation marks correctly, e.g., a line like
    const char *begcmt = "/*";
    
    should appear on output unchanged.