Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,209 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 914 people online right now. Registration is fast and FREE... Join Now!




how to print results every second?

 
Reply to this topicStart new topic

how to print results every second?

Pegasus
post 17 Feb, 2008 - 06:23 AM
Post #1


New D.I.C Head

*
Joined: 19 Jan, 2008
Posts: 4

Hi there is there anyone who can help, I have managed to write this programme in C and it works!! ( first time for everything ).

The problem is that the programme asks for a time increment but the returned value is not in seconds how can I adapt the programme to return the values in second(s) intervals upto the final returned value?

CODE
#include<stdio.h>
#include<math.h>

bool Check(float P, float Vf, float Vin)
{
    float diff = Vf-Vin;

    if(diff<0)
        diff *= -1;

    if(diff<P)
        return 0;

    return 1;
}

float CalcVolt(float Vin, float R1, float R2, float C, int t)
{
    float Vout;

    Vout = float(Vin * (R2/(R1+R2)) * (1 - exp (-t * (R1+R2)/(C*R1*R2))));

    return Vout;
}

void Voltage(float Vin, float Vf, float R1, float R2, float C, int t, float Percentage)
{
    float Vout;

    int i = 0;

    do
    {
        Vout = CalcVolt(Vin, R1, R2, C, i*t);
        i += t;

        printf("At t = %3d : Vout = %5.3f Volts\n", i*t, Vout);
    }
    while(Check(Percentage, Vf, Vout));
}

int main()
{
    float Vf, Vin, R1, R2, C;
    float Percentage[3] = {0.1f, 0.05f, 0.01f};
    int choice;
    int t;

    printf("Enter Value of Vin: ");
    scanf("%f", &Vin);

    printf("Enter Value of R1: ");
    scanf("%f", &R1);

    while(R1<0)
    {
        printf("Negative value not allowed. Enter again. ");
        scanf("%f", &R1);
    }

    printf("Enter Value of R2: ");
    scanf("%f", &R2);

    while(R2<0)
    {
        printf("Negative value not allowed. Enter again. ");
        scanf("%f", &R2);
    }

    printf("Enter Value of Capacitor: ");
    scanf("%f", &C);
    
    while(C<0)
    {
        printf("Negative value not allowed. Enter again: ");
        scanf("%f", &C);
    }

    printf("Enter Value of t (time increment): ");
    scanf("%d", &t);

    while(t<0)
    {
        printf("Negative value not allowed. Enter again: ");
        scanf("%d", &t);
    }

    printf("Enter Value of Vf (final Voltage): ");
    scanf("%f", &Vf);

    printf("\nEnter 1 for 10%% of final value\n");
    printf("Enter 2 for  5%% of final value\n");
    printf("Enter 3 for  1%% of final value\n");
    printf("Percentage : ");
    scanf("%d", &choice);

    while(choice<1 || choice>3)
    {
        printf("Invalid choice, Enter again: ");
        scanf("%d", &choice);
    }

    printf("\n");

    Voltage(Vin, Vf, R1, R2, C, t, Percentage[choice-1]);

    return 0;
}


This post has been edited by Pegasus: 17 Feb, 2008 - 06:48 AM
User is offlineProfile CardPM

Go to the top of the page

GWatt
post 17 Feb, 2008 - 06:37 AM
Post #2


human inside

Group Icon
Joined: 1 Dec, 2005
Posts: 2,150



Thanked 16 times

Dream Kudos: 450
My Contributions


Hi there. Next time code.gif

You can use the time function, a variable, and a while loop like so.

CODE
#include <time.h>
/*
* function declaration
*/
long t = time(0);
while(/*some condition here*/)
{
    if (time(0) > t)
    {
        printf("stuff\n");
        t = time(0);
    }
}
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 07:38PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month