Thread: Array trouble

  1. #1
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149

    Array trouble

    Ok,
    I have a 2d array, and am reading the numbers from a file. I have to display them, and display the average of the sum of the columns and rows.
    I can display them, but i can't get the sum correct. It comes up with a wierd number.
    The part in bold is where i compute the sums of each row and column.
    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    void Compute_Sums(int Array1[][3], int RowSums[], int Colsums[]);
    void Compute_Avg(int RowSums[], int Colsums[], float RowAvg[], float ColAvg[]);
    void Print(int RowSums[], int Colsums[], float RowAvg[], float ColAvg[]);
    
    void main()
    {
    	ifstream Array_Info;
    	Array_Info.open("A:IntFile.DAT");	
    
    	int Array1[7][3], RowSums[7], Colsums[3];
    	float RowAvg[7], ColAvg[3];
    
    	Compute_Sums(Array1, RowSums, Colsums);
    	Compute_Avg(RowSums, Colsums, RowAvg, ColAvg);
    	Print(RowSums, Colsums, RowAvg, ColAvg);
    	
    }
    
    void Compute_Sums(int Array1[][3], int RowSums[], int Colsums[])
    {
    	int row;
    	int column;
    	row = 0;
    	column = 0;
    		
    	for(column = 0; column < 3; column++)
    		Colsums[column] = 0;
    			
    	for(column = 0; column < 3; column++)
    		for(row = 0; row < 7; row++)
    			Colsums[column] = Colsums[column] + Array1[row][column];
    
    	for(row = 0; row < 7; row++)
    		RowSums[row] = 0;
    
    	for(row = 0; row < 7; row++)
    		for(column = 0; column < 3; column++)
    			RowSums[row] = RowSums[column] + Array1[row][column];
    		
    }
    void Compute_Avg(int RowSums[], int Colsums[], float RowAvg[], float ColAvg[])
    {
    	int row;
    	int column;
    	row = 0;
    	column = 0;
    
    	for(row=0; row<7; row++)
    		RowAvg[row]=RowSums[row]/3.0;
    	
    	for(column=0; column<3; column++)
    		ColAvg[column]=Colsums[column]/7.0;
    		
    }
    void Print(int RowSums[], int Colsums[], float RowAvg[], float ColAvg[])
    {
    	ifstream Array_Info;
    	Array_Info.open("A:IntFile.DAT");
    	int Array1[7][3];
    		
    	
    	cout<<"       "<<"Fish1"<<"  "<<"Fish2"<<"  "<<"Fish3"<<endl<<endl;
    	for(int row=0; row<7; row++)
    	{
    				
    		cout<<"lake "<<row+1;
    		for(int column=0; column<3; column++)
    		{
    			Array_Info>>Array1[row][column];
    			cout<<setw(6)<<Array1[row][column]<<' ';
    		}
    		cout<<endl;
    	}
    	cout<<endl<<Colsums[1]<<endl;
    }
    These are the numbers (not all are used, only 21).
    8 27 33 14 81 146 305
    249 412 71 226 4 144 55
    94 493 133 265 788 240
    380 117 88 25 60

    I output "Colsums[1]" to test to see if the sum was correct. It comes up with a wierd number.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> reading the numbers from a file
    You are reading in the numbers during the Print function, which isn't called until after the sums are calculated.

  3. #3
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    aww, i didn't even realize that. Thank you.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void main()
    *faints*
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    > void main()
    *faints*
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    Was this intentional?
    Code:
    RowSums[row] = RowSums[column] + Array1[row][column];
    To avoid that kind of problem/ambiguity, you can write it like this:
    Code:
    RowSums[row] += Array1[row][column];
    You can do this at the variable's initialization:
    Code:
    int Colsums[3];
    
    	for(column = 0; column < 3; column++)
    		Colsums[column] = 0;
    ->
    Code:
    int Colsums[3] = {0, 0, 0};
    If you leave off elements, 0 is implied, so you could use
    Code:
    int Colsums[3] = {0};
    Or you could use memset (in <cstring>) instead of a for loop.

    I don't think you use anything from <string>, nor <cmath> for that matter.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble moving lines up in 2D array
    By powell5487 in forum C Programming
    Replies: 5
    Last Post: 03-03-2009, 12:54 PM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  3. trouble creating an array
    By s_ny33 in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2005, 11:18 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM