Jump to content

Algorithm check

Venum
Go to solution Solved by smjpl,

for my input data type i changed it into Character since inputing a number in cube as radius can be referred in computer terminology as characters?

(i.e r³=6³ or 3³ and so on)

 

From my limited knowledge of programming, a character or char (Java) is a single character (i.e. single text character, letter or digit) but it isn't recognised as a number so that wouldn't be correct. If you were to enter the radius as a character, you would then have to convert it to a number data type (int, float, double etc) for use in the algorithm.

 

What I am getting at is more, is the radius a whole number? More often than not when measuring length, we do not measure whole numbers. We tend to measure to a decimal place. If you wanted the algorithm to do this, you can get more acurate results but the disadvantage is that more system memory is used when using floats or doubles as opposed to using an int for whole numbers. So where you can, use the smallest possible data type but you need to know the limitations of it. It would be quite common for someone to enter 6.3 as the radius but if you have r set to an int, the program will crash. So you would need to tell the user what format the radius should be input as if there are such limitations. 

hi guys :)

Im rolling out an algorithm for this simple program.

 

here it is, i want to create a program to compute the volume of a sphere, using the formula V=(4/3)πr³ is equal to 3.1415 aproxx. The r³ is the radius..

 

i would like to know what kind of data type is r³?

thnx :))

Link to comment
Share on other sites

Link to post
Share on other sites

r³ is the radius cubed, it will still have a numerical value, r is the base radius which is a distance, this would need to be a floating point value (float or double) to allow for the decimal places.

Given radius r,

PI = 3.1415

volume = (4/3)*PI*(r*r*r)

Arch Linux on Samsung 840 EVO 120GB: Startup finished in 1.334s (kernel) + 224ms (userspace) = 1.559s | U mad windoze..?

Link to comment
Share on other sites

Link to post
Share on other sites

I am not really sure what you mean. What data type should you set the variable r to in your code? Well that depends on your problem. You can set it to whatever you want, int, float or double. If you are only going to be dealing with whole numbers then make r an integer. If not, use float or double. Doesn't really matter as this sounds like a simple problem with no need for precision or conserving memory. 

 

If you are referring to the actual equation for area of a cube, then r is the radius and the units will be length. The resulting units (volume) will depend on the units you use for the radius. If you measure the radius in mm, then the volume will be calculated as mm³, or if r is measured in cm the volume will be cm³ and so on. 

Rig: i7 2600K @ 4.2GHz, Larkooler Watercooling System, MSI Z68a-gd80-G3, 8GB G.Skill Sniper 1600MHz CL9, Gigabyte GTX 670 Windforce 3x 2GB OC, Samsung 840 250GB, 1TB WD Caviar Blue, Auzentech X-FI Forte 7.1, XFX PRO650W, Silverstone RV02 Monitors: Asus PB278Q, LG W2243S-PF (Gaming / overclocked to 74Hz) Peripherals: Logitech G9x Laser, QPad MK-50, AudioTechnica ATH AD700

Link to comment
Share on other sites

Link to post
Share on other sites

r stands for radius and should be able to contain any posisble radius data.
Radiusses can't be negative so we can exclude that.

What language are you coding this in?

I'd settle for whichever data type excludes negatives and holds an OK amount of data.

Grammar nazis are people too!
Treat your local grammar nazi nicely and he might teach you a thing or two. (Note that I'm Belgian and not a native English speaker.)
Chivalry isn't dead!

Link to comment
Share on other sites

Link to post
Share on other sites

for powers in java you can use a for loop like so

int r = 0;for (int exponent = 0; exponent < r; ++exponent) {//print out current power of 3System.out.println(r);//find next power of 3r=r*3;}
Link to comment
Share on other sites

Link to post
Share on other sites

r stands for radius and should be able to contain any posisble radius data.

Radiusses can't be negative so we can exclude that.

What language are you coding this in?

 

From one grammar nazi advocate to another. The correct plural of radius is radii (but apparently radiuses is also acceptable). ;)

 

I'd settle for whichever data type excludes negatives and holds an OK amount of data.

Or just use any data type and get the absolute value of r.

Rig: i7 2600K @ 4.2GHz, Larkooler Watercooling System, MSI Z68a-gd80-G3, 8GB G.Skill Sniper 1600MHz CL9, Gigabyte GTX 670 Windforce 3x 2GB OC, Samsung 840 250GB, 1TB WD Caviar Blue, Auzentech X-FI Forte 7.1, XFX PRO650W, Silverstone RV02 Monitors: Asus PB278Q, LG W2243S-PF (Gaming / overclocked to 74Hz) Peripherals: Logitech G9x Laser, QPad MK-50, AudioTechnica ATH AD700

Link to comment
Share on other sites

Link to post
Share on other sites

I appreciate the response guys. I would like to clear out im going to make an algorithm->pseudocode->flowchart-> code in that matter.

given the problem what should my analysis be?

what are my:

outputs

inputs

constant/s

 

like ex..

1. Output discription                                                       Data type               Data name

    an integer that represent the volume of the sphere    =integer/real             = total_volume_of_sphere

Link to comment
Share on other sites

Link to post
Share on other sites

Well think about it so.

 

What is always going to be constant (i.e. what number will never change)? What will you have to input into the equation and what will be the result (output) of the algorithm for a given input? 

 

I don't want to give you the answer.

Rig: i7 2600K @ 4.2GHz, Larkooler Watercooling System, MSI Z68a-gd80-G3, 8GB G.Skill Sniper 1600MHz CL9, Gigabyte GTX 670 Windforce 3x 2GB OC, Samsung 840 250GB, 1TB WD Caviar Blue, Auzentech X-FI Forte 7.1, XFX PRO650W, Silverstone RV02 Monitors: Asus PB278Q, LG W2243S-PF (Gaming / overclocked to 74Hz) Peripherals: Logitech G9x Laser, QPad MK-50, AudioTechnica ATH AD700

Link to comment
Share on other sites

Link to post
Share on other sites

here is what i think.

 

OUTPUT:

Output discription                                                       Data type                  

a real value that represent the volume of the sphere    =real

INPUT:

input discription                                                          Data type

an integer representing the value of the radius          =integer

CONSTANTS:

π=3.1415

 

hmm i feel something is wrong in my analysis.

Link to comment
Share on other sites

Link to post
Share on other sites

here is what i think.

 

OUTPUT:

Output discription                                                       Data type                  

a real value that represent the volume of the sphere    =real

INPUT:

input discription                                                          Data type

an integer representing the value of the radius          =integer

CONSTANTS:

π=3.1415

 

hmm i feel something is wrong in my analysis.

 

For the most part yes you are correct. Where you may (but not necessarily) have issues is with your data types.

 

  • Real numbers (double or float) can be negative but volume can't be negative (there are ways around this as stated above but this might not be necessary for your problem, but it can't hurt to have it either).
  • Having your input as an integer, why might you have an issue with having radius set as an integer? (Again I am not saying having the radius as an input is wrong but you must know its benefits/limitations if you are to set it to an integer).

Rig: i7 2600K @ 4.2GHz, Larkooler Watercooling System, MSI Z68a-gd80-G3, 8GB G.Skill Sniper 1600MHz CL9, Gigabyte GTX 670 Windforce 3x 2GB OC, Samsung 840 250GB, 1TB WD Caviar Blue, Auzentech X-FI Forte 7.1, XFX PRO650W, Silverstone RV02 Monitors: Asus PB278Q, LG W2243S-PF (Gaming / overclocked to 74Hz) Peripherals: Logitech G9x Laser, QPad MK-50, AudioTechnica ATH AD700

Link to comment
Share on other sites

Link to post
Share on other sites

Again I might be thinking into the problem too much (I tend to do that), but it would be good to have these sorts of questions in your mind. Extra marks is always a good thing ;)

Rig: i7 2600K @ 4.2GHz, Larkooler Watercooling System, MSI Z68a-gd80-G3, 8GB G.Skill Sniper 1600MHz CL9, Gigabyte GTX 670 Windforce 3x 2GB OC, Samsung 840 250GB, 1TB WD Caviar Blue, Auzentech X-FI Forte 7.1, XFX PRO650W, Silverstone RV02 Monitors: Asus PB278Q, LG W2243S-PF (Gaming / overclocked to 74Hz) Peripherals: Logitech G9x Laser, QPad MK-50, AudioTechnica ATH AD700

Link to comment
Share on other sites

Link to post
Share on other sites

 

For the most part yes you are correct. Where you may (but not necessarily) have issues is with your data types.

 

  • Real numbers (double or float) can be negative but volume can't be negative (there are ways around this as stated above but this might not be necessary for your problem, but it can't hurt to have it either).
  • Having your input as an integer, why might you have an issue with having radius set as an integer? (Again I am not saying having the radius as an input is wrong but you must know its benefits/limitations if you are to set it to an integer).

 

for my input data type i changed it into Character since inputing a number in cube as radius can be referred in computer terminology as characters?

(i.e r³=6³ or 3³ and so on)

Link to comment
Share on other sites

Link to post
Share on other sites

float r = -1, pi = 3.1415, volume;//Get r using whatever input method you prefer/whatever your language supports e.g:std::string input;while(r<0){  std::getline(std::cin, input);  r = std::stoi(input); //yes yes it's unsafe}volume = (4*pi*(r*r*r))/3;

Almost any language has PI predefined, with superior accuracy. So consider using that PI instead, and then round the number to whatever accuracy you require.

Link to comment
Share on other sites

Link to post
Share on other sites

for my input data type i changed it into Character since inputing a number in cube as radius can be referred in computer terminology as characters?

(i.e r³=6³ or 3³ and so on)

 

From my limited knowledge of programming, a character or char (Java) is a single character (i.e. single text character, letter or digit) but it isn't recognised as a number so that wouldn't be correct. If you were to enter the radius as a character, you would then have to convert it to a number data type (int, float, double etc) for use in the algorithm.

 

What I am getting at is more, is the radius a whole number? More often than not when measuring length, we do not measure whole numbers. We tend to measure to a decimal place. If you wanted the algorithm to do this, you can get more acurate results but the disadvantage is that more system memory is used when using floats or doubles as opposed to using an int for whole numbers. So where you can, use the smallest possible data type but you need to know the limitations of it. It would be quite common for someone to enter 6.3 as the radius but if you have r set to an int, the program will crash. So you would need to tell the user what format the radius should be input as if there are such limitations. 

Rig: i7 2600K @ 4.2GHz, Larkooler Watercooling System, MSI Z68a-gd80-G3, 8GB G.Skill Sniper 1600MHz CL9, Gigabyte GTX 670 Windforce 3x 2GB OC, Samsung 840 250GB, 1TB WD Caviar Blue, Auzentech X-FI Forte 7.1, XFX PRO650W, Silverstone RV02 Monitors: Asus PB278Q, LG W2243S-PF (Gaming / overclocked to 74Hz) Peripherals: Logitech G9x Laser, QPad MK-50, AudioTechnica ATH AD700

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×