Computer Help and Spyware Removal Computer Help and Spyware Removal Computer Help and Spyware Removal Computer Help Forums Windows Startup Programs Database Spyware and Malware Removal Guides Computer Tutorials Uninstall Database File Database Computer Glossary Computer Resources
 

Welcome Guest ( Log In | Click here to Register a free account now! )



Register a free account to unlock additional features at BleepingComputer.com
Welcome to Bleeping Computer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.
Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

 
Reply to this topicStart new topic
> Here Is A Program I Wrote For Class, it is using ohms law, can I make it shorter and better?
ryan_w_quick
post Feb 2 2008, 11:44 PM
Post #1


Forum Regular
***

Group: Members
Posts: 316
Joined: 24-March 05
From: Fairland, IN
Member No.: 15,312



It just seems really long, repetitve, and clunky for what it does. Can I make it better and more efficient?

-------------------------------------------------------------------------------------------
CODE
//Ryan Quick
//Ohm's Law
#include <stdio.h>
#include <math.h>
#include <time.h>
void stuff ();

void main ()
{
    stuff ();
}

void stuff ()
{
    //Local Declaration

    char unknown;
    double a,b,answer;
    int c,d,e;

    //Statements
    printf("\n\nNote:  Be sure to use standard units for all calculations.\n");
    printf("When asked yes or no questions, answer 1 for yes and 0 for no.\n");
    printf("What do you want to solve for?\n v = voltage \n i = current \n r = resistance \n p = power\n");
    scanf("%c", &unknown);//What you want to solve for?

    //Solving for Voltage

    if(unknown == 'v')
    {
        printf("Do you know current?\n");
        scanf("%d", &c);
            if(c == 1)
            {
                printf("Do you know resistance?\n");
                scanf("%d", &d);
                if(d == 1)
                {
                    printf("What is the current?\n");
                    scanf("%lf", &a);
                    printf("What is the resistance?\n");
                    scanf("%lf", &b);
                    answer = a * b;
                    printf("%lf Volts\n", answer);
                }
                if(d == 0)
                {
                    printf("What is the current?\n");
                    scanf("%lf", &a);
                    printf("What is the power?\n");    
                    scanf("%lf", &b);
                    answer = b/a;
                    printf("%lf Volts\n", answer);
                }
            }
            if(c == 0)
            {
                printf("What is the resistance?\n");
                scanf("%lf", &a);
                printf("What is the power?\n");
                scanf("%lf", &b);
                answer = sqrt(a * B);
                printf("%lf Volts\n", answer);
            }
    }

    //Solving for Current

    if(unknown == 'i')
    {
        printf("Do you know voltage?\n");
        scanf("%d", &c);
            if(c == 1)
            {
                printf("Do you know resistance?\n");
                scanf("%d", &d);
                if(d == 1)
                {
                    printf("What is the voltage?\n");
                    scanf("%lf", &a);
                    printf("What is the resistance?\n");
                    scanf("%lf", &b);
                    answer = a / b;
                    printf("%lf Amperes\n", answer);
                }
                if(d == 0)
                {
                    printf("What is the voltage?\n");
                    scanf("%lf", &a);
                    printf("What is the power?\n");
                    scanf("%lf", &b);
                    answer = b/a;
                    printf("%lf Amperes\n", answer);
                }
            }
            if(c == 0)
            {
                printf("What is the resistance?\n");
                scanf("%lf", &a);
                printf("What is the power?\n");
                scanf("%lf", &b);
                answer = sqrt(b/a);
                printf("%lf Amperes\n", answer);
            }
    }

    //Solving for Resistance

    if(unknown == 'r')
    {
        printf("Do you know voltage?\n");
        scanf("%d", &c);
            if(c == 1)
            {
                printf("Do you know current?\n");
                scanf("%d", &d);
                if(d == 1)
                {
                    printf("What is the voltage?\n");
                    scanf("%lf", &a);
                    printf("What is the current?\n");
                    scanf("%lf", &b);
                    answer = a / b;
                    printf("%lf Ohms\n", answer);
                }
                if(d == 0)
                {
                    printf("What is the voltage?\n");
                    scanf("%lf", &a);
                    printf("What is the power?\n");
                    scanf("%lf", &b);
                    answer = a * a / b;
                    printf("%lf Ohms\n", answer);
                }
            }
            if(c == 0)
            {
                printf("What is the current?\n");
                scanf("%lf", &a);
                printf("What is the power?\n");
                scanf("%lf", &b);
                answer = (B) / (a*a);
                printf("%lf Ohms\n", answer);
            }
    }

    //Solving for Power

    if(unknown == 'p')
    {
        printf("Do you know voltage?\n");
        scanf("%d", &c);
            if(c == 1)
            {
                printf("Do you know current?\n");
                scanf("%d", &d);
                if(d == 1)
                {
                    printf("What is the voltage?\n");
                    scanf("%lf", &a);
                    printf("What is the current?\n");
                    scanf("%lf", &b);
                    answer = a * b;
                    printf("%lf Watts\n", answer);
                }
                if(d == 0)
                {
                    printf("What is the voltage?\n");
                    scanf("%lf", &a);
                    printf("What is the resistance?\n");
                    scanf("%lf", &b);
                    answer = a * a / b;
                    printf("%lf Watts\n", answer);
                }
            }
            if(c == 0)
            {
                printf("What is the current?\n");
                scanf("%lf", &a);
                printf("What is the resistance?\n");
                scanf("%lf", &b);
                answer = a * a * b;
                printf("%lf Watts\n", answer);
            }
    }
    main ();
}


--------------------
"To do less than your best is to sacrifice the gift." Steve Prefontaine

"The things you own end up owning you." Tyler Durden

"I do not feel obliged to believe that the same god who has endowed us with sense, reason and intellect has intended us to forgo their use." Galileo

Go to the top of the page
 
+Quote Post
ryan_w_quick
post Feb 2 2008, 11:46 PM
Post #2


Forum Regular
***

Group: Members
Posts: 316
Joined: 24-March 05
From: Fairland, IN
Member No.: 15,312



i don't know why it did those smiley, but they should be a b followed by a parentheses
cool.gif b )


--------------------
"To do less than your best is to sacrifice the gift." Steve Prefontaine

"The things you own end up owning you." Tyler Durden

"I do not feel obliged to believe that the same god who has endowed us with sense, reason and intellect has intended us to forgo their use." Galileo

Go to the top of the page
 
+Quote Post
groovicus
post Feb 3 2008, 08:53 AM
Post #3


Hail Groovicus!
******

Group: Site Admin
Posts: 5,769
Joined: 5-June 04
From: Vermillion, SD
Member No.: 689



I fixed your post to use the code tags so that the code would format properly, otherwise it is too hard to read.

Anytime you are repeating code, it becomes a candidate for writing a reusable function. You also have some statements that can be combined. For instance,
CODE
answer = a * b;
printf("%lf Volts\n", answer);


Could be combined into:
CODE
                    printf("%lf Volts\n", (a*b));


That in itself would eliminate a dozen lines of code.

You use the following code 6 times:
CODE
printf("What is the current?\n");
scanf("%lf", &a);

That makes it a candidate for writing a function. Something like this:
CODE
int getCurrent(){
    printf("What is the current?\n");
    scanf("%lf", &a);
    return a;
}


Now when you need to get the current, you use something like:
CODE
a= getCurrent();


I have not coded in any c-based language for awhile, so my syntax is probably not correct. You can do the same thing with getResistance, getPower, etc. That would clear up a bunch of your code.

You don't need to worry about efficiency. If your code was 100000 lines long, then maybe.

One final comment on your variable names. Unless they are being instantiated and used right away in just one spot, they should never be called a,b,c, etc. They should have useful names that lets you know exactly what that variable is holding. A variable named current allows me to better understand the code. A varaible named unknown is equally useless. You might know what it means, but someone else trying to look at your code won't have a clue.


--------------------
Go to the top of the page
 
+Quote Post
ryan_w_quick
post Feb 3 2008, 02:33 PM
Post #4


Forum Regular
***

Group: Members
Posts: 316
Joined: 24-March 05
From: Fairland, IN
Member No.: 15,312



yeah my prof gets on me for those short meaningless variable names. I guess I'll fix that, along with putting in those functions. Thanx for the tips, and yeah that syntax is mostly right, thanx.


--------------------
"To do less than your best is to sacrifice the gift." Steve Prefontaine

"The things you own end up owning you." Tyler Durden

"I do not feel obliged to believe that the same god who has endowed us with sense, reason and intellect has intended us to forgo their use." Galileo

Go to the top of the page
 
+Quote Post
rongchaua
post Feb 3 2008, 05:48 PM
Post #5


Member
**

Group: Members
Posts: 57
Joined: 18-January 08
Member No.: 184,382



@ryan: I'm not professional but I have some tips. Maybe they are good for you.

1. Use if...else... or switch case... instead of only if. Try to look at your structure.

if(unknown == 'v')...
if(unknown == 'i')...
if(unknown == 'r')....

The program will be slowed down because it must always test all of cases. For example, I choose 'v', and the program calculates, displays the results.... and after solving the case (unknown =='v') it will be test straighforward "if (unknown=='i'), (unknown=='r')..." it is senseless. Because unknown can not be i,r,p anymore. And the other programmers who can resume later your work, will have difficulty to recognize the flow control of program and the relation between cases.

2. Try to catch the error as well as you can. What will it occur when I do not enter one of v,i,r,p. The main() function will also be called. Only God knows what will happen. And there are also more bugs in your program about catching exception.

3. answer = a * a / b; <-- Be carefull with the priority of operation. Use bracket to make it clearer. It's just better for you. Because sometimes it'll make you crazy to figure out where the error is.

Hope you will not be annoyed with my tips. I do not intend to "teach" you. Just share you some tips which I use in my work.

This post has been edited by rongchaua: Feb 3 2008, 05:54 PM


--------------------


Go to the top of the page
 
+Quote Post
ryan_w_quick
post Feb 3 2008, 10:34 PM
Post #6


Forum Regular
***

Group: Members
Posts: 316
Joined: 24-March 05
From: Fairland, IN
Member No.: 15,312



I know i need to work more on the sensless and repetitive code, but I'm not to woried about the math and order of operations and precedence that you are talking about. I usualy use a lot of parentheses to keep things very clear in my mind, but this is a formula i'm very familiar with and just didnt feel the need.


--------------------
"To do less than your best is to sacrifice the gift." Steve Prefontaine

"The things you own end up owning you." Tyler Durden

"I do not feel obliged to believe that the same god who has endowed us with sense, reason and intellect has intended us to forgo their use." Galileo

Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



Lo-Fi Version Time is now: 21st August 2008 - 05:38 PM


Advertise   |   About Us   |   Terms of Use   |   Privacy Policy   |   Contact Us   |   Site Map   |   Chat   |   Tutorials   |   Uninstall List
Discussion Forums   |   The Computer Glossary   |   Resources   |   RSS Feeds   |   Startups   |   The File Database   |   Malware Removal Guides

© 2003-2008 All Rights Reserved Bleeping Computer LLC.