C++ Assignment

1235»

Comments

  • edited March 2007
    Okay after messing around a little got the precision right.

    [php]/*
    __________________________________________________________________
    Programmer : Christopher La
    Project : Ch. 3.9
    Class : CS2
    Due : 3/22/07
    Description : Write a program that takes a loan amount and
    interest rate as input and then outputs the
    monthly payments and balance of the loan until the
    loan is paid off.
    ___________________________________________________________________
    */
    #include <iostream.h>
    #include <stdlib.h>
    #include <iomanip.h>

    int main()
    {
    int loan, ans, fixed;
    double interest, payment, paid_in_interest;

    do
    {
    cout << "Enter loan (rounded to whole dollars): $";
    cin >> loan;

    cout << "Enter interest rate: ";
    cin >> interest;
    payment = (loan * (0.05));

    cout << "It will take a monthly payment of $ " << payment << " to pay off\n"
    << "the loan." << endl;
    paid_in_interest = ((interest/100) * loan) / 12;

    cout.setf(ios::fixed);
    cout.precision(2);

    cout << "Paid interest of $ " << paid_in_interest << " for first month."
    << endl;

    cout << "\nTry again? (y or n): ";
    cin >> ans;

    }while (ans == 'Y' || ans == 'y');

    system("PAUSE");
    return 0;
    } [/php]

    But same goes for the post above, not too sure how to continue on the problem after this point.
  • edited March 2007
    Thought I would just post up the rest of my problems incase it makes things easier... dunno..

    7. Write a program that accepts a year written as a four-digit Arabic numeral and outputs the year written in Roman numerals. Important Roman numerals are V for 5, X for 10, L for 50, C for 100, D for 500, and M for 1,000. Recall that some numbers are formed by using a kind of subtraction of one Roman “digit”; for example, IV is 4 produced as C minus I, XL is 40, CM is 900, and so on. A few sample years: MCM is 1900, MCML is 1950, MCMLX is 1960, MCMXL is 1940, MCMLXXXIX is 1989. Assume year is between 1000 and 3000. Your program should include a loop that lets the user repeat this calculation until the user says she or he is done.
    [php]/*
    __________________________________________________________________
    Programmer : Christopher La
    Project : Ch. 3.7
    Class : CS2
    Due : 3/22/07
    Description : Write a program that accepts a year written as a
    four-digit Arabic numeral and outputs the year
    written in Roman numerals. Assume year is between
    1000 and 3000. Your program should include a loop
    that lets the user repeat this calculation until
    the user says she or he is done.
    ___________________________________________________________________
    */
    #include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    int ans, year, V=5, X=10, L=50, C=100, D=500, M=1000, Roman;

    do
    {
    cout << "Enter a year between 1000 and 3000: ";
    cin >> year;
    Roman = V;

    cout << "\nThe year you entered in Roman numerals is: " << Roman;



    }while (ans == 'Y' || ans == 'y');
    cout << "\nTry again? (y or n): ";
    cin >> ans;

    system("PAUSE");
    return 0;
    }[/php]



    ---
    12. An approximate value of pi can be calculated using the series given below:
    Pi = 4 [ 1 – 1/3 + 1/5 – 1/7 + 1/9 … + ((-1)<sup>n</sup>)/(2n+1) ]
    Write a C++ program to calculate the approximate value of pi using this series. The program takes an input n that determines the number of terms in the approximation of the value of pi and outputs the approximation. Include a loop that allows the user to repeat this calculation for new values n until the user says she or he wants to end the program.
  • airbornflghtairbornflght Houston, TX Icrontian
    edited March 2007
    Well, as for the Pi, that is a fairly simple program. I have actually just recently coded something similar in Java, so I will post that and you can take a look at how I did it. Java is close to C++ so you should be able to read it.

    [php]
    import java.util.Scanner;
    public class pi{
    public static void main(String[] args){
    double denom, num, pi;
    int reps;

    Scanner sc = new Scanner(System.in);

    do{

    System.out.print("How many repetitions (0 to quit): ");
    reps=sc.nextInt();
    pi=1;
    if(reps!=0)
    {
    for(int i=1;i<=reps;i++)
    {
    num=Math.pow(-1,i);
    denom=2*i+1;
    pi+=(num/denom);
    }
    pi*=4;
    System.out.println("Calculated value of Pi: " + pi+"\n");
    }
    }while(reps!=0);
    }
    }
    [/php]


    I also just coded the roman numeral program in java. But I'm not going to post the code until I get some more out of you for that. Its not too hard either, too me about 25 minutes. let me give you a hint. You only need two variables, String roman and int arabic. though I used an extra arabic for output but its not needed. Another hint, your going to use 7 while loops and 6 if statements.
  • edited March 2007
    For the Roman Numeral program I found an example and attempted to manipulate it in terms of learn off of it but there are some terms that I am not familar with...

    Original Code:
    [php]/* HEADER FILES */
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>

    /* The char "c1" and "c2" are initialized in order that not affect the thousands */
    char *a2roman (int value, char *c1, char *c2, char *c3);

    int main (void)
    {
    int arabicalNumeral = 1; /* Initialization for read althought one time */
    int result; /* "result" store the position value */
    char roman[15] = ""; /* "roman" contain the roman-numeral string */

    /* This cicle allow to continue if "arabicalNumber" to be in the range */
    do
    {
    /* Clear the screen */
    clrscr();

    /* Print a message to user */
    printf ("Enter a integer in 1 to 3000 range of integers: \n\t");

    /* Read the value */
    scanf ("%d", &arabicalNumeral);
    }
    while ((arabicalNumeral < 1) || (arabicalNumeral > 3000));

    /* Obtain the value of thousands */
    if ((arabicalNumeral <= 3000) && (arabicalNumeral >= 1000))
    {
    result = arabicalNumeral / 1000;
    strcat (roman, a2roman(result, "M", " ", " "));
    arabicalNumeral -= (result * 1000);
    }

    /* Obtain the value of hundreds */
    if ((arabicalNumeral < 1000) && (arabicalNumeral >= 100))
    {
    result = arabicalNumeral / 100;
    strcat (roman, a2roman(result, "C", "D", "M"));
    arabicalNumeral -= (result * 100);
    }

    /* Obtain the value of tens */
    if ((arabicalNumeral < 100) && (arabicalNumeral >= 10))
    {
    result = arabicalNumeral / 10;
    strcat (roman, a2roman(result, "X", "L", "C"));
    arabicalNumeral -= (result * 10);
    }

    /* Obtain the value of units */
    if ((arabicalNumeral < 10) && (arabicalNumeral >= 1))
    {
    strcat (roman, a2roman(arabicalNumeral, "I", "V", "X"));
    }

    /* Display the Roman numeral */
    printf ("The Roman numeral is: \n\t%s\n\n", roman);
    printf ("\t\t ...Press any key to exit.");
    getch();

    /* Succesfull return */
    return 0;
    }

    char *a2roman (int value, char *c1, char *c2, char *c3)
    {
    int i; /* "i" is the index of the iteration */
    char rRoman[15] = "";

    /* If value = 1, 2, 3 */
    if ((value >= 1) && (value <= 3))
    {
    for (i = 0; i < value; i++)
    strcat (rRoman, c1);
    }

    /* If value = 5, 6, 7, 8 */
    if ((value >= 5) && (value <= 8))
    {
    strcat (rRoman, c2);

    for (i = 0; i < (value - 5); i++)
    strcat (rRoman, c1);
    }

    /* If value = 4 */
    if (value == 4)
    {
    strcat (rRoman, c1);
    strcat (rRoman, c2);
    }

    /* If value = 9 */
    if (value == 9)
    {
    strcat (rRoman, c1);
    strcat (rRoman, c3);
    }

    return (rRoman);
    }[/php]

    What I don't understand from that is 'stdio.h' if I change that to 'iostream.h' that just changes the printf/scanf to cout/cin right? Also not too sure how the statement, 'strcat (roman, a2roman(result, "M", " ", " "));' works.. as I am not keen on 'strcat'.

    Lastly... The whole bottom portion...

    So just from what I can get from it.. my manipulated code looks something like this:
    [php]#include <iostream.h>
    #include <conio.h>
    #include <string.h>

    char *a2roman (int value, char *c1, char *c2, char *c3);

    int main (void)
    {
    int arabicNumeral = 1;
    int result;
    char roman[15] = "";

    do
    {
    cout << "Enter a integer in 1000 to 3000 range of integers: ";
    cin >> "%d", &arabicNumeral;
    }
    while ((arabicNumeral < 1) || (arabicNumeral > 3000));

    /* Obtain the value of thousands */
    if ((arabicNumeral <= 3000) && (arabicNumeral >= 1000))
    {
    result = arabicNumeral / 1000;
    strcat (roman, a2roman(result, "M", " ", " "));
    arabicNumeral -= (result * 1000);
    }

    /* Obtain the value of hundreds */
    if ((arabicNumeral < 1000) && (arabicNumeral >= 100))
    {
    result = arabicNumeral / 100;
    strcat (roman, a2roman(result, "C", "D", "M"));
    arabicNumeral -= (result * 100);
    }

    /* Obtain the value of tens */
    if ((arabicNumeral < 100) && (arabicNumeral >= 10))
    {
    result = arabicNumeral / 10;
    strcat (roman, a2roman(result, "X", "L", "C"));
    arabicNumeral -= (result * 10);
    }

    /* Obtain the value of units */
    if ((arabicNumeral < 10) && (arabicNumeral >= 1))
    {
    strcat (roman, a2roman(arabicNumeral, "I", "V", "X"));
    }

    /* Display the Roman numeral */
    cout << "The Roman numeral is: %s", roman;

    return 0;
    }[/php]

    Forgot to mention not too sure what the %d/%s represent either...
  • airbornflghtairbornflght Houston, TX Icrontian
    edited March 2007
    well, I guess you really didn't attempt this but at least you put some effort into it. That code seems overly complex for what it does. look at this:

    [PHP]
    import java.util.Scanner;

    public class roman2{
    public static void main (String[] args){


    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a year in arabic numeral form: ");
    int arabic = sc.nextInt();
    int arabic2=arabic;


    String roman="";
    while (arabic >= 1000)
    {
    arabic -= 1000;
    roman += "M";
    }

    if (arabic >= 900)
    {
    roman += "CM";
    arabic -= 900;
    }

    while (arabic >= 500)
    {
    arabic -= 500;
    roman += "D";
    }

    if (arabic >= 400)
    {
    roman += "CD";
    arabic -= 400;
    }

    while (arabic >= 100)
    {
    arabic -= 100;
    roman += "C";
    }

    if (arabic >= 90)
    {
    roman += "XC";
    arabic -= 90;
    }

    while (arabic >= 50)
    {
    arabic -= 50;
    roman += "L";
    }

    if (arabic >= 40)
    {
    roman += "XL";
    arabic -= 40;
    }

    while (arabic >= 10)
    {
    arabic -= 10;
    roman += "X";
    }

    if (arabic >= 9)
    {
    roman += "IX";
    arabic -= 9;
    }

    while (arabic >= 5)
    {
    arabic -= 5;
    roman += "V";
    }

    if (arabic >= 4)
    {
    roman += "IV";
    arabic -= 4;
    }

    while (arabic >= 1)
    {
    arabic -= 1;
    roman += "I";
    }

    System.out.println("The year " + arabic2 + " in roman numeral form is: " + roman);
    }
    }[/PHP]

    I'm fairly sure that this works in all cases, though I didn't do any sort of extensive testing.
  • edited March 2007
    hmm so if 'converting' that to C++, what would the 'public class' and 'public static' become? and I am sure the 'System.out.print' would be 'cout <<'

    But then not too sure as to these two:
    <code style="white-space: nowrap;"><code><span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 0, 187);">[php]Scanner sc = new Scanner(System.in);

    int arabic = sc.nextInt();
    [/php]
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2007
    Write your own, don't try to adapt his.
  • airbornflghtairbornflght Houston, TX Icrontian
    edited March 2007
    Yeh, write your own. I'm sure there are more elegant ways to do that. I didn't put a whole lot of thought into the design, I just jotted some things down on paper about how the logic of roman numerals worked and started coding. but examine my solution and others, then see if you can think of any improvements that could be made. Then code your solution.
Sign In or Register to comment.