Hi im trying to calculate the average of an inputfile using a 2d array. I tried to calculate the average per row and add them all together but my code gives me an average of zero. I was wondering if anyone can see what im doing wrong or a better way to do it. Thanks in advance for your help.

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
const int NROW=10;
const int NCOLS=7;
double average=0.0;
string str;
double total=0;
double average1=0,average2=0,average3=0,average4=0,average5=0,average6=0;
double average7=0,average8=0,average9=0,average10=0;
double total1=0,total2=0,total3=0,total4=0,total5=0,total6=0,total7=0;
double total8=0,total9=0,total10=0;
int array[NROW][NCOLS];


ifstream inputFile("power1.txt");
    if(!inputFile){
        cout << "Error the file power1.txt does not exist.\n";
        exit(1);
    }

while(getline(inputFile,str)){ //reading in each line from the file
        for(int i=0;i<NROW; i++){
            total1= total1 + array[0][i];
            average1=(double) total/10;
        }
        for(int i=0;i<NROW; i++){
            total2= total2 + array[1][i];
            average2 =(double) total/10;
        }
        for(int i=0; i<NROW; i++){
            total3=total3 + array[2][i];
            average3=(double) total/10;
        }
        for(int i=0; i<NROW; i++){
            total4=total4 +array[3][i];
            average4=(double) total/10;
        }
        for(int i=0; i<NROW; i++){
            total5=total5+array[4][i];
            average5=(double) total/10;
        }
        for(int i=0; i<NROW; i++){
            total6=total6+array[5][i];
            average6=(double) total/10;
        }
        for(int i=0;i<NROW; i++){
            total7=total7+array[6][i];
            average7=(double) total/10;
        }
        for(int i=0; i<NROW; i++){
            total8=total8+array[7][i];
            average8=(double) total/10;
        }
        for(int i=0;i<NROW; i++){
            total9=total9+array[8][i];
            average9=(double) total/10;
        }
        for(int i=0; i<NROW; i++){
            total10=total10+array[9][i];
            average10=(double) total/10;
        }
    }
    cout << endl;
    average=((average1+average2+average3+average4+average5+average6+average7+average8+average9+average10))/10;
        cout << "The average power output is "<< average<< endl;;

inputFile.close();
}

i dont see where you actually populated the array.

First, as Crutoy as responded, you want to populate the array.

Use nested for-loops. A generic one you may find would include something similar to this.

/*
	Purpose: thread347853 - filling a multidimensional array
	Name: Saith	
	Date: 2/17/11
	*/

#include<iostream>
using namespace std;


int main() {

	const int vertical = 2;
	const int horizontal = 2;
	int list[vertical][horizontal];

	for(int index = 0; index < vertical; index++){		// will fill your array vertically
		for(int index2 = 0; index2 < horizontal; index2++){		// fill your array horizontally
			cout << "Please input [" << index << "]["<< index2<<"]" << endl;
			cin >> list[index][index2];
		}
	}
	cout << "OUTPUT "<< endl;
	// And to output them, you would do the same thing.

	for(int index = 0; index < vertical; index++){		
		// will output your array vertically

		for(int index2 = 0; index2 < horizontal; index2++){		
			// output your array horizontally
			
			cout << list[index][index2] << " ";
		}
		cout << endl;		
		// as the index increases (vertically) a new line will be make for the next set of numbers
	}

	return 0;
}

If you compile and test this code, it may help you understand how/why multi-dimensional work. You can really cut down on the amount of coded lines. Imagine, if not 10x10 but 1000 x 1000, trying to code in for each possibility. Yeah. Good luck on that one! Good luck to you on this current project.

-Saith

Thank you guys. Saith I was playing with the code you posted and I think I understand how to set up an array much better. I redid my code and it now displays the correct output, but in order to calculate the average would I set up two more for loops and add the totals together.

for(int i=0; i<SIZE; i++){
       for(int j=0; j<SIZE; j++){
            total= total +array[i][j];
            average = (double) total/70;
         }
}

The way that I had this working in my mind was that each time the loop ran it would add total, starting at 0, to the value at array[j] and would keep a running total, and then divide this number by either the total number of numbers or the number of values per row, not quite sure. But I don't know if this will work. Any help would be appreciated.

Matt

Ok, I got everything figured out and working. Thanks for helping me out.

Matt

I don't see why you would be calculating the average until BOTH for loops end, that would defeat the purpose. you could do

int i, j; // define variables here so they are in the scope and can be used for average
for(i=0; i<SIZE; i++){
       for(j=0; j<SIZE; j++){
            total = total + array[i][j]; // add to total
         }
}
average = (double) total/(i*j);

Yeah that's how i finally got it to work, thank you for taking the time to help.

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.