Rewrite lab assignment 7 using pointer to allocate the array

Rewrite lab assignment 7 using pointer to allocate the array memory.

#include<iostream>

#include<fstream>

#include<iomanip>

using namespace std;

//Intiate Functions

int calculateAverageRainFall(int rainFall [], int size)

{

int sum=0;

for(int month=0; month

sum+=rainFall[month];

return sum/size;

  

}

void inputRainFall(int rainFall[], int size)

{

  

//Open the file

ifstream inputFile;

inputFile.open(\"rainfall.txt\");

  

//Initialize month counter

int month = 0;

  

//Read the monthly rainfall in the file

for(month=0; month

inputFile >> rainFall[month];

  

}

void classifyAndDisplayRainfall(int rainFall[], int months)

{

//Start to List Month & Store in String

string monthNames[] = {\"January\",\"Febuary\",\"March\",\"April\",\"May\",

\"June\",\"July\",\"August\",\"September\",\"October\",\"November\",\"December\"};

  

int avgRainFall=calculateAverageRainFall(rainFall,months);

int highestRainFall,lowestRainFall;

int highestRainFallIndex,lowestRainFallIndex;

highestRainFall=lowestRainFall=rainFall[0];

highestRainFallIndex=lowestRainFallIndex=0;

  

//Formatted Output Columns

cout<<\"\ Month\\tRainfall(mm)\\tClassification\ \";

cout<<\"-------\\t------------\\t------------------\ \";

//Start For-Loop

for(int month=0; month

{

cout<<(month+1)<<\"\\t\"<

if((avgRainFall+(avgRainFall*0.2))<=rainFall[month])

cout<<\"Rainy\ \";

  

else if(avgRainFall>rainFall[month]+(avgRainFall*0.25))

cout<<\"Dry\ \";

else

cout<<\"Average\ \";

if(highestRainFall

{

highestRainFall=rainFall[month];

highestRainFallIndex=month;

}

if(lowestRainFall>rainFall[month])

{

lowestRainFall=rainFall[month];

lowestRainFallIndex=month;

}

  

}//End For-Loop

  

///Output Information Back to User

cout<<\"\ The year\'s average monthly rainfall was \"<

cout<

cout<

}

//Start Main

int main()

{

int rainFall[12];

int size=12;

  

inputRainFall(rainFall,size);

  

classifyAndDisplayRainfall(rainFall,size);

return 0;

  

}//End Main

Solution

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

using namespace std;

//Intiate Functions
int calculateAverageRainFall(int *rainFall, int size)
{
   int sum=0;
   for(int month=0; month<size; ++month)
       sum+=rainFall[month];
   return sum/size;
}

void inputRainFall(int *rainFall, int size)
{
   //Open the file
   ifstream inputFile;
   inputFile.open(\"rainfall.txt\");

   //Initialize month counter
   int month = 0;

   //Read the monthly rainfall in the file
   for(month=0; month<size; ++month)
       inputFile >> rainFall[month];

}

// this is how you pass around array pointers
void classifyAndDisplayRainfall(int *rainFall, int months)

{

   //Start to List Month & Store in String
   string monthNames[] = {\"January\",\"Febuary\",\"March\",\"April\",\"May\", \"June\",\"July\",\"August\",\"September\",\"October\",\"November\",\"December\"};

   int avgRainFall=calculateAverageRainFall(rainFall,months);
   int highestRainFall,lowestRainFall;
   int highestRainFallIndex,lowestRainFallIndex;
   highestRainFall=lowestRainFall=rainFall[0];
   highestRainFallIndex=lowestRainFallIndex=0;

   //Formatted Output Columns
   cout<<\"\ Month\\tRainfall(mm)\\tClassification\ \";
   cout<<\"-------\\t------------\\t------------------\ \";
   //Start For-Loop
  
   for(int month=0; month<months; ++month)
   {
       // uncomment this once you have fixed the below cout statement
       //cout<<(month+1)<<\"\\t\"<
           if((avgRainFall+(avgRainFall*0.2))<=rainFall[month])
               cout<<\"Rainy\ \";
           else if(avgRainFall>rainFall[month]+(avgRainFall*0.25))
               cout<<\"Dry\ \";
           else
               cout<<\"Average\ \";
           if(highestRainFall<rainFall[month])
           {
               highestRainFall=rainFall[month];
               highestRainFallIndex=month;
           }

           if(lowestRainFall>rainFall[month])
           {
               lowestRainFall=rainFall[month];
               lowestRainFallIndex=month;
           }

   }//End For-Loop

   // uncomment this once you have fixed the below cout statement
   /*
   ///Output Information Back to User
   cout<<\"\ The year\'s average monthly rainfall was \"<
       cout<
       cout<
       */
}

//Start Main
int main()
{
   // this is how you initialize an int array with pointers
   int *rainFall = new int[12];
   int size=12;

   inputRainFall(rainFall,size);
   classifyAndDisplayRainfall(rainFall,size);
   return 0;
}//End Main

Rewrite lab assignment 7 using pointer to allocate the array memory. #include<iostream> #include<fstream> #include<iomanip> using namespace st
Rewrite lab assignment 7 using pointer to allocate the array memory. #include<iostream> #include<fstream> #include<iomanip> using namespace st
Rewrite lab assignment 7 using pointer to allocate the array memory. #include<iostream> #include<fstream> #include<iomanip> using namespace st
Rewrite lab assignment 7 using pointer to allocate the array memory. #include<iostream> #include<fstream> #include<iomanip> using namespace st
Rewrite lab assignment 7 using pointer to allocate the array memory. #include<iostream> #include<fstream> #include<iomanip> using namespace st

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site