C++ Assignment

Moderators: vinyard, KreepingMonkey

Message
Author
User avatar
vinyard
Server Admin
Posts: 301
Joined: Fri Jun 15, 2007 7:43 pm
Operating System: Windows Vista Ultimate 32bit
CPU: Intel C2D E8200 [Stock]
Video Card: EVGA GeForce 8800 GT 512MB SC
Sound Card: Onboard
Memory: Corsair Twin2x2048-6400Pro
Motherboard: EVGA 680i AR1 p32
Contact:

C++ Assignment

#1 Post by vinyard » Mon Sep 24, 2007 1:19 pm

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.
Last edited by vinyard on Fri Sep 28, 2007 1:50 am, edited 6 times in total.
Reason: Edited the code. 3.1 Complete. 3.2 Complete. Posted 3.2 Problem.
- vinyard.

skaterbasist
C.S. Server Admin
Posts: 91
Joined: Fri Jul 13, 2007 11:43 pm
Contact:

Re: C++ Assignment

#2 Post by skaterbasist » Mon Sep 24, 2007 1:57 pm

This would be a peace of a cake in Java for me :D

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

.
Image

User avatar
zero86ea
C.S. Server Admin
Posts: 318
Joined: Tue Jun 19, 2007 12:02 am
Contact:

Re: C++ Assignment

#3 Post by zero86ea » Mon Sep 24, 2007 2:32 pm

nice vinyard! skater your such a show off :)
Image

User avatar
vinyard
Server Admin
Posts: 301
Joined: Fri Jun 15, 2007 7:43 pm
Operating System: Windows Vista Ultimate 32bit
CPU: Intel C2D E8200 [Stock]
Video Card: EVGA GeForce 8800 GT 512MB SC
Sound Card: Onboard
Memory: Corsair Twin2x2048-6400Pro
Motherboard: EVGA 680i AR1 p32
Contact:

Re: C++ Assignment

#4 Post by vinyard » Mon Sep 24, 2007 10:05 pm

Assignment 3.2 Posted.
- vinyard.

User avatar
.Fila
KOBK Clan Member
Posts: 283
Joined: Thu Sep 20, 2007 10:25 am
Contact:

Re: C++ Assignment

#5 Post by .Fila » Mon Sep 24, 2007 11:29 pm

damm i dont know what you just said there vin...this is so confusion to me..but i would like to learn.....
Image

Image

skaterbasist
C.S. Server Admin
Posts: 91
Joined: Fri Jul 13, 2007 11:43 pm
Contact:

Re: C++ Assignment

#6 Post by skaterbasist » Tue Sep 25, 2007 12:53 am

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();

         
      
      }
   }
Image

User avatar
Ministry
Posts: 52
Joined: Tue Sep 25, 2007 2:35 am
Contact:

Re: C++ Assignment

#7 Post by Ministry » Tue Sep 25, 2007 3:00 am

I can do that, I just don't want to :lol:

~Ministry
¨Mιиιsтяy¨
I Spend Too Much Time Doing Random Alt + NumPad Codes .Ω
Looking for someone thats good at a photo editor to make a signiture <3

User avatar
vinyard
Server Admin
Posts: 301
Joined: Fri Jun 15, 2007 7:43 pm
Operating System: Windows Vista Ultimate 32bit
CPU: Intel C2D E8200 [Stock]
Video Card: EVGA GeForce 8800 GT 512MB SC
Sound Card: Onboard
Memory: Corsair Twin2x2048-6400Pro
Motherboard: EVGA 680i AR1 p32
Contact:

Re: C++ Assignment

#8 Post by vinyard » Tue Sep 25, 2007 9:03 am

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.
- vinyard.

User avatar
vinyard
Server Admin
Posts: 301
Joined: Fri Jun 15, 2007 7:43 pm
Operating System: Windows Vista Ultimate 32bit
CPU: Intel C2D E8200 [Stock]
Video Card: EVGA GeForce 8800 GT 512MB SC
Sound Card: Onboard
Memory: Corsair Twin2x2048-6400Pro
Motherboard: EVGA 680i AR1 p32
Contact:

Call funtion from within the function?

#9 Post by vinyard » Tue Sep 25, 2007 3:47 pm

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.
Last edited by vinyard on Sat Sep 29, 2007 2:57 am, edited 3 times in total.
Reason: Tested code, it works.
- vinyard.

User avatar
zero86ea
C.S. Server Admin
Posts: 318
Joined: Tue Jun 19, 2007 12:02 am
Contact:

Re: C++ Assignment

#10 Post by zero86ea » Tue Sep 25, 2007 10:29 pm

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
Image

Post Reply