need 2D array help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krunk
    New Member
    • Apr 2007
    • 2

    need 2D array help

    This is my code. I am to read in numbers from a file which is my nums.txt.
    Then we are to have the user choose what row or colum they would like to work with. I have two different functions for working with a row or column and we are suppose to find the min, max and avg of the given column/row.

    I am having my problem on line 50 and 57.
    statisticsR(x[i][j]); - which says invalid conversion from 'int' to 'int(*)[200]'
    if anyone knows what im doing wrong please help




    Code:
    #include<iostream>
    #include<fstream>
    
    using namespace std;
    ifstream fin;
    #define max1 100
    #define max2 200
    void display();
    int intalize();
    int statisticsR (int x[max1][max2]);
    int statisticsC (int x[max1][max2]);
    
    
    int main() {
    	
    	display();
    	
    	
    	
    	return 0;
    	
    }
    int intalize(){
    	int i,j;
    	int x[max1][max2];
    	fin.open("nums.txt");
    	if (!fin) cout << "file did not open";
    	while(!fin.eof()){
    		for(j=0; j<4; j++){
    		fin >> x[i][j];
    		return x[i][j]; }
    	    cout<< endl;
    		i++;
    }
    }
    
    void display(){
    	char a, r, c;
    	int num, i, j;
       
    	cout<< "please enter filename."<<endl;
    	intalize();
    	cout << "would you like to work with rows or columns?" <<endl;
    	cin>>a;
    	if(a ==r) {
    		cout << "what row?" <<endl;
    		cin >>num;
    		if(num>100) cout<< "number exceeds the array";
    		i=num;
    		statisticsR(x[i][j]);
    	}
    	if (a == c) {
    		cout << "what column?" << endl;
    		cin >>num;
    		if (num>200) cout << "number exceeds the array";
    		j=num;
    		statisticsC(x[i][j]);
    	}
    	if (a!=r||a!=c) cout << "please choose again" << endl;
    }
    int statisticsR (int x[max1][max2]){
    	int i, j,max, min, sum=0;
    	double avg=0;
    	
    	for( j=1; j<200;j++){
    		if (x[i][j]> x[i][j-1]) max= x[i][j];
    		if (x[i][j]< x[i][j-1]) min= x[i][j];
    		sum=sum+ x[i][j];
    	}
    	avg=sum/j;
    	cout<< "max: " <<max <<endl;
    	cout<< "min: "<< min<< endl;
    	cout <<"avg: "<<avg<<endl;
    	return avg;
    }
    int statisticsC (int x[max1][max2]){
    	int j, i,max, min, sum=0;
    	double avg=0;
    	
    	for( i=1; i<100;i++){
    		if (x[i][j]> x[i-1][j]) max= x[i][j];
    		if (x[i][j]< x[i-1][j]) min= x[i][j];
    		sum=sum+ x[i][j];
    	}
    	avg=sum/i;
    	cout<< "max: " <<max <<endl;
    	cout<< "min: "<< min<< endl;
    	cout <<"avg: "<<avg<<endl;
    	return avg;
    Last edited by Ganon11; Apr 25 '07, 06:56 PM. Reason: code tags added
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    You call your functions like this:

    Code:
    statisticsR(x[i][j]);
    which passes the function x[i][]j which is an integer value. But your functions are expecting a 2d array, not an integer. Try

    Code:
    statisticsR(x);

    Comment

    • nmadct
      Recognized Expert New Member
      • Jan 2007
      • 83

      #3
      What Ganon11 said, plus it's good style to use capital letters for your symbolic constants (MAX1, not max1) so that they don't look like variable names.

      Comment

      • krunk
        New Member
        • Apr 2007
        • 2

        #4
        thanks a lot. It fixed the errors in my program

        Comment

        Working...