EECS168 09:Homework6

From ITTC
Jump to: navigation, search
Navigation
Home
Information

Syllabus
Schedule
Lecture Notes
Exam Reviews

Classwork

Labs
Homework
Submitting Work

Announcement

  • To open a file listed by the getDir() function, you need to append the directory to the file name. Otherwise, your program would not be able to open the file, as it would not have the correct path to the file. To append two strings together you can simply use the + operator in between them.

Overview

You are going to write a program that reads the list of average temperatures for days of a month from a text file. The program stores the temperatures in an array. Then it identifies the Maximum and Average of the temperatures for each month, and writes the Maximum and Average numbers into an output file. This process is repeated for all the month text files listed in a given input directory.

DUE DATE: The electronic submission of your homework must be received before 11:59 pm, Tuesday, 7th of April.

Problem Description

The input files should be named by month names. For example March.txt or April.txt. Each file will have a maximum of 31 space delimited integers. An example input file would have the following content:

36 54 76 65 63 64 38 32 30 28 37 39 34 23 39 42 43 48 49 58 53 56 62 53 49 40 33 37 47 57 60

Create a folder called InputFiles and put all your input files in that folder. You are required to have at least two input files in this folder.

List all the files in a directory

Use the following getDir() function to automate getting the list of all files in a directory. The following code also shows you how to use the getDir() function. Try the code with the current directory to see how it works. Remember that later on in the complete program you might need to exclude directories listed by the getDir() function. For example . and .. directories. You can use a simple if statement to do so.


#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>

using namespace std;

typedef vector<string> stringVector;  // define a vector of string values

// CREATES A LIST OF FILES IN THE GIVEN DIRECTORY
int getDir (string dir, stringVector &files)
{
	DIR *dp;
	struct dirent *dirp;
	
	if((dp  = opendir(dir.c_str())) == NULL) {
		cout << "Error(" << errno << ") opening " << dir << endl;
		return errno;
	}
	
	while ((dirp = readdir(dp)) != NULL)  {
		files.push_back(string(dirp->d_name));
	}
	closedir(dp);
	return 0;
}


int main()
{
    string dir; 
    cin>>dir;
    // e.g. to list the files in the current directory you could input . 
    stringVector files = stringVector();
	
    getDir(dir,files);
	
    for (unsigned int i = 0; i < files.size(); i++) {
        cout << files[i] << endl;
    }
    return 0;
}

Program Requirements

Name your source code file arrayOperations.cpp. In writing the program you should satisfy the following requirements.

  • The program should prompt the user for the path of input directory.
    • When running the program, if you want to give the path to a folder in one level higher directory than where your program is currently running from, you can enter ../YourDirectoryName. You can do this in several hierarchical levels. For example ../../InputFiles will take you two levels higher from the current directory and look for a folder called InputFiles. If the input folder is in the current directory as your program is running from, you just need to type in the name of the file at the prompt e.g. InputFiles/
  • The program should use an integer array to store the list of numbers read from an input file. The maximum size of this array will be 31.
  • The program should write a list of Maximum temperature of all the input files into a text file called MaxsOfArrays.txt
  • The program should write a list of Average temperature of all the input files into a text file called AvgsOfArrays.txt
  • The program should also display the Average and Maximum for each month on the screen.
  • Make sure that any opened file streams are closed before the end of the program.
  • You will be writing 5 functions with the specified names. Here is a list of them:
    • OpenInputFile()
      • Return type: void
      • Arguments: address of an input file stream (ifstream&) , filename(string)
      • Description: Opens a given input file
    • OpenOutputFile()
      • Return type: void
      • Arguments: address of an output file stream (ofstream&) , filename(string)
      • Description: Opens a given output file
    • ReadArray()
      • Description: Reads in space delimited numbers from a file to an array
      • Return type: size of the array (int)
      • Arguments: input file stream address (ifstream&), address of the array (int[])
    • CalcAvg()
      • Description: finds average of the numbers in an array
      • Return Type: average temperature of a month (float)
      • Arguments: array of temperature values (integer array), array size
    • CalcMax()
      • Description: finds maximum of the numbers in an array
      • Return Type: max temperature of a month (float)
      • Argument: array of temperature values (integer array), array size

Hints for writing the program

Here are a few points that might be helpful to you in writing the program:

  • To pass an array by reference, you do not need to use the & operator, just use the array name.
  • You can use the at() operator to get the character at a specific position in a string.
    • To read more about the at() operator please refer to page 474 of your book.
    • You might take use of this option to identify the directory names listed by the getDir function and exclude them.
  • For outputting the name of month on the screen, you need to omit the .txt part from the related file name. For example if you have March.txt as the file name, you want to print out March. To do this, you can use some of the member functions from the string class such as size() and substr(). please refer to chapter 8, Display 8.7 to read more about these functions.

Example Program Execution

The following is an example of what a program run should look like. The user's input is shown in bold.

Enter the directory containing the input files: InputFiles/

________________________

Max temperature in March has been: 99

Average temperature in March has been: 68

________________________

Max temperature in January has been: 39

Average temperature in January has been: 21

Notes

  • Your source code files should have comments at the top with:
    • your name
    • the class ("EECS 168")
    • the assignment name (i.e., "Homework 6")
    • the date
    • an explanation of what the code is supposed to do
  • You should have comments in the code that explain what the code is doing.


Submission

The files you should submit electronically (in a tarball) are:

  • ArrayOperations.cpp : The program
  • testing.txt: The testing you did on your finished ArrayOperations.cpp program.
  • InputFiles: The sub-folder that includes all your input files.
  • The subject of your e-mail should be '[EECS 168] Homework 06'