Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Need help finding weight left over still to lose with for loop

code c codeinc

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1 mm1990

mm1990

    CC Newcomer

  • Member
  • PipPip
  • 15 posts

Posted 19 June 2015 - 09:04 AM

I have the while loop for finding weight left over, but I need to do a for loop. Can someone please help me I tried so many different ways to find a for loop and it is very hard to find.
 
#include <stdio.h>




int main(){




    FILE * filePtr = NULL;


    /* Name of file I am creating */
    
    char * weigh = "Weightlog.txt";


    /* Creating file for weightlog */
    
    filePtr = fopen(weigh, "w");


    /* Data for the text file */
    
    fprintf(filePtr, "135\n");
    fprintf(filePtr, "5\n");
    fprintf(filePtr, "158.2 157.5\n");
    fprintf(filePtr, "157.0 156.5\n");
    fprintf(filePtr, "156.6 151.2\n");
    fprintf(filePtr, "151.0 148.9\n");
    fprintf(filePtr, "148.7 146.4\n");
    fprintf(filePtr, "0\n");


    /* Closing the file, so I can open it again in read mode */
    
    fclose(filePtr);




    /* Reading the file */
    
    filePtr =  fopen(weigh, "r");


    /* Setting variables for the output */
    
    int goal = 0, week;
    float week1beg , week1end, week2beg, week2end, week3beg, week3end;
    float week4beg, week4end, week5beg, week5end, expr1, expr2, expr3;
    float expr4, expr5, expr6, expr7, min=0, max=6, average=3;
    /* Goal weight printed and scanning the weight numbers */
    
    fscanf(filePtr, "%d", &goal);
    fscanf(filePtr, "%d", &week);
    fscanf(filePtr, "%f", &week1beg);
    fscanf(filePtr, "%f", &week1end);
    fscanf(filePtr, "%f", &week2beg);
    fscanf(filePtr, "%f", &week2end);
    fscanf(filePtr, "%f", &week3beg);
    fscanf(filePtr, "%f", &week3end);
    fscanf(filePtr, "%f", &week4beg);
    fscanf(filePtr, "%f", &week4end);
    fscanf(filePtr, "%f", &week5beg);
    fscanf(filePtr, "%f", &week5end);
    
    printf("Progress Report After 5 weeks\n");
    printf("-----------------------------\n");
    printf("Goal Weight: %d lbs\n", goal);
    
    /* Calculating number of pounds lost each week. Also Total weight lost */
    
    expr1 = week1beg - week1end;
    expr2 = week2beg - week2end;
    expr3 = week3beg - week3end;
    expr4 = week4beg - week4end;
    expr5 = week5beg - week5end;
    expr6 = expr1 + expr2 + expr3 + expr4 + expr5;
    
    printf("Total Weight Lost: %.1f lbs\n", expr6);
    
    /* While loop for finding minimum and maximum weight lost in a week */
    /* Minimum */
    
    while(min>=0)
    
    {
        if (expr1 >= min)
        {
            printf("Min lost in a week: %.1f lbs\n", expr1);
            min = -1;
        }
        else if (expr2 >= min)
        {
            printf("Min lost in a week: %.1f lbs\n", expr2);
            min = -1;
        }
        else if (expr3 >= min)
        {
            printf("Min lost in a week: %.1f lbs\n", expr3);
            min = -1;
        }
        else if (expr4 >= min)
        {
            printf("Min lost in a week: %.1f lbs\n", expr4);
            min = -1;
        }
        else if (expr5 >= min)
        {
            printf("Min lost in a week: %.1f lbs\n", expr5);
            min = -1;
        }
        else
        {
            min--;
        }
    }
    
    /* Maximum */
    
    while(max>=0)
    
    {
        if (expr1 >= max)
        {
            printf("Max lost in a week: %.1f lbs\n", expr1);
            max = -1;
        }
        else if (expr2 >= max)
        {
            printf("Max lost in a week: %.1f lbs\n", expr2);
            max = -1;
        }
        else if (expr3 >= max)
        {
            printf("Max lost in a week: %.1f lbs\n", expr3);
            max = -1;
        }
        else if (expr4 >= max)
        {
            printf("Max lost in a week: %.1f lbs\n", expr4);
            max = -1;
        }
        else if (expr5 >= max)
        {
            printf("Max lost in a week: %.1f lbs\n", expr5);
            max = -1;
        }
        else
        {
            max--;
        }
    }
    
    /* While loop for finding average weight */
    
expr7 = expr6/5;


while(average>=0)
    
    {
        if (expr7 >= average)
        {
            printf("Avg weight lost per week: %.1f lb\n", expr7);
            average = -1;
        }
        else
        {
            average--;
        }
    }


 /* While loop for finding weight still to be lost */


float expr8;


expr8=week5end-goal;


while(goal>=0)
    
    {
        if (expr8 >= goal)
        {
            printf("Weight Goal is not met yet\n");     
            printf("Weight still to be lost: %.1f lbs\n", expr8);
            goal = -1;
        }
        else
        {
            goal--;
        }
    }
    /* Close the file and exit */
    fclose(filePtr);


  
    
    
    
    system("PAUSE");
    return 0;
}

Edited by dargueta, 01 July 2015 - 07:07 PM.
Added code tags.


#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 19 June 2015 - 10:32 AM

From your question and code, it's not clear what you're trying to do. It sounds like you have a homework assignment to do something using a for loop, but I'm not really clear what that something is. Given that it is a for loop, I suspect you're supposed to be using arrays to store your data.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 mm1990

mm1990

    CC Newcomer

  • Member
  • PipPip
  • 15 posts

Posted 19 June 2015 - 03:56 PM

The weight I have to get to is 135 and near the top of my code you can see where my current weight is on the code.

 

This is what it outputs:

Progress Report After 5 weeks----------------------------------------Goal Weight: 135 lbsTotal Weight Lost: 11.0 lbsMin lost in a week: 0.5 lbsMax lost in a week: 5.4 lbsAvg weight lost per week:  2.2 lbWeight Goal is not met yetWeight still to be lost: 11.4 lbs



#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 20 June 2015 - 01:23 PM

I see a LOT of numbers and calculations, but I don't know the entire problem. You have to get to 135. Based on what? 5*27? 3*45? something else?

 

Assume we have no idea what you're doing. For example, how does the file fit into all this?


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#5 TheSLY

TheSLY

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 29 June 2015 - 05:30 PM

Take A Look:

int i=0;
while(i<=10) {

//your code here
i++;
}

Is same as:

for(int i=0;i<=10;i++) {

//your code here

}

Depending on what you're trying to do, use one of them.


Edited by TheSLY, 29 June 2015 - 05:32 PM.


#6 AceInfinity

AceInfinity

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 313 posts

Posted 04 July 2015 - 01:27 PM

Take A Look:

int i=0;
while(i<=10) {

//your code here
i++;
}
Is same as:
for(int i=0;i<=10;i++) {

//your code here

}
Depending on what you're trying to do, use one of them.

 
In theory yes, but with that for loop example i is scopped to that structure. His while loop is actually the same as:
int i = 0;
for (;i <= 10; i++)
{
}

Microsoft MVP (2012) - .NET Programming | ®Crestron DMC-T Certified Programmer
Posted Image





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download