Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

I can't figure out how to get min, max, and average of text file in C! Help!

code c

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

#1 mm1990

mm1990

    CC Newcomer

  • Member
  • PipPip
  • 15 posts

Posted 19 June 2015 - 12:14 AM

This is what my code is supposed to output in command prompt:

 

Progress Report After 5 weeks

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

Goal Weight: 135 lbs

Total Weight Lost: 11.0 lbs

Min lost in a week: 0.5 lbs

Max lost in a week: 5.4 lbs

Avg weight lost per week: 2.2 lb

Weight Goal is not met yet

Weight still to be lost: 11.4 lbs

 

I am trying minimum first with while loop and I can't figure it out! Please help! Here is what I have so far:

 

#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, min, max, average;
    /* 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. */
    
    expr1 = week1beg - week1end;
    expr2 = week2beg - week2end;
    expr3 = week3beg - week3end;
    expr4 = week4beg - week4end;
    expr5 = week5beg - week5end;
    
    /* While loop for finding minimum weight lost in a week */
    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--;
        }
    }
 
 
    /* Close the file and exit */
    fclose(filePtr);
 
  
    
    
    
    system("PAUSE");
    return 0;
}
 


#2 mm1990

mm1990

    CC Newcomer

  • Member
  • PipPip
  • 15 posts

Posted 19 June 2015 - 01:33 AM

I found the min and max by myself finally, but not i need help to figure out how to find the average with a for loop in C. Here is what I attempted so far:

 

#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, min=0, max=6, average;
    /* 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--;
        }
    }
    
    /* For loop for finding average weight */
 
 float average = 5, i, ave, expr7;
 
 expr7=expr6/average;
 
 for(i=0; i<times; i++){
 
expr6+=expr7;
 
 
    /* Close the file and exit */
    fclose(filePtr);
 
  
    
    
    
    system("PAUSE");
    return 0;
}


#3 AceInfinity

AceInfinity

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 313 posts

Posted 04 July 2015 - 01:26 PM

Pseudocode:

var sum = 0;
for (int i = 0; i < count; ++i)
{
  var value = data[i];
  sum += value;
}
var average = sum / count;

 

If reading from a file, you read until EOF and increment the # of records parsed from the file until that point, AS you add to the sum. Then divide by the sum by that count to get an average.

 

Essentially you're trying to figure out the total # of values, and divide this from the sum of all of the values, so you'll need to add the values together, and get the total count one way or another.


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