C++ Read Integers from File, find sum, find average, find Min/Max average

Discussion in 'Mac Programming' started by corywoolf, Feb 17, 2010.

  1. macrumors 65816

    corywoolf

    Joined:
    Jun 28, 2004
    #1
    Hi,

    I am in a C++ class and am having trouble piecing together a program that does the following:

    (C++)
    Read 6 Integers from File, find sum, find average, and find Min/Max average.

    I borrowed some code from other forums that had similar programs, but obviously it doesn't match my needs specifically.

    Here is the assignment:
    Code:
    
    //******************************************************************************************
    // This program calculates the sum, average.
    // By Cory Woolf
    //******************************************************************************************
    
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <cctype>
    #include <fstream>
    #include <stdio.h>
    
    using namespace std;
    
    main ()
    {
    
    while (inFile >> num)
    {
    	
    	if (count3 == 0) 
    	{
    		max = num;
    		min = num;
    	}
    	if (num > max) 
    	{
    		max = num;
    	}
    	if (num < min)
    	{
    		min = num;
    	}
    	count3++;
    	
    	
    	sum += num;
    	totSum += num;
    	count1++;
    	count2++;
    }
    	
    
    	
    	
    	int main()
    	{
    		float Max, Min, num;
    		int i;
    		FILE *fi;
    		FILE *fo;
    		if ((fi = fopen("a3.txt")) == 0)
            {
    			printf("error opening file\n");
    			return 1;
            }
    		
    		fscanf(fi, %f, & Max);
    		Min = Max;
    		
    		for (i = 0; i < 8; i ++)
    		{
    			if (num > Max)
    			{
    				Max = num;
    			}
    			if (Temp < Min)
    			{
    				Min = num;
    			}
    		}
    		fclose(fi);
    		if ( (fo=fopen("a3.txt")) == NULL )
    		{
    			printf("ERROR opening file\n");
    			return 1;
    		}
    		
    		cout << "The minimum value found is %f" << Min;
    		cout << "The maximum = " << Max;
    		fclose(fo);
    		return 0;
    		
    	}
    		system ("Pause");
    		return 0;
    	}
    	
    
    	
    
    
    
    
    
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    You need to say what the code is doing now, and what problems it has compared to the requirements. Also, ditch code you stole/borrowed and start writing from scratch using what you do know. This will be easier than manipulating code you don't have any familiarity with.

    We won't do your homework. We will be glad to help if you get started and run into trouble.

    -Lee
     
  3. thread starter macrumors 65816

    corywoolf

    Joined:
    Jun 28, 2004
    #3
    Thanks for the quick response. I wasn't intending anyone to complete this assignment for me. I don't understand how to get individual lines of integer numbers in from a text file and assign them variables.
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    You have a few options. You need an ifstream, which has a constructor that takes a filename to do the open for you. You can then use the extraction operator >> or getline. The conversion to an int you should be able to handle. Summing and tracking the line count should also be trivial using += and ++.

    -Lee
     
  5. thread starter macrumors 65816

    corywoolf

    Joined:
    Jun 28, 2004
    #5
    Thanks for the tips, this is the code I began creating from scratch before searching for a similar program online. So am I converting a string to an integer, because a char is only one character, correct?

    My professor is very hard to understand because of his accent and his TA is reluctant to explain too much. I am just trying to find examples of similar programs to figure out how to create this program. I have been reading through the file input/output chapter in my book and have found mostly examples of inputing names as strings. Thanks again for your help, it's much appreciated.

    Code:
    //******************************************************************************************
    // This program calculates the sum, average.
    // By Cory Woolf
    //******************************************************************************************
    
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <cctype>
    #include <fstream>
    
    using namespace std;
    
    int avg; // Average of Variables
    int sum; // Sum of variables
    
    int main () 
    {
    	ifstream inFile;
    	inFile.open("a3.txt"); // Open File
    			
    	
    	while (inFile>>i) // Part 1 Read values from file
    	{
    		 total= int i
    		
    		int AVE (int line);
    		{
    			cout << "The values are: " << num; // Display input values from file
    			cout << "The sum is: " << num ; // Sum
    			avg = 
    			cout << "The average is: " << num / 6; // Average
    			
    		}
    		
    	float MaxMin (int num) // Part 2
    		{
    			int max, min;
    			float avg;
    			max=0;
    			min=0;
    			
    			if (v)
    		
    		for (int i=0; i<count; i++)		{
    			int v;
    			int MAX;
    			MAX = v;
    			
    			
    			
    		inFile.close ();	
    		system ("Pause");
    		return 0;
    	}
    	
    
     
  6. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    Getline should get you a "C string" in an array you pass in. You can pass this to atoi to get an int. If you use >> with an int it would get you an int immediately instead of needing to convert explcitly.

    -Lee
     
  7. macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #7
    I would suggest you break this problem into smaller chunks, and work on them one at a time.

    Start by writing a program that takes 6 numbers from keyboard input (e.g. "enter value 1:" "enter value 2:", etc.) and calculates the values you need.

    You might even want to break that down further: How do you get an average? How do you keep track of a min/max value? Make sure you understand how all that logic works as you are writing this first program.

    Hint: there are several ways you can do this. You can read all of the values into memory first, then do all the calculations at the end. Or, you can keep "running totals" of at least some of the things while you are collecting the values. The latter will require fewer variables and sounds like what your prof is probably looking for.

    Once you've got that all working, start with a fresh slate and write a program that reads 6 values from a file and does nothing but print them out.

    Finally, put the two algorithms together, using the constraints of the assignment (e.g. put them into function calls instead of "main" programs) and you're done!
     
  8. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #8
    C/C++ does not allow for the embedding of functions within functions thus the following in not legal syntax:

    Code:
       
    int main()
    {
        return_type func1()
        {
            return result_type;
        }
    
        return_type func2()
        {
            return result_type;
        }
    
        return EXIT_SUCCESS;
    }
    
    Instead subroutines need to be defined ouside of functions thus:

    Code:
    return_type func1(parms p, ...)
    {
        return result_type;
    }
    
    return_type func2(parms p, ...)
    {
        return result_type;
    }
    
    int main()
    {
        return EXIT_SUCCESS;
    }
    
     
  9. macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #9
    If it helps for you, write the program as pseudocode before doing any sort of coding. I don't know C++ exactly but the point of Pseudocode is to give an initial logic structure.

    Get a piece of paper and pen. Or just type it up quickly.

    EG:

    Declare Variable Min Max Av Sum
    Declare Array Integers(6) *I'm storing the Integers as an array

    For Loop
    i = 1, a = 0, i < 7, i = i++ a = a++
    Read File Location 1 into Integers(0)
    End Loop

    ETC ETC

    ---

    It's better if you write your own code period, Especially when learning a language. It may seem harder but you reap the benefits afterwards.
     

Share This Page