# Thread: Idea for final project

1. I suggest using modulus "%" to display the remainder and still do just integer division.

Tim S.

2. Originally Posted by stahta01
I suggest using modulus "%" to display the remainder and still do just integer division.

Tim S.
but % will ONLY give me the remainder, I am trying to input 2 numbers and display their quotient even when the result is not a whole number....

3. use a float a make a cast when you divide

Code:
```float quotient;
.....

quotient = (float)first_Num / sec_Num;
printf("\nThe quotient of %d and %d is %f\n\n", first_Num, sec_Num, quotient);```

to complicate things, did u do function pointers? It's a perfect case to use them, if you have not yet studied them does nothing it's just an advice
Or you can set up your program in a different way, for example
Code:
```switch(choice){
case 1: sum(first_Num, sec_Num); break;
case 2:
...

}```
and write a function for each operation.

4. Originally Posted by root
use a float a make a cast when you divide

Code:
```float quotient;
.....

quotient = (float)first_Num / sec_Num;
printf("\nThe quotient of %d and %d is %f\n\n", first_Num, sec_Num, quotient);```
that is giving me the result of 0.000000 no matter what numbers i enter.

5. Originally Posted by root
did u do function pointers? It's a perfect case to use them, if you have not yet studied them does nothing it's just an advice
yeah we learned about pointers this past week... how would i implement them in this situation?
since a switch statement doesnt allow the use of double do i need to change the whole code to nested if statements instead?

6. This is what you want?
Code:
```#include <stdio.h>
#include <math.h> /* for fmod */

int main(void)
{

float a,b;

scanf("%f",&a);
scanf("%f",&b);

printf("Quotent a/b = %f\n",a/b);
printf("Quotent a mod b = %f\n",fmod(a,b));

return 0;
}```
Comments : Declare numbers as float, read them, divide them and print quotient and remainer(mod).

7. I found the problem, had to declare all my variables as doubles instead of int except for choice. now I'm having problems inside my 8th case statement which opens a new menu of different shapes to find the area.... any suggestions?

Code:
```#include <stdio.h>
#include <math.h>

int main(void)
{
double sum, first_Num, sec_Num, diff, product, quotient, srt;
int choice, selection;
do
{
printf("\nChoose what calculation to perform\n");
printf("\t2 - Subtration\n");
printf("\t3 - Multiplication\n");
printf("\t4 - Division\n");
printf("\t5 - Square Root\n");
printf("\t6 - Exponents\n");
printf("\t7 - Absolute Value\n");
printf("\t8 - Area of Shapes\n");
printf("(Enter a zero to exit) \n");
printf("\nMake a selection: ");
scanf("%d", &choice);

switch (choice)
{
case 1:
{
printf("\nEnter first number: ");
scanf("%lf", &first_Num);
printf("Enter second number: ");
scanf("%lf", &sec_Num);
sum = first_Num + sec_Num;
printf("\nThe sum of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, sum);
break;
}
case 2:
{
printf("\nEnter first number: ");
scanf("%lf", &first_Num);
printf("Enter second number: ");
scanf("%lf", &sec_Num);
diff = first_Num - sec_Num;
printf("\nThe difference of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, diff);
break;
}
case 3:
{
printf("\nEnter first number: ");
scanf("%lf", &first_Num);
printf("Enter second number: ");
scanf("%lf", &sec_Num);
product = first_Num * sec_Num;
printf("\nThe product of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, product);
break;
}
case 4:
{
printf("\nEnter first number: ");
scanf("%lf", &first_Num);
printf("Enter second number: ");
scanf("%lf", &sec_Num);
quotient = first_Num / sec_Num;
printf("\nThe quotient of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, quotient);
break;
}
case 5:
{
printf("\nEnter number: ");
scanf("%lf", &srt);
{
while (srt <= 0)
{
printf("Value must be a positive number\n");
printf("Enter number: ");
scanf("%lf", &srt);
}

printf("The square root of %.4f is %.4f\n", srt, sqrt(srt));
}
break;
}
case 6:
{
printf("\nEnter base number: ");
scanf("%lf", &first_Num);
printf("Enter exponent: ");
scanf("%lf", &sec_Num);
printf("%.4f to the power of %.4f is %.4f", first_Num, sec_Num, pow(first_Num, sec_Num));
break;
}
case 7:
{
printf("\nEnter number: ");
scanf("%lf", &first_Num);
printf("The absolute value of %.4f is %.4f", first_Num, fabs(first_Num));
break;
}
case 8:
{
do
{
printf("Find the area of a shape\n");
printf("Which shape?\n");
printf("\t1 - Square\n");
printf("\t2 - Rectangle\n");
printf("\t3 - Parallelogram\n");
printf("\t4 - Trapezoid\n");
printf("\t5 - Circle\n");
printf("\t6 - Ellipse\n");
printf("\t7 - Triangle\n");
printf("\t8 - Go back to previous menu\n");

printf("Make a selection ");
scanf("%d", &selection);

switch (selection)
{
case 1:
{
printf("Enter length of a side of the square: ");
scanf("%lf", first_Num);
while (first_Num <= 0)
{
printf("ERROR!! Distance must be a positive number");
printf("Enter length of a side of the square: ");
scanf("%lf", first_Num);
}

printf("The area of the square is %.4f\n\n", (first_Num * first_Num));
break;

}
}
break;
}
while (selection != 8);
}
}
}
while (choice != 0);
return (0);
}```

8. Code:
`scanf("%lf", first_Num)`
scanf requires you to pass the adress of the variable, thus &first_Num

Tip : When you report an error, it is good to give more information.What is the error?What kind of an error is that?Where it happens( you said only this).

Or even better post what the compiler says He is not a lier not a dummy. He is clever

9. Originally Posted by std10093
Code:
`scanf("%lf", first_Num)`
scanf requires you to pass the adress of the variable, thus &first_Num

Tip : When you report an error, it is good to give more information.What is the error?What kind of an error is that?Where it happens( you said only this).

Or even better post what the compiler says He is not a lier not a dummy. He is clever

HA HA HA oh man i feel stupid. yeah i forgot the damn ampersand. the compiler wasn't giving me any errors that's why i was so confused! thanks for correcting my stupidity!

10. Originally Posted by friedsandwich
HA HA HA oh man i feel stupid. yeah i forgot the damn ampersand. the compiler wasn't giving me any errors that's why i was so confused! thanks for correcting my stupidity!
I would not call it stupidity You are still learning (like all of us )

11. here is my code any suggestions on what i should add?
all input is much appreciated!!

Code:
```#include <stdio.h>
#include <math.h>
#define PI 3.14159

int main(void)
{
double sum, first_Num, sec_Num, third_Num, diff, product, quotient, srt, area, rad1, rad2, base, height;
int choice, selection;
do
{
printf("\nChoose what calculation to perform\n");
printf("\t2 - Subtration\n");
printf("\t3 - Multiplication\n");
printf("\t4 - Division\n");
printf("\t5 - Square Root\n");
printf("\t6 - Exponents\n");
printf("\t7 - Absolute Value\n");
printf("\t8 - Area of Shapes\n");
printf("(Enter a zero to exit) \n");
printf("\nMake a selection: ");
scanf("%d", &choice);

switch (choice)
{
case 1:
{
printf("\nEnter first number: ");
scanf("%lf", &first_Num);
printf("Enter second number: ");
scanf("%lf", &sec_Num);
sum = first_Num + sec_Num;
printf("\nThe sum of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, sum);
break;
}
case 2:
{
printf("\nEnter first number: ");
scanf("%lf", &first_Num);
printf("Enter second number: ");
scanf("%lf", &sec_Num);
diff = first_Num - sec_Num;
printf("\nThe difference of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, diff);
break;
}
case 3:
{
printf("\nEnter first number: ");
scanf("%lf", &first_Num);
printf("Enter second number: ");
scanf("%lf", &sec_Num);
product = first_Num * sec_Num;
printf("\nThe product of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, product);
break;
}
case 4:
{
printf("\nEnter first number: ");
scanf("%lf", &first_Num);
printf("Enter second number: ");
scanf("%lf", &sec_Num);
quotient = first_Num / sec_Num;
printf("\nThe quotient of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, quotient);
break;
}
case 5:
{
printf("\nEnter number: ");
scanf("%lf", &srt);
{
while (srt <= 0)
{
printf("ERROR!! Value must be a positive number\n\n");
printf("Enter number: ");
scanf("%lf", &srt);
}

printf("\nThe square root of %.4f is %.4f\n", srt, sqrt(srt));
}
break;
}
case 6:
{
printf("\nEnter base number: ");
scanf("%lf", &first_Num);
printf("Enter exponent: ");
scanf("%lf", &sec_Num);
printf("\n%.4f to the power of %.4f is %.4f\n\n", first_Num, sec_Num, pow(first_Num, sec_Num));
break;
}
case 7:
{
printf("\nEnter number: ");
scanf("%lf", &first_Num);
printf("\nThe absolute value of %.4f is %.4f\n\n", first_Num, fabs(first_Num));
break;
}
case 8:
{
do
{
printf("\nFIND THE AREA OF A SHAPE\n\n");
printf("Which shape?\n");
printf("\t1 - Square\n");
printf("\t2 - Rectangle\n");
printf("\t3 - Parallelogram\n");
printf("\t4 - Trapezoid\n");
printf("\t5 - Circle\n");
printf("\t6 - Ellipse\n");
printf("\t7 - Triangle\n");
printf("\t8 - Go back to previous menu\n");

printf("\nMake a selection: ");
scanf("%d", &selection);

switch (selection)
{
case 1:
{
printf("\nArea Of A Square\n");
printf("\nEnter length of a side of the square: ");
scanf("%lf", &first_Num);
while (first_Num <= 0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter length of a side of the square: ");
scanf("%lf", &first_Num);
}
area = first_Num * first_Num;
printf("\nThe area of a square with a side of %.4f is %.4f\n\n", first_Num, area);
break;
}
case 2:
{
printf("\nArea Of A Rectangle\n");
printf("\nEnter width: ");
scanf("%lf", &first_Num);
while (first_Num <=0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter width: ");
scanf("%lf", &first_Num);
}
printf("Enter height: ");
scanf("%lf", &sec_Num);
while(sec_Num <= 0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter height: ");
scanf("%lf", &sec_Num);
}
area = first_Num * sec_Num;
printf("\nThe area of a rectagle with sides of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, area);
break;
}
case 3:
{
printf("\nArea Of A Parallelogram");
printf("\nEnter base: ");
scanf("%lf", &first_Num);
while (first_Num <=0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter base: ");
scanf("%lf", &first_Num);
}
printf("\nEnter height: ");
scanf("%lf", &sec_Num);
while(sec_Num <= 0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter height: ");
scanf("%lf", &sec_Num);
}
area = first_Num * sec_Num;
printf("\nThe area of a parallelogram with sides of %.4f and %.4f is %.4f\n\n", first_Num, sec_Num, area);
break;
}
case 4:
{
printf("\nArea Of A Trapezoid");
printf("\nEnter base-1: ");
scanf("%lf", &first_Num);
while (first_Num <=0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter base-1: ");
scanf("%lf", &first_Num);
}
printf("\nEnter base-2: ");
scanf("%lf", &sec_Num);
while(sec_Num <= 0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter base-2: ");
scanf("%lf", &sec_Num);
}
printf("\nEnter height: ");
scanf("%lf", &third_Num);
while(third_Num <= 0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter height: ");
scanf("%lf", &third_Num);
}
area = (third_Num/2) * (first_Num + sec_Num);
printf("\nThe area of a trapezoid with a base-1 of %.4f, a base-2 %.4f, and a height of %.4f is %.4f\n\n", first_Num, sec_Num, third_Num, area);
break;
}
case 5:
{
printf("\nArea Of A Circle\n");
{
printf("ERROR!! Distance must be a positive number\n\n");
}
printf("The area of a circle with a radius of %.4f is %.4f\n\n", rad1, area);
break;
}
case 6:
{
printf("\nArea Of An Ellipse");
{
printf("ERROR!! Distance must be a positive number\n\n");
}
{
printf("ERROR!! Distance must be a positive number\n\n");
}
break;
}
case 7:
{
printf("\nArea Of A Triangle");
printf("\nEnter base: ");
scanf("%lf", &base);
while(base<=0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter base: ");
scanf("%lf", &base);
}
printf("Enter height: ");
scanf("%lf", &height);
while(height<=0)
{
printf("ERROR!! Distance must be a positive number\n\n");
printf("Enter height: ");
scanf("%lf", &height);
}
area = (base * height) / 2;
printf("The area of a triangle with a base of %.4f and a height of %.4f is %.4f", base, height, area);
break;
}
default:
{
if (selection !=8)
{
}
break;
}
}
break;

}

while (selection != 8);

}
default:
{
if (choice != 0)
{
}
break;
}
}
}
while (choice != 0);
return (0);
}```

12. I'm thinking of adding volume formulas too. maybe a way to find the factors of a number but i'm not sure yet how to do that...

For example in case of division...the user may input as a second number zero (by accident or not ) .What will happen? Probably won't crash, but you should take care of situations like that.I suggest you run it and see what happens

14. Hey man. I like your code, in fact, I was going to do something like your work but my professor challenged me with this: My calc has to scan the input as a string and then parse the string to figure out what belongs where.

Input sample: 3+5*6-4/2+6*9
Output sample: 85

So far, no luck. I was just able to make the program differentiate between numbers and operators, but I still need to do a lot of work! If you have any ideas, please let me know too OP.

If I get my code to work, feel free to add it to your calc when it's done

15. Originally Posted by Icekilla
Hey man. I like your code, in fact, I was going to do something like your work but my professor challenged me with this: My calc has to scan the input as a string and then parse the string to figure out what belongs where.

Input sample: 3+5*6-4/2+6*9
Output sample: 85

So far, no luck. I was just able to make the program differentiate between numbers and operators, but I still need to do a lot of work! If you have any ideas, please let me know too OP.

If I get my code to work, feel free to add it to your calc when it's done
yeah that's what i don't like about my code. the fact that you can only do 1 operation kinda makes it less useful if i have an equation to solve. the best part of my code, imo, is the formulas for area, im also doing the same thing for volumes too. if i can come up with a code like you are trying to that would probably save a lot of memory, (which doesnt matter since my code is built for a pc not an embedded system though) but would add a lot more functionality. let me know what you come up with, I'll keep my code up to date with any changes i make also.