7 Replies - 255 Views - Last Post: 13 July 2012 - 03:34 PM Rate Topic: -----

#1 nigelmichael  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-July 12

How do I work with this?

Posted 12 July 2012 - 01:37 PM

Hey guys, I have been awake all night for 9 hours straight without rest trying to figure out what loop to use and I cant seen to get it even though how hard I try. Well, I am supposed to
create a program that computes Voltage and Power of a circuit. I have everything up and running except for the looping part. I need to run the program for infinite times, until I enter 0, which will then calculate and sum everything up.

I need the result something like this:

printf ("+------------+------------+------------+------------+------------+\n");
printf ("| No of              | Total               | Average          | Total              | Average           |\n");
printf ("| Circuits:          | Voltage:          | Voltage:          | Power:           | Power:             |\n");
printf ("|	                    |		             |		              |	                      |	                       |\n");
printf ("|		12	  	 48.91mV	           4.07mV           292.02mW            24.34mW  |\n");
printf ("+------------+------------+------------+------------+------------+\n");



And for now, this is all I got.
#include <stdio.h>

int main ()
{

	int circuit;


{


{
	printf ("Please enter the type of circuits being tested.\nPlease enter 1 for Serial, 2 for Parallel.\n");
	scanf ("%d", &circuit);
}	

while (circuit != 1 && circuit != 2)
{
	printf ("Invalid input.\n");
	printf ("Please re-enter the type of circuits being tested. 1 for Serial, 2 for Parallel.");
	scanf ("%d", &circuit);

}


	if (circuit==1)
	{
	printf ("You have chosen: Serial.\n");
	}

	else
	{
	printf ("You have chosen: Parallel.\n");
	goto parallel;
	}

}

{
float current;
float resistor1;
float resistor2;
float resistor3;	


	printf ("Please enter the current value of the circuit: ");
	scanf ("%f", &current);


while (current > 500 || current < 5)
{
	printf ("Please re-enter the current value: ");
	scanf ("%f", &current);
}


	printf ("Now, enter the first resistor value: ");
	scanf ("%f", &resistor1);
	printf ("Now, enter the second resistor value (If not applicable, type 0): ");
	scanf ("%f", &resistor2);
	printf ("Now, enter the third resistor value (If not applicable, type 0): ");
	scanf ("%f", &resistor3);

float voltage;
float power;

	voltage=current*(resistor1+resistor2+resistor3);
	power=current*voltage;
	
	printf ("The voltage of the circuit is %.2fmV.\n", voltage);
	printf ("The power of the circuit is %.2fmW.\n", power);

return 0;
}


	parallel:
{
float current;
float resistor1;
float resistor2;
float resistor3;


	printf ("Please enter the current value of the circuit: ");
	scanf ("%f", &current);


while (current > 500 || current < 5)
{
	printf ("Please re-enter the current value: ");
	scanf ("%f", &current);
}


	printf ("Now, enter the first resistor value: ");
	scanf ("%f", &resistor1);
	printf ("Now, enter the second resistor value (If not applicable, type 0): ");
	scanf ("%f", &resistor2);
	printf ("Now, enter the third resistor value (If not applicable, type 0): ");
	scanf ("%f", &resistor3);

float voltage;
float power;


if (resistor1 == 0)
{
voltage=current*(1/((1/resistor2)+(1/resistor3)));
power=current*voltage;
	printf ("The voltage of the circuit is %.2fmV.\n", voltage);
	printf ("The power of the circuit is %.2fmW.\n", power);
}


else if (resistor2 == 0)
{
voltage=current*(1/((1/resistor1)+(1/resistor3)));
power=current*voltage;
	printf ("The voltage of the circuit is %.2fmV.\n", voltage);
	printf ("The power of the circuit is %.2fmW.\n", power);
}


else if (resistor3 == 0)
{
voltage=current*(1/((1/resistor1)+(1/resistor2)));
power=current*voltage;
	printf ("The voltage of the circuit is %.2fmV.\n", voltage);
	printf ("The power of the circuit is %.2fmW.\n", power);
}


else
{
voltage=current*(1/((1/resistor1)+(1/resistor2)+(1/resistor3)));
power=current*voltage;
	printf ("The voltage of the circuit is %.2fmV.\n", voltage);
	printf ("The power of the circuit is %.2fmW.\n", power);	
}


return 0;
}
}

This post has been edited by jimblumberg: 12 July 2012 - 01:44 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: How do I work with this?

#2 jimblumberg  Icon User is online

  • member icon

Reputation: 3045
  • View blog
  • Posts: 9,281
  • Joined: 25-December 09

Re: How do I work with this?

Posted 12 July 2012 - 01:47 PM

First use code tags when posting code.

:code:

Next does this program even compile? If not post the complete error messages exactly as they appear in your development environment.

And you need to find an indentation style and use it consistently. This will make your code much easier to read and troubleshoot.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 nigelmichael  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-July 12

Re: How do I work with this?

Posted 12 July 2012 - 01:52 PM

Yes, it does compile, but only for a single circuit. I need to get it to compute all the circuit types entered, and find the average out of the number of circuits entered.
Was This Post Helpful? 0
  • +
  • -

#4 totgeburt  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 220
  • Joined: 03-August 11

Re: How do I work with this?

Posted 12 July 2012 - 01:59 PM

so make a sentinel controlled loop since you are entering an arbitrary amount of data, and make the sentinel = 0. that way, when you enter zero, viola! it stops the loop. just as you described
something like while(input != 0) or whatever

as for the average, you'll need a variable to hold a total and a variable to be a counter, then divide total by counter and you'll get your average

This post has been edited by totgeburt: 12 July 2012 - 02:01 PM

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon

Reputation: 3045
  • View blog
  • Posts: 9,281
  • Joined: 25-December 09

Re: How do I work with this?

Posted 12 July 2012 - 01:59 PM

First properly indent your code, then I suggest you rework your code to remove all the goto statements. Learn to use one of the other looping statements like while(), do/while() or the for loop. Using goto makes following your logic very difficult and they are considered a bad programming practice.


Jim
Was This Post Helpful? 1
  • +
  • -

#7 DoNotWant  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 47
  • Joined: 03-November 11

Re: How do I work with this?

Posted 13 July 2012 - 12:31 AM

while (circuit != 1 && circuit != 2)
Lol wut? Line 17.
Was This Post Helpful? 0
  • +
  • -

#8 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 489
  • View blog
  • Posts: 1,540
  • Joined: 20-March 10

Re: How do I work with this?

Posted 13 July 2012 - 11:39 AM

Hi NigelMichael,

I take it your an electrical engineering student ?

anyway to your problem ...

Your whole program needs to optimised... for functions.

goto as has been said should be avoided.

link to explanation of function calls

I know this is a different way of thinking... so...
I will give you this..

#include <stdio.h>

/*Function prototypes*/
void parallel();
void serial();
void Details(float *current, float *resistor1, float *resistor2, float *resistor3);
void Table(int totalcircuits);
/*Globals*/
float TotalPower;
float TotalVoltage;

int main ()
{

    int circuit;
    int totalcircuits;

    printf ("Please enter the total amount of circuits being tested.\n");
    scanf ("%d", &totalcircuits);

    while (circuit != 1 && circuit != 2)
    {
        printf ("Please enter the type of circuits being tested.\nPlease enter 1 for Serial, 2 for Parallel.\n");
        scanf ("%d", &circuit);
        if (circuit != 1 && circuit != 2)
        {
          printf ("Invalid input.\n");
        }

    }


    if (circuit==1)
    {
        int x;

        printf ("You have chosen: Serial.\n");
        for (x=0; x < totalcircuits; x++)
        {
        printf("Circuit No. %d\n",x+1);
        serial();
        }
    }

    else
    {
        int x;
        printf ("You have chosen: Parallel.\n");
        for (x=0; x < totalcircuits; x++)
        {
        printf("Circuit No. %d\n",x+1);
        parallel();
        }
    }
    printf("\n");
    printf("The Table \n\n");
    Table(totalcircuits);
    return 0;

}



Now all you have to do is write the functions for this program...

Best Wishes

Snoopy.

This post has been edited by snoopy11: 13 July 2012 - 11:43 AM

Was This Post Helpful? 0
  • +
  • -

#9 totgeburt  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 220
  • Joined: 03-August 11

Re: How do I work with this?

Posted 13 July 2012 - 03:34 PM

you're such a nice guy snoopy
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1