help! two dimensional array

This is a discussion on help! two dimensional array within the C++ Programming forums, part of the General Programming Boards category; help me if you can! My program generates random numbers into a grid of 4 columns by 5 rows to ...

  1. #1
    Unregistered
    Guest

    Question help! two dimensional array

    help me if you can!
    My program generates random numbers into a grid of 4 columns by 5 rows to represent daily/weekly sales, but I can't sum up the total sales in columns and also in rows. I would be most grateful if anyone could help and advise me on this, and once I get this to work I would also like to convert it into using pointer which I need to learn and to get to grip with both array and pointer notations. My compiler is Borland C++ 4.52 and I also have Dev-4.
    My code so far: Thanks in anticipation...

    #include<iostream.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>

    #define ROW 5
    #define COLUMN 4

    int main()
    {
    int sales[ROW][COLUMN];
    int sum[ROW][COLUMN];
    int down;
    int across;

    srand((unsigned)time(NULL));
    cout.setf(ios::right);
    cout<<endl;

    //initialise the array
    for (down=0;down<ROW;down++)
    {
    for (across=0;across<COLUMN;across++)
    {
    sales[down][across]=0;
    sum[down][across]=0;
    }
    }

    //print the table
    for (down=0;down<ROW;down++)
    {
    for (across=0;across<COLUMN;across++)
    {
    sales[down][across]=rand()%21;
    cout.width(4);
    cout<<sales[down][across];
    }
    cout<<" |"<<endl;
    }
    cout<<"-----------------|------"<<endl;

    //sums of the columns
    for (across=0;across<COLUMN;across++)
    {
    for (down=0;down<ROW;down++)
    {
    sum[down][across]+=sales[down][across];
    }
    cout.width(4);
    cout<<sum[down][across];
    }
    cout<<" |"<<endl;

    return(0);
    }
    -----------------------------------------------------------------------
    Ask and it shall be given unto you...

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    As the total is one value then you don't need an array to print it (although if you want to store the subtotals then you could use a one-dimensional array) -

    Code:
    #include<iostream> 
    #include <cstdlib> 
    #include <cstdio> 
    #include <ctime> 
    
    using namespace std;
    
    #define ROW 5 
    #define COLUMN 4 
    
    int main() 
    { 
    	int sales[ROW][COLUMN]={0}; 
    	int sumtotal=0,sum=0; 
    	int down; 
    	int across; 
    
    	srand((unsigned)time(NULL)); 
    	cout.setf(ios::right); 
    	cout<<endl; 
    
    
    	//print the table 
    	for (down=0;down<ROW;down++) 
    	{ 
    		for (across=0;across<COLUMN;across++) 
    		{ 
    			sum += sales[down][across]=rand()%21; 
    			cout.width(4); 
    			cout<<sales[down][across]; 
    		} 
    		cout<<" |"<<sum << endl; 
    		sum=0;
    	} 
    	cout<<"-----------------|------"<<endl; 
    
    	sum =0;
    	//sums of the columns 
    	for (across=0;across<COLUMN;across++) 
    	{ 
    		for (down=0;down<ROW;down++) 
    		{ 
    			sum+=sales[down][across]; 
    		} 
    		cout.width(4); 
    		sumtotal+=sum;
    		cout<<sum; 
    		sum=0;
    	} 
    	cout<<" |"<<sumtotal<<endl ; 
    
    	return 0; 
    }
    zen

  3. #3
    Unregistered
    Guest

    Wink Thanks

    Thanks very much Zen for your help! - You're a real helper, a star, a guardian angel, a professor of C/C++ and above all a true Zen!

    Oh, by the way, any advise on how to convert it using pointer as I'm learning how to use array and pointer, and they are not my homework from the class, but my own home-study (self betterment - self taught). Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-10-2008, 12:53 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Replies: 5
    Last Post: 11-20-2001, 12:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21