Page 1 of 2

C++ Assignment

Posted: Mon Sep 24, 2007 1:19 pm
by vinyard
I'm trying to figure this out. The assignments are 3.1 & 3.2.

3.1
Write a Short program that asks for your height in integer inches and converts your height to feet & inches.

- Use underscore character to indicate where to type the response.
- Use const symbolic constant to represent the conversion factor.

This what I have for assignment 3.1:

Code: Select all

//Assignment 3.1
#include <iostream>
#include <cstdlib>
using std::cout;
using std::cin;
using std::endl;

int inchesToFeet(int i)
{
    int f = i/12;
    return f;
}

int getRemainder(int i)
{
    int r = i % 12;
    return r;
}

int main()
{
    int heightInches;
    int heightFeet;
    int heightRemainInches;

    cout << "Enter your height in inches:";
    cin >> heightInches;
    heightFeet = inchesToFeet(heightInches);
    heightRemainInches = getRemainder(heightInches);

    cout << "You are " << heightFeet << " feet & " << heightRemainInches << " inches tall." << endl;
    system("PAUSE");
    return 0;
}
I haven't gotten to the rest of it. I need to test it first too. I'll do 3.2 as soon as I get this one done.

Assignment 3.2:

Write a short program that asks for your height in feet & inches and your weight in pounds. (Use three variables to store the information.) Have the program report body mass index (BMI). To calculate the BMI, first convert your height in feet & inches to your height in inches (1 foot = 12 inches). Then, convert your height in inches to your height in meters by multiplying by 0.0254. Then, convert your weight in pounds into your mass in kilograms by dividing by 2.2. Finally, compute your BMI by dividing your mass in kilograms by the square of your weight in meters. Use symbolic constants to represent the various conversion factors.

Code: Select all

//Assignment 3.2
#include <iostream>
#include <cstdlib>
using std::cout;
using std::cin;
using std::endl;

int feetToInches(int i)
{
	 double r = i * 12;
	 return r;
}

double inchesToMeters(int i)
{
	double m = i * 0.0254;
	return m;
}

double poundsToKilogramsMass(int p)
{
	double k = p/2.2;
	return k;
}

double getBMI(double weightK,double heightM)
{
	double hSquared = heightM * heightM;
	double r = weightK/hSquared;
	return r;
}

int main()
{
	int heightInches;
	int heightFeet;
	int weight;

	cout << "Enter your height in feet & inches."
		<< endl
		<< "Feet: ";
	cin >> heightFeet;
	cout << "Inches: ";
	cin >> heightInches;
	cout << "Enter your weight in pounds:";
	cin >> weight;

	int feetConv = feetToInches(heightFeet);
	int heightInchesTotal = feetConv + heightInches;
	double heightMeters = inchesToMeters(heightInchesTotal);
	double kiloMass = poundsToKilogramsMass(weight);
	double BMI = getBMI(kiloMass,heightMeters);

	cout << "Your Body Mass Index (BMI) is " << BMI << endl;
	system("PAUSE");
	return 0;
}
All Done. A bit late though.

Re: C++ Assignment

Posted: Mon Sep 24, 2007 1:57 pm
by skaterbasist
This would be a peace of a cake in Java for me :D

Sorry, don't know the C++ language yet.

.

Re: C++ Assignment

Posted: Mon Sep 24, 2007 2:32 pm
by zero86ea
nice vinyard! skater your such a show off :)

Re: C++ Assignment

Posted: Mon Sep 24, 2007 10:05 pm
by vinyard
Assignment 3.2 Posted.

Re: C++ Assignment

Posted: Mon Sep 24, 2007 11:29 pm
by .Fila
damm i dont know what you just said there vin...this is so confusion to me..but i would like to learn.....

Re: C++ Assignment

Posted: Tue Sep 25, 2007 12:53 am
by skaterbasist
Hey Vinyard, can you post up the problem for 3.2?

For the hell of it, I did 3.1 in Java just to compare... I was bored and needed some practice :D

The program works perfectly (Java):

Code: Select all

//For the hell of it...
   import java.io.*;
   import java.util.*;
    class ForTheHellOfIt
   {
       public static void main (String args [])
      {
         int integerInches;
         int outputFeet;
         int outputInches;
         Scanner keyboard = new Scanner (System.in);
         System.out.print("Enter height in integer inches: ");
         integerInches = keyboard.nextInt();
         while (integerInches<=0)
         {
         //Request a positive height
            System.out.println("Integer Inches must be greater than 0");
            integerInches=keyboard.nextInt();
         }
         if (integerInches>0)
         //Convert Interger Inches to Feet & Inches
         {
            outputFeet= integerInches / 12;
            outputInches = integerInches % 12;
         
         //Display Integer Inches to Feet & Inches
         
            System.out.println("The height integer inches " +integerInches+ " in feet & inches is " +outputFeet+ " Feet and " +outputInches+" Inches");
         }
         
      	//Return to original request
       	System.out.print("Enter the height in integer Inches: ");
 			integerInches=keyboard.nextInt();

         
      
      }
   }

Re: C++ Assignment

Posted: Tue Sep 25, 2007 3:00 am
by Ministry
I can do that, I just don't want to :lol:

~Ministry

Re: C++ Assignment

Posted: Tue Sep 25, 2007 9:03 am
by vinyard
skaterbasist wrote:Hey Vinyard, can you post up the problem for 3.2?
No prob. I'll post it later on today.

Edit: First post updated.

Call funtion from within the function?

Posted: Tue Sep 25, 2007 3:47 pm
by vinyard
skaterbasist wrote:

Code: Select all

         //Return to original request
          System.out.print("Enter the height in integer Inches: ");
         integerInches=keyboard.nextInt();
Couldn't you just loop this by calling the same function over again?

for example:

Code: Select all

#include <iostream>
#include <cstdlib>
using std::cout;
using std::cin;
using std::endl;

int main()
{
    int lenght = 25;
    char username[lenght];  //names longer than 25 characters are not supported in this example.

    cout << "Enter your name: ";
    cin.get(username, lenght).get(); //Get 25 (lenght) characters from line, newline
    cout << "Hello " << username << endl;
    system("PAUSE");

    main();  //We do the loop here;
    return 0;
}
This should work. Don't know if it's different for C++ or Java.

Re: C++ Assignment

Posted: Tue Sep 25, 2007 10:29 pm
by zero86ea
skaterbasist wrote:Hey Vinyard, can you post up the problem for 3.2?

For the hell of it, I did 3.1 in Java just to compare... I was bored and needed some practice :D

The program works perfectly (Java):

Code: Select all

//For the hell of it...
   import java.io.*;
   import java.util.*;
    class ForTheHellOfIt
   {
       public static void main (String args [])
      {
         int integerInches;
         int outputFeet;
         int outputInches;
         Scanner keyboard = new Scanner (System.in);
         System.out.print("Enter height in integer inches: ");
         integerInches = keyboard.nextInt();
         while (integerInches<=0)
         {
         //Request a positive height
            System.out.println("Integer Inches must be greater than 0");
            integerInches=keyboard.nextInt();
         }
         if (integerInches>0)
         //Convert Interger Inches to Feet & Inches
         {
            outputFeet= integerInches / 12;
            outputInches = integerInches % 12;
         
         //Display Integer Inches to Feet & Inches
         
            System.out.println("The height integer inches " +integerInches+ " in feet & inches is " +outputFeet+ " Feet and " +outputInches+" Inches");
         }
         
      	//Return to original request
       	System.out.print("Enter the height in integer Inches: ");
 			integerInches=keyboard.nextInt();

         
      
      }
   }
not bad for a rookie :D