r/programmingrequests • • Mar 02 '20

solved C program If Else program that calculates meters 3 places in each direction (from meter up to kilo or milimeter)

I want it to calculate what a meter converts to in a if else system. So I would put in 10 or 70352 meters and it calculates up to kilo so 3 places up (deka hecto then kilometer) or downward up to millimeter. I dont know how to do both math and a if else statement. the program would prompt you to enter a meter value and it would output it in the correct form (10 meters = .1 kilometers)

0 Upvotes

14 comments sorted by

View all comments

2

u/serg06 Mar 02 '20

There's some information missing. It sounds like you don't understand the requirements yourself.

Have you heard of Rubber Ducky Debugging? It's when you explain a problem in detail to a rubber ducky, and through that process you understand the problem well enough to solve it yourself.

0

u/[deleted] Mar 02 '20

Like what?

1

u/serg06 Mar 02 '20

For example,

  • Do you want the user to be able to input decimals or only integers?
  • Do you actually need to calculate and store a decimal result before printing it (e.g. 10 meters = 0.1 kilometers), or can you convert the meters variable directly to a string?
  • How do you decide whether to convert to kilometers or millimeters?

1

u/[deleted] Mar 02 '20
  1. Yes for decimals
  2. you can convert directly
  3. Whatever the number is closest to so if I put in 1000 it will display 1 kilometers but if I put in 900 it will downgrade to 90 decameters and down again once it goes below one decameter and if I go below 1 meter like .9 then it will switch to 9 decimeters

1

u/serg06 Mar 02 '20
  • Why would it need to downgrade multiple times (meter -> decameter -> whatever -> millimeter) instead of doing it just once (meter -> millimeter)?
  • Is it allowed to output decameters? Or only km/mm?
  • How do you decide if it's closer to mm or km?
  • Are you allowed to output meters?

0

u/[deleted] Mar 02 '20

The assignment requires me to do everything between kilo and milimeter and it decides if its closer to km or mm if it has a decimal in it becuase a .9 will always be lower then a meter so it chooses lower meter incramints and if I were to put in anything from one meter to ten it would stay in meters

1

u/[deleted] Mar 02 '20

So a good way to find out would be

1-9 meter

10-99 decameter

100-999 hectometer

1000-infiant would be kilo

3

u/serg06 Mar 02 '20
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
	// make sure they gave value argument
	if (argc != 2) {
		printf("wrong number of arguments!\n");
		exit(-1);
	}

	// get value
	double val = atof(argv[1]);

	/* SMALLER THAN METER */

	// millimeters
	if (val < 0.01) {
		printf("%.2f mm\n", val * 1000);
	}
	// centimeters
	else if (val < 0.1) {
		printf("%.2f cm\n", val * 100);
	}
	// decimeters
	else if (val < 1) {
		printf("%.2f dm\n", val * 10);
	}

	/* BIGGER THAN METER */

	// kilometers
	else if (val > 1000) {
		printf("%.2f km\n", val / 1000);
	}
	// hectometers
	else if (val > 100) {
		printf("%.2f hm\n", val / 100);
	}
	// dekameters
	else if (val > 10) {
		printf("%.2f dam\n", val / 10);
	}

	/* NEITHER */

	else {
		printf("%.2f m\n", val);
	}
}

1

u/[deleted] Mar 02 '20

It outputs "Wrong number of arguments". Also thank you for helping me get this far.

2

u/serg06 Mar 02 '20

It does that because I assumed you wanted to use the program like this:

$ ./temp7 0.005
5.00 mm
$ ./temp7 1350
1.35 km
→ More replies