Arrays and functions in a program

Help with a program using arrays and functions

Page 1 of 1

4 Replies - 13309 Views - Last Post: 26 November 2008 - 10:20 PM Rate Topic: -----

#1 gamebrain89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-November 08

Arrays and functions in a program

Posted 26 November 2008 - 09:24 PM

Ok, I am a fairly new guy to C++ programming, though I think I am doing ok. My problem is understanding how to use arrays and functions together. This is the program I am working on

Write a program that lets the user enter the total rainfall for each of 12 months into an array of doubles. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts.

I am fairly sure I do not have it entirely correct based on reading through my book, but I cant pinpoint exactly what I am doing wrong, which is probably pretty obvious. I am getting quite a few different errors, but for the most part they are


I have quite a few invalid type errors, as well as invalid conversion, and too many arguements and the like. Here is my code.

 // Chapter 7, Programming Challenge 2
// RainFall Statistics
#include<iostream>
#include <iomanip>
using namespace std;

// Constant for the number of months
const int NUM_MONTHS = 12;

// Function prototypes
double getTotal(double [], int);
double getAverage(double [], int);
double getLargest(double [], int, int &);
double getSmallest(double [], int, int &);


double getTotal(int rainFall,double NUM_MONTHS[])
{
double total = 0;
for (int count = 0; count < NUM_MONTH; count++)
total += NUM_MONTH[count];
return total;
}

double getAverage(int rainFall,double NUM_MONTH[])
{getTotal(rainFall,NUM_MONTH)
average= total/NUM_MONTHS;
return average;
}

double getHighest(int rainFall, double NUM_MONTHS[]) //I left out the subScript peice as I was not sure how to procede with that;
{
double largest;
largest = NUM_MONTHS[0];

for ( int month = 1; month <= NUM_MONTHS; month++ ){
					  if ( values[month] > largest ){
				  largest = values[month];
return largest;

			 } 



double getSmallest(int rainFall, double NUM_MONTHS[])
{
double smallest;
smallest = NUM_MONTHS[0];

for ( int month = 1; month <= NUM_MONTHS; month){
					  if ( values[month] < smallest ){
				  smallest = values[month];
return smallest;

			 } 
	
int main()
{
   // Array to hold the rainfall data
   double rainFall[NUM_MONTHS];

   // Get the rainfall for each month.
   for (int month = 0; month < NUM_MONTHS; month++)
   {
	  // Get this month's rainfall.
	  cout << "Enter the rainfall (in inches) for month #";
	  cout << (month + 1) << ": ";
	  cin >> rainFall[month];
	  
	  // Validate the value entered.
	  while (rainFall[month] < 0)
	  {  
		 cout << "Rainfall must be 0 or more.\n"
			  << "Please re-enter: ";
		 cin  >> rainFall[month];
	  }
   }
   
   // Set the numeric output formatting.
   cout << fixed << showpoint << setprecision(2) << endl;
   
   // Display the total rainfall.
   cout << "The total rainfall for the year is ";
   cout << getTotal(rainFall, NUM_MONTHS)
		<< " inches." << endl;
   
   // Display the average rainfall.
   cout << "The average rainfall for the year is ";
   cout << getAverage(rainFall, NUM_MONTHS)
		<< " inches." << endl;

   // Now display the largest & smallest amounts.
   // The subscript variable will be passed by reference
   // the the getLargest and getSmallets functions.
   int subScript;

   // Display the largest amount of rainfall.
   cout << "The largest amount of rainfall was ";
   cout << getLargest(rainFall, NUM_MONTHS, subScript)
		<< " inches in month ";
   cout << (subScript + 1) << "." << endl;

   // Display the smallest amount of rainfall.
   cout << "The smallest amount of rainfall was ";
   cout << getSmallest(rainFall, NUM_MONTHS, subScript)
		<< " inches in month ";
   cout << (subScript + 1) << "." << endl << endl;
   return 0;
}

			  



I really want to understand exactly what I am doing wrong, so I thank you for your help in advance.

This post has been edited by gamebrain89: 26 November 2008 - 09:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Arrays and functions in a program

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Arrays and functions in a program

Posted 26 November 2008 - 10:02 PM

Ok you had a lot of silly errors that you need to watch out for.

1) Make sure that your prototypes match the parameters and types you have defined in the functions themselves. Remember the prototypes are to tell the compiler what is coming up so it can get ready for it. You don't want to throw a curve ball by changing the signature of a function.

2) Also you had getHighest defined and not getLargest. Make sure you match the names.

3) Notice that rainfall holds doubles, not ints. So you can't pass rainfall as an array of integers. Also you need double brackets to show that it is an array.

4) You got yourself mixed up between an array called NUM_MONTH and your constant NUM_MONTHS. You never needed NUM_MONTH since all your values were in rainfall.

5) Make sure you check for semicolons. You forgot one after getTotal in your getAverage function. That line has been removed anyways in this updated code below. Just saying to keep an eye out for those.

6) You forgot closing curly braces on your functions getHighest (aka getLargest) and getSmallest. Make sure that all closing braces match all opening curly braces.

7) Make sure you define all variables and array names before you use them. In getaverage you didn't define total or average before attempting to use them. In getHighest and getSmallest you also didn't define "values". No matter, this is actually suppose to be rainfall again because that is where your values are.

That is a majority of them. Here is your code fixed up.

// Chapter 7, Programming Challenge 2
// RainFall Statistics
#include<iostream>
#include <iomanip>
using namespace std;

// Constant for the number of months
const int NUM_MONTHS = 12;

// Function prototypes
// These must match your signatures exactly. Since you define a constant
// you only need to provide the array. Also you defined getLargest and getHighest
// so make sure you check your functions names to see if they match.
// Prototypes are also only needed if you don't define the function BEFORE main.
double getTotal(double []);
double getAverage(double []);
double getLargest(double [], int &);
double getSmallest(double [], int &);

    
int main()
{
   // Array to hold the rainfall data
   double rainFall[NUM_MONTHS];

   // Get the rainfall for each month.
   for (int month = 0; month < NUM_MONTHS; month++)
   {
      // Get this month's rainfall.
      cout << "Enter the rainfall (in inches) for month #";
      cout << (month + 1) << ": ";
      cin >> rainFall[month];
      
      // Validate the value entered.
      while (rainFall[month] < 0)
      {  
         cout << "Rainfall must be 0 or more.\n"
              << "Please re-enter: ";
         cin  >> rainFall[month];
      }
   }
  
   // Set the numeric output formatting.
   cout << fixed << showpoint << setprecision(2) << endl;
  
   // Display the total rainfall.
   cout << "The total rainfall for the year is ";
   cout << getTotal(rainFall)
        << " inches." << endl;
  
   // Display the average rainfall.
   cout << "The average rainfall for the year is ";
   cout << getAverage(rainFall)
        << " inches." << endl;

   // Now display the largest & smallest amounts.
   // The subscript variable will be passed by reference
   // the the getLargest and getSmallets functions.
   int subScript = 0;

   // Display the largest amount of rainfall.
   cout << "The largest amount of rainfall was ";
   cout << getLargest(rainFall, subScript)
        << " inches in month ";
   cout << (subScript + 1) << "." << endl;

   // Display the smallest amount of rainfall.
   cout << "The smallest amount of rainfall was ";
   cout << getSmallest(rainFall, subScript)
        << " inches in month ";
   cout << (subScript + 1) << "." << endl << endl;
   return 0;
}

// Get Total, takes array and reads values from the array to get total.
// It goes for 0 to 11 (remember arrays start at 0)
double getTotal(double rainFall[])
{
	double total = 0;
	for (int count = 0; count < NUM_MONTHS; count++)
	total += rainFall[count];
	return total;
}

// Get the total and calculate the average
double getAverage(double rainFall[])
{
	double average = 0.0;
	average= getTotal(rainFall)/NUM_MONTHS;
	return average;
}

double getLargest(double rainFall[], int &subscript)
{
	double largest;
	largest = rainFall[0];

	// Again start at zero and go until we hit 12, also remember to increment
	for ( int month = 0; month < NUM_MONTHS; month++ ){
         if ( rainFall[month] > largest ){
              largest = rainFall[month];
			  subscript = month;
         }
	}

	return largest;
}



double getSmallest(double rainFall[],  int &subscript)
{
	double smallest;
	smallest = rainFall[0];

	for ( int month = 0; month < NUM_MONTHS; month++){
		if ( rainFall[month] < smallest ){
             smallest = rainFall[month];
			 subscript = month; // Notice in each loop we record the month to our subscript
		}
	}
	return smallest;
}



I have added some in code comments to show you a few of the problems and corrected your syntax errors. Please be sure to read through this so you know what is going on.

Enjoy!

"At DIC we be rainfall collecting code ninjas... we are environmentalists... on non environmentally friendly machines" :snap:
Was This Post Helpful? 1
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Arrays and functions in a program

Posted 26 November 2008 - 10:02 PM

Example:

#include <iostream>
#include <time.h>
using namespace std;

//all functions take the array and size as parameters
void bubbleSort(double*, const int); //must pass by ref, otherwsie chances are only made to hsallow copy
double totalRain(double[], const int);

int main()
{
	const int NUM_MONTHS = 12;
	double rainfall[NUM_MONTHS];
	double highest, lowest, total = 0;
	srand(time(NULL));

	for(int i =0; i < NUM_MONTHS; i++)
	{
			rainfall[i] = rand() % 20; //0-19 inches, just example
	}

	//display before calcs
	for(int i =0; i < NUM_MONTHS; i++)
	{
			cout << rainfall[i] << " ";
	}
	bubbleSort(rainfall, NUM_MONTHS);

	cout << "sorted rainfall array:\n";
	for(int i =0; i < NUM_MONTHS; i++)
	{
			cout << rainfall[i] << " ";
	}

	highest = rainfall[11];
	lowest = rainfall[0];
	total = totalRain(rainfall, NUM_MONTHS);
	//could also just do cout << "Highest: " << rainfall[11];, etc...
	cout << "Highest: " << highest << endl;
	cout << "Lowest: " << lowest << endl;
	cout << "Total rainfall: " << total;

	//random asignment for easy testing right now

	return 0;
}

void bubbleSort(double* rain, const int size)
{
	double temp = 0;
	for (int i = 0; i <= size; i++)
	{
		for(int j = 1; j < size; j++)
		{
			if (rain[j-1] > rain[j])
			{
				temp = rain[j-1];
				rain[j-1] = rain[j];
				rain[j] = temp;
			}
		}
	}
}

double totalRain(double rain[], const int size)
{
	double total = 0;
	for (int i = 0; i < size; i++)
	{
		total += rain[i];
	}

	return total;
}



Rather then have separate functions for highest and lowest, simply sort the array of data, arr[0] is the lowest and arr[size-1] is the highest.
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Arrays and functions in a program

Posted 26 November 2008 - 10:03 PM

Martyr2 and KYA covered all the things I did, but better! B)

This post has been edited by n8wxs: 26 November 2008 - 10:06 PM

Was This Post Helpful? 0
  • +
  • -

#5 reaper7861  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 17-November 08

Re: Arrays and functions in a program

Posted 26 November 2008 - 10:20 PM

did not realize everyone had already posted lol

This post has been edited by reaper7861: 26 November 2008 - 10:21 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1