#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Arrays and data manipulation

Member Posts: 1
I'm working on a program that deals with annual rainfall. I have two 1D arrays, one with the rainfall data and another with the months of the year. I need to find the average yearly rainfall, driest and wettest month, as well as driest and wettest 3 month periods. All this needs to be outputted to a file. I was trying to use a comparision operation but the only one I could come up with was way to long. Any help would be greatly appreciated.

Indiana

## Comments

• Member Posts: 44
: I'm working on a program that deals with annual rainfall. I have two 1D arrays, one with the rainfall data and another with the months of the year. I need to find the average yearly rainfall, driest and wettest month, as well as driest and wettest 3 month periods. All this needs to be outputted to a file. I was trying to use a comparision operation but the only one I could come up with was way to long. Any help would be greatly appreciated.
:
: Indiana
:
Not much scope for increasing speed here ,i guess.
But calculating the avg rainfall, finding the driest and wettest months shouldnt be tpoo difficult.
For calculating the 3 month periods,just start from the beggining of the array and add the next three, come back to the second index of the array, then add the next three.
I suppose you'll require two for loops, one for keeping a track of the starting month , and next loop for the three month addition.
'outer for starting month and inner for 3 cosecutive months.

-Shehjar

• Member Posts: 265
If you are careful, you can keep the indexes equal for both array, but I think there may be an easier answer here: classes (or structs).

class CMonth
{
public:
char[64] strMonth;
int nRain;
};

Now you need one array (using a template-based array). Don't know if you want to do this or not, but classes are the way to go if you can.
• Member Posts: 1
: I'm working on a program that deals with annual rainfall. I have two 1D arrays, one with the rainfall data and another with the months of the year. I need to find the average yearly rainfall, driest and wettest month, as well as driest and wettest 3 month periods. All this needs to be outputted to a file. I was trying to use a comparision operation but the only one I could come up with was way to long. Any help would be greatly appreciated.
:
: Indiana
:

what do you think about this:

double rainfall [12]; // months rainfall data

double avr = rainfall [0]; // average yearly rainfall

double dMonthIdx = 0; // index of driest month
double wMonthIdx = 0; // index of wettest month

double d3MonthIdx = 0; // start index of driest 3 months period
double w3MonthIdx = 0; // start index of wettest 3 months period

// temporary vars
double d3Temp = w3Temp = m3Temp = rainfall [0] + rainfall [1] + rainfall [2];

{

for (int i = 1; i < 12; i++){
avr += rainfall [i];

if (rainfall [i] > rainfall [dMonthIdx]){
dMonthIdx = i;
}
else
if (rainfall [i] < rainfall [wMonthIdx]){
wMonthIdx = i;
}

m3Temp += ((-rainfall [i - 1]) + rainfall [(i + 2) % 12]);

if (m3Temp > d3Temp){
d3MonthIdx = i;
d3Temp = m3Temp;
}
else
if (m3Temp < w3Temp){
w3MonthIdx = i;
w3Temp = m3Temp;
}
}

avr = avr / 12;
}

???

• USAMember Posts: 4,624 ✭✭

________ || http://forcoder.org || free ebooks and video tutorials about ( PHP, Python, JavaScript, PL/SQL, C++, Delphi, Visual Basic, Visual Basic .NET, Ruby, Go, Assembly, C#, Swift, Scratch, Java, R, Perl, MATLAB, C, Objective-C Rust, Lua, ABAP, Logo, VBScript, ML, F#, Dart, Crystal, Prolog, Erlang, Kotlin, Apex, Julia, FoxPro, SAS, Ada, Awk, Fortran, COBOL, Bash, Scheme, Lisp, Hack, Transact-SQL, Alice, Scala, LabVIEW, Clojure, D ) ________

Sign In or Register to comment.