this is simulated data on daily vehicle traffic crossing a bridge for one
week. From Monday through Sunday the daily vehicle counts were {986, 818, 638, 763,
992, 534, 683}. Create an input file named cars_data.dat containing these numbers.
Write a program that will read these values from the input file, and store the values in an
array. The single-dimension array of integers is to be a global variable. The program
should calculate the average daily traffic load of the bridge, and present information on
minimum and maximum traffic. Output should be to the screen in a format similar to the
following:
Day Vehicles
Monday 986
Tuesday 818
...
Sunday ...
Total Vehicles for the week:
Average daily vehicle count:
Day(s) with heaviest traffic count:
Day(s) with lightest traffic count:
Screen output should also be written to a file on the disk. Your program must use a
function solely for reading the data file into the memory array, a different function for
calculating the average, and yet a third function for output. Your program should be able
to accept input files with any values, and must be able to handle unusual situations (e.g.,
what if two days have the same number of cars?)

Recommended Answers

All 8 Replies

i'm not very familiar with arrays and the book i have isn't really helping. This is my start but i'm not sure how to go from here.

#include <iostream>
#include <fstream>
using namespace std;

void reading_data_to_array ();
void calculate_average ();
void output ();

int main()
{

ifstream infile("C:\\cars_data.dat")
#include <iostream>
#include <fstream>

using namespace std;

int reading_data_into_array (int);
void calculate_average ();
void output ();

const int N=7
int a[N]

int main()
{

	int reading_data_into_array(a[N]);
	

}

int reading_data_into_array(int a[N])
{
	ifstream infile("C:\\cars_data.dat")
	infile>>a[N];
	return a[N];
}


void output()

Help please

since the array is global it isn't necessary to pass it as a parameter.

line 16: just make that a function call reading_data_into_array(); line 21: make that a void function because it isn't necessary to return anything and delete the parameter because the array is global..

line 24: you need a while loop to read each of the numbers into the array

int i = 0;
while( infile >> a[i] )
    ++i;

line 25: delete it.

#include <iostream>
#include <fstream>

using namespace std;

void reading_data_into_array (int);
void calculate_average (int);
void output ();

const int N=7
int a[N]

int main()
{
	reading_data_into_array(a[N]);
	calculate_average (a[N]);
	output (a[N], 
	
}

void reading_data_into_array(a[N])
{
	int i=0
		
	ifstream infile("C:\\cars_data.dat")
	
	while (infile>>a[i])
		++i;

	
}

void calculate_average (a[N])
{
        int allnum=0;
	for (int i=0; i<=7; i++)
		{
			allnum += a[i]
		}
	average = allnum/7;
	
}

		
void output()
{
cout

shouldn't i make my functions return their values, other wise how will i use their values in my output function. And I don't understand how to make a global array.

void output()
{
cout<<"Day"<<setw(15)<<"Vehicles"<<endl;
cout<<"Monday"<<setw(7)<<a[0]<<endl;
cout<<"Tuesday"<<setw(6)<<a[1]<<endl;
cout<<"wednesday"<<setw(4)<<a[2]<<endl;
cout<<"Thursday"<<setw(5)<<a[3]<<endl;
cout<<"Friday"<<setw(7)<<a[4]<<endl;
cout<<"Saturday"<<setw(5)<<a[5]<<endl;
cout<<"Sunday"<<setw(7)<<a[6]<<endl;
cout<<"Total vehicles for the week: "<<allnum<<endl;
cout<<"Average daily vehicle count: "<<average<<endl;
cout<<"Day(s) with heaviest traffic count: "<<
cout<<"Day(s) with lightest trsffic count: "<<

see i need the values of a[N], allnum and average for my output
and does anyone know how i would calculate the heaviest and lightest days?

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void reading_data_into_array ();
void calculate_average (int&, int&);
void output (int, int);

const int N=7;
int a[N], allnum=0, average;

int main()
{
	reading_data_into_array ();
	calculate_average (allnum, average);
	output (allnum, average);
	
}

void reading_data_into_array ()
{
	int i=0;
	ifstream infile("C:\\cars_data.dat");
	while (infile>>a[i])
		++i;
	infile.close();
}

void calculate_average (int& allnum, int& average)
{
	for (int i=0; i<=7; i++)
		{
			allnum += a[i];
		}
	average = allnum/7;
}

		
void output(int allnum, int average)
{

	cout<<"Day"<<setw(15)<<"Vehicles"<<endl;
	cout<<"Monday"<<setw(7)<<a[0]<<endl;
	cout<<"Tuesday"<<setw(6)<<a[1]<<endl;
	cout<<"wednesday"<<setw(4)<<a[2]<<endl;
	cout<<"Thursday"<<setw(5)<<a[3]<<endl;
	cout<<"Friday"<<setw(7)<<a[4]<<endl;
	cout<<"Saturday"<<setw(5)<<a[5]<<endl;
	cout<<"Sunday"<<setw(7)<<a[6]<<endl;
	cout<<"Total vehicles for the week: "<<allnum<<endl;
	cout<<"Average daily vehicle count: "<<average<<endl;
	cout<<"Day(s) with heaviest traffic count: "<<endl;
	cout<<"Day(s) with lightest traffic count: "<<endl;

ofstream outfile("C:\\cars_data.out");
	outfile<<"Day"<<setw(15)<<"Vehicles"<<endl;
	outfile<<"Monday"<<setw(7)<<a[0]<<endl;
	outfile<<"Tuesday"<<setw(6)<<a[1]<<endl;
	outfile<<"wednesday"<<setw(4)<<a[2]<<endl;
	outfile<<"Thursday"<<setw(5)<<a[3]<<endl;
	outfile<<"Friday"<<setw(7)<<a[4]<<endl;
	outfile<<"Saturday"<<setw(5)<<a[5]<<endl;
	outfile<<"Sunday"<<setw(7)<<a[6]<<endl;
	outfile<<"Total vehicles for the week: 	"<<allnum<<endl;
	outfile<<"Average daily vehicle count: 	"<<average<<endl;
	outfile<<"Day(s) with heaviest traffic count: "<<endl;
	outfile<<"Day(s) with lightest traffic count: "<<endl;
	outfile.close();
}

Ok figured it out and it's all working but I don't know how to do heaviest and lightest traffic.. can anyone help? I also need it to be able to tell if there are more then one day with the heaviest/lightest traffic.

create two variables called heavest and lightest. Then create a loop like you did on line 33, but instead of adding up everything keep track of the heavest and lightest values. For example:

if( a[i] > heavest)
   heavest = a[i];

and do the same for lightest except of course your have to use < operator instead of >.

Be sure to initailize both of the variables to a[0] before starting the loop.

and must be able to handle unusual situations (e.g.,
what if two days have the same number of cars?)

For this you can do what Ancient Dragon said and find out the maximum/minimum value and then traverse the array once again and compare each element with the maximim/minimum value, if it is equal then simply print that day also.

Another alternative would be sorting the array in ascending order and then printing values from the beginning/end while two consecutive values are equal.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.