Like Tree7Likes

Idea for final project

This is a discussion on Idea for final project within the General Discussions forums, part of the Community Boards category; I suggest using modulus "%" to display the remainder and still do just integer division. Tim S....

  1. #16
    Registered User
    Join Date
    May 2009
    Posts
    2,663
    I suggest using modulus "%" to display the remainder and still do just integer division.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  2. #17
    Registered User
    Join Date
    Nov 2012
    Posts
    35
    Quote Originally Posted by stahta01 View Post
    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. #18
    Registered User
    Join Date
    Nov 2012
    Posts
    23
    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.
    Last edited by root; 11-12-2012 at 11:33 AM.

  4. #19
    Registered User
    Join Date
    Nov 2012
    Posts
    35
    Quote Originally Posted by root View Post
    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. #20
    Registered User
    Join Date
    Nov 2012
    Posts
    35
    Quote Originally Posted by root View Post
    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. #21
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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. #22
    Registered User
    Join Date
    Nov 2012
    Posts
    35
    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("\t1 - Addition\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);
    }
    Last edited by friedsandwich; 11-12-2012 at 01:56 PM.

  8. #23
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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
    friedsandwich likes this.

  9. #24
    Registered User
    Join Date
    Nov 2012
    Posts
    35
    Quote Originally Posted by std10093 View Post
    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. #25
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by friedsandwich View Post
    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. #26
    Registered User
    Join Date
    Nov 2012
    Posts
    35
    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("\t1 - Addition\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("\nEnter the radius: ");
    								scanf("%lf", &rad1);
    								while(rad1 <= 0)
    								{
    									printf("ERROR!! Distance must be a positive number\n\n");
    									printf("Enter the radius: ");
    									scanf("%lf", &rad1);
    								}
    								area = (rad1 * rad1) * PI;
    								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("\nEnter radius-1: ");
    								scanf("%lf", &rad1);
    								while(rad1 <= 0)
    								{
    									printf("ERROR!! Distance must be a positive number\n\n");
    									printf("Enter radius-1: ");
    									scanf("%lf", &rad1);
    								}
    								printf("\nEnter radius-2: ");
    								scanf("%lf", &rad2);
    								while(rad2 <= 0)
    								{
    									printf("ERROR!! Distance must be a positive number\n\n");
    									printf("Enter radius-2: ");
    									scanf("%lf", &rad2);
    								}
    								area = (rad1 * rad2) * PI;
    								printf("The area of an ellipse with a radius-1 of %.4f and a radius-2 of %.4f is %.4f", rad1, rad2, area);
    								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)
    								{
    									printf("\nYour selection was not from the menu\n");
    									printf("Please try again.\n\n");
    								} 
    								break;
    							}
    						}
    						break;
    
    
    					}
    
    
    				while (selection != 8);
    			
    			}
    			default:
    			{	
    				if (choice != 0)
    				{
    					printf("\nYour selection was not from the menu\n");
    					printf("Please try again.\n\n");
    				}
    				break;
    			}
    		}
    	}
    	while (choice != 0);
    return (0);
    }

  12. #27
    Registered User
    Join Date
    Nov 2012
    Posts
    35
    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...

  13. #28
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Before adding functionality , test functionality that you already have!

    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. #29
    Registered User Icekilla's Avatar
    Join Date
    Nov 2012
    Posts
    8
    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. #30
    Registered User
    Join Date
    Nov 2012
    Posts
    35
    Quote Originally Posted by Icekilla View Post
    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.

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ideas for a final project
    By doubleanti in forum General Discussions
    Replies: 11
    Last Post: 09-09-2012, 08:00 AM
  2. Must be final project time again....
    By VirtualAce in forum General Discussions
    Replies: 21
    Last Post: 05-19-2010, 07:04 PM
  3. Final Project for HS, any ideas?
    By Conneticut#88 in forum C++ Programming
    Replies: 13
    Last Post: 05-24-2006, 09:22 PM
  4. Final year project
    By afisher in forum C# Programming
    Replies: 3
    Last Post: 07-04-2005, 08:15 AM
  5. Final year project
    By khpuce in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 10-10-2003, 07:04 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21