1 Replies - 209 Views - Last Post: 21 March 2013 - 05:39 AM Rate Topic: -----

#1 Millhouse17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-March 13

Arrays files and functions error calling array function

Posted 21 March 2013 - 05:13 AM

I am trying to create 2 functions, one that calculates and displays the average weekly food intake for 3 monkeys. (getAvg();)

Name Average weekly intake in pounds
Bonnie results
Clyde etc
Trigger etc

The other function will determine the monkey who ate the least amount of food in any one day and most amount.

Least amount: __monkeyname__ ate ___ pounds on Day #___
Most amount: ___monkeyname__ ate ___ pounds on Day #___

the test data that i read from the file food.txt is

45 33 55 66 77 88 100
88 77 66 55 44 33 22
44 55 00 66 77 88 99

I also can't figure out how to make the computer read in "00" to the array.

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

const int MONKEYS = 3;
const int DAYS = 7;

void getMonkeys(ifstream &file, string M[]);
void getFood(ifstream &file, double a[][7]);
void print(string M[], double b[][7]);

double getAvg();
double getMinMax();

int main()
{
	double mFood[MONKEYS][DAYS];
	string mNames[MONKEYS];

	ifstream inFile;
	ifstream inFile2;

	inFile.open("names.txt");
	getMonkeys(inFile, mNames);
	inFile.close();

	inFile2.open("food.txt");
	getFood(inFile2, mFood);
	inFile2.close();
	
	print(mNames, mFood);

	system("pause");
	return 0;
}

void getMonkeys(ifstream &file, string M[])
{
	string name_holder;
	int c = 0;

	while (file >> name_holder)
	{
		M[c] = name_holder;
		c++; 
	}
}

void getFood(ifstream &file, double a[][7])
{
	double num_holder;
	int row = 0, col = 0, input = 0;

	while (file >> num_holder)
	{
		a[row][col] = num_holder;
		input++;
		col++;

		if (input%DAYS == 0)
		{
			row++;
			col=0;
		}
	}
}

void print(string M[], double b[][7])
{

	cout << "Name" << "\t" << "    " << "Day 1\t" << "Day 2\t" << "Day 3\t"
		<< "  " << "Day 4" << "  " << "Day 5" << "  " << "Day 6" << "  " << "Day 7" << endl;
	cout << "-----" << "\t" << "    " << "-----" << "\t" << "-----" << "\t" << "-----" << "  "
		<< "-----" << "  " << "-----" << "  " << "-----" << "  " << "-----" << endl;

	for(int c=0;c<MONKEYS;c++)
	{
		cout<<setw(14)<<left<<M[c];

		for(int i=0;i<DAYS;i++)
		{
			if(b[i][c]==0)
				cout<<"0";
			cout<< b[c][i] << "     ";
		}
		cout<<endl;
	}
	cout << endl;
	cout << endl;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Arrays files and functions error calling array function

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3548
  • View blog
  • Posts: 10,986
  • Joined: 05-May 12

Re: Arrays files and functions error calling array function

Posted 21 March 2013 - 05:39 AM

What is the result you are seeing right now? What kind of help are you looking for? Are you expecting us to write the functions for you?

You need to be more descriptive than:

Quote

I am trying to create 2 functions
:
I also can't figure out how to make the computer read in "00" to the array.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1