Thread: Adding Columns and Rows..Help!

  1. #1
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    Adding Columns and Rows..Help!

    I had a hard time adding the values stored in each row..as a Total- I figured that out. My question now is, how do I add the values stored in each column? You will see the section in bold. Thanks.

    Code:
    #include<iomanip.h>
    #include<fstream.h>
    #define Size 4
    #define College 7
    #include<stdlib.h>
    
    int C, I;
    
    void GetQuan( int Quan[Size][College])
      { for(C=0;C<Size;C++)
    		 {for(I=0;I<College;I++)
    			 {cout<<"\n Enter element Quan["<<C<< "]["<<I<<"]: "; cin>>Quan[C][I];}
    
    		  }  //for
    		//for
    	}//main
    void PrintData(int Quan[Size][College])
     {for(C=0;C<Size;C++)
    	  {for(I=0;I<College;I++)
    		cout<<Quan[C][I]<<" ";
    		cout<<endl;
    	  }
     }
    
    void StoreData(int Quan[Size][College])
     {  ofstream Inventry("Inventry.dat");
    	for(C=0;C<Size;C++)
    	  {for(I=0;I<College;I++)
    		Inventry<<Quan[C][I]<<" ";
    		Inventry<<endl;
    		}
     }
    
    
    void ReadData(int Quan[Size][College])
     { ifstream Invntry1("Inventry.dat");
    	for(C=0;C<Size;C++)
    	 {for(I=0;I<College;I++)
    	  Invntry1>>Quan[C][I];
    	 }
     }
    
    
    void DisplayReport(int Quan[Size][College])      
      {int Sum, Tot;
    	ofstream DisRep("DisRep.dat");
    
    		 DisRep<<"\t\t\t\t\t\t Inventory College Report "<<endl;
    		 DisRep<<"\t\t\t\t 1 \t\t 2 \t\t 3 \t\t 4 \t\t 5 \t\t 6 \t\t 7 \t\t\t Size Total"<<endl;
    
    
    		 DisRep<<"\n\n Small ";
            for(C=0;C<1;C++)
             {for(I=0;I<College;I++)
               {DisRep<<" \t\t"<<Quan[C][I];}
             }
            for(C=0;C<1;++C)
    		 {Sum=0;
    		  for(I=0;I<College;++I)
            Sum=Sum+Quan[C][I];
    		  DisRep<<"\t\t\t\t"<<Sum<<endl;}
    
    
    
           DisRep<<"\n\n Medium ";
            for(C=1;C<2;C++)
             {for(I=0;I<College;I++)
               {DisRep<<" \t\t"<<Quan[C][I];}
             }
            for(C=1;C<2;++C)
    		 {Sum=0;
    		  for(I=0;I<College;++I)
            Sum=Sum+Quan[C][I];
    		  DisRep<<"\t\t\t\t"<<Sum<<endl;}
    
    
    
             DisRep<<"\n\n Large ";
            for(C=2;C<3;C++)
             {for(I=0;I<College;I++)
               {DisRep<<" \t\t"<<Quan[C][I];}
             }
            for(C=2;C<3;++C)
    		 {Sum=0;
    		  for(I=0;I<College;++I)
            Sum=Sum+Quan[C][I];
    		  DisRep<<"\t\t\t\t"<<Sum<<endl;}
    
    
    
             DisRep<<"\n\n X-Large ";
            for(C=3;C<4;C++)
             {for(I=0;I<College;I++)
               {DisRep<<" \t\t"<<Quan[C][I];}
             }
            for(C=3;C<4;++C)
    		 {Sum=0;
    		  for(I=0;I<College;++I)
            Sum=Sum+Quan[C][I];
    		  DisRep<<"\t\t\t\t"<<Sum<<endl;}
    
    
    
            DisRep<<"\n\n College Total "; // How do i count values stored in columns?
            for(C=0;C<Size;C++)
             {Tot=0;
             for(I=0;I<1;++I)
              Tot=Tot+Quan[C][I];
                DisRep<<Tot;}
    
      }
    
    
    
      
    
    main()
    	{	int Quan[Size][College];
    		GetQuan(Quan);
    		PrintData(Quan);
    		StoreData(Quan);
    		ReadData(Quan);
    		DisplayReport(Quan);
    
            system("PAUSE");
    		return 0;
    }
    Is there a better way to add columns and rows? Sorry if this thing is hard to read...
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  2. #2
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    No One Can Help? I'm in distress...please help your fellow human being! lol
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  3. #3
    Aran
    Guest
    another new jersian? that is the state from which i hail...

    i'd help you, but i'm too lazy at the moment...

  4. #4
    Unregistered
    Guest
    make one extra row in the 2 dimensional array to hold the totals
    assign 0 to each element of the last row of the array.
    use a loop to add the elements of the rows.


    int 2D[x][y];
    for(s = 0; s < y; s++)
    {
    for(i = 0; i < x - 1; x++)
    {
    2D[x - 1][s] += 2D[i][s];
    }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. Addition of the rows and columns in an array.
    By Turtal in forum C Programming
    Replies: 4
    Last Post: 11-14-2006, 09:00 PM
  3. sum rows & columns in array
    By ronenk in forum C Programming
    Replies: 7
    Last Post: 06-20-2004, 04:16 AM
  4. adding rows
    By RedZippo in forum C++ Programming
    Replies: 1
    Last Post: 05-11-2004, 02:36 PM
  5. sizeof rows (not columns)
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 09-18-2001, 04:45 AM