12 Replies - 413 Views - Last Post: 15 April 2012 - 11:11 AM Rate Topic: -----

#1 teamsilvamma  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 140
  • Joined: 17-January 12

2-dim array

Posted 12 April 2012 - 04:37 PM

I am working on this code (instructions in the comments on top of the class) and I am posting all files in case you want to run it. technically it compiles correctly but the output is not coming out in tabular format as it should. Some help please?

p.s. I don't want to play the riddles game, nor do I want you to write the program for me. Just point me in the right direction. If you think I'm trying to get code written for me just ignore this post. You are not obligated to help.

Thanks

/********************************
* SalesSummary.cpp
* Luis Silva
*
*********************INSTRUCTIONS******************************
* Use a two-dimmensional array sales to store all total sales
* A company has salesperson 1-4 who sale products 1-5
* Once a day they report their sales passing a slip containing:
        a) The salesperson number
        B)/> The product number
        c) The total value of the product sold that day

* Assuming last month's information is available, write a program that
  summarizes the total sales by salesperson by product.
* After processing all info print it in a tabular format with
  columns = salesperson and rows = products.
* Print totals at the end of columns and rows

**************************OUTPUT*****************************

              Salesman 1    Salesman 2    Salesman 3   Salesman 4    Total

Product 1     5             3             4            1             $34.00

Product 2     1             3             2            4             $210.00

Product 3     0             2             1            1             $94.00

Product 4     3             4             2            1             $100.00

Product 5     2             1             3            5             $111.00


Total         11            13            12           12


*************************************************************/
#include <iostream>
#include <iomanip>

using namespace std;

#include "salessummary.h"


//Constructor
SalesSummary::SalesSummary(const int salesArray[][salesmen])
{
    for ( int product = 0; product < products; product++ )
        for( int salesman = 0; salesman < salesmen; salesman++ )

        sales[products][salesmen] = salesArray[products][salesmen];
}// end constructor

int SalesSummary::getTotalQty()
{

}//end getTotalQty

int SalesSummary::getTotalValue(const int*, int)
{

}//end getTotalValue

void SalesSummary::outputSales()
{
    cout << "\t    ";
    for (int salespeople = 0; salespeople < salesmen; salespeople++)
        cout << "Salesman " << setw(2) << salespeople + 1 << "   ";

    cout << "   Total " << endl;

    for (int prod = 0; prod < products; prod++)
        cout << "Product " << setw(2) << prod + 1;

    for (int salespeople = 0; salespeople < salesmen; salespeople++)
        cout << setw(8) << sales[products][salesmen];

        cout << endl;
    cout << "   Total " << endl;

}





/********************************
* main.cpp
* Luis Silva
*
* main file to run class and header
*************************************************************/
#include "salessummary.h"

using namespace std;

int main()
{

    int salesArray[SalesSummary::products][SalesSummary::salesmen] = {{5, 3, 4, 1},
                                                                      {1, 3, 2, 4},
                                                                      {0, 2, 1, 1},
                                                                      {3, 4, 2, 1},
                                                                      {2, 1, 3, 5}};

    SalesSummary Summary(salesArray);
    Summary.outputSales();

    //cout << salesArray << endl;

    return 0;
}




/***************************
* SalesSummary.h
* Luis Silva
*
* header file for SalesSummary. cpp
****************************/

#ifndef SALESSUMMARY_H
#define SALESSUMMARY_H

#include <string>

using namespace std;

class SalesSummary
{
    public:
    //constants
        static const int products = 5;
        static const int salesmen = 4;

        SalesSummary(const int [][salesmen]);

        int getTotalQty();
        int getTotalValue(const int [], const int);
        void outputSales();


    private:


        int sales[products][salesmen];
};//end class

#endif // SALESSUMMARY_H



Is This A Good Question/Topic? 0
  • +

Replies To: 2-dim array

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 974
  • View blog
  • Posts: 3,390
  • Joined: 19-February 09

Re: 2-dim array

Posted 12 April 2012 - 06:23 PM

Hi, you are using the constants and not the variables in the constructor.

52	        sales[products][salesmen] = salesArray[products][salesmen];



The outputSales function has a similar problem.


Capitalized constants are often used to make them stand out.

19	        static const int PRODUCTS = 5;
20	        static const int SALESMEN = 4;


Was This Post Helpful? 0
  • +
  • -

#3 mgcdrd  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 82
  • Joined: 22-November 09

Re: 2-dim array

Posted 12 April 2012 - 06:46 PM

Are you missing integers in you array in the first code. After the first one i only see 3 in each. is this all you code? You say you want to list prices, but i dont see prices for anything

This post has been edited by mgcdrd: 12 April 2012 - 06:49 PM

Was This Post Helpful? 0
  • +
  • -

#4 teamsilvamma  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 140
  • Joined: 17-January 12

Re: 2-dim array

Posted 12 April 2012 - 08:40 PM

View Postmgcdrd, on 12 April 2012 - 08:46 PM, said:

Are you missing integers in you array in the first code. After the first one i only see 3 in each. is this all you code? You say you want to list prices, but i dont see prices for anything



Thanks for replying mgcdrd..I haven't finished with all the coding. I have to assign costs and calculates the sum and totals and other stuff. Right now I'm just trying to figure out why it's not tabulating

View Post#define, on 12 April 2012 - 08:23 PM, said:

Hi, you are using the constants and not the variables in the constructor.

52	        sales[products][salesmen] = salesArray[products][salesmen];



The outputSales function has a similar problem.


Capitalized constants are often used to make them stand out.

19	        static const int PRODUCTS = 5;
20	        static const int SALESMEN = 4;



View Post#define, on 12 April 2012 - 08:23 PM, said:

Hi, you are using the constants and not the variables in the constructor.

52	        sales[products][salesmen] = salesArray[products][salesmen];



The outputSales function has a similar problem.


Capitalized constants are often used to make them stand out.

19	        static const int PRODUCTS = 5;
20	        static const int SALESMEN = 4;




Thanks #define...I will go over my code and pay closer attention to my variables. Good tip tho!
Was This Post Helpful? 0
  • +
  • -

#5 teamsilvamma  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 140
  • Joined: 17-January 12

Re: 2-dim array

Posted 12 April 2012 - 08:48 PM

View Postteamsilvamma, on 12 April 2012 - 10:40 PM, said:

View Postmgcdrd, on 12 April 2012 - 08:46 PM, said:

Are you missing integers in you array in the first code. After the first one i only see 3 in each. is this all you code? You say you want to list prices, but i dont see prices for anything



Thanks for replying mgcdrd..I haven't finished with all the coding. I have to assign costs and calculates the sum and totals and other stuff. Right now I'm just trying to figure out why it's not tabulating

View Post#define, on 12 April 2012 - 08:23 PM, said:

Hi, you are using the constants and not the variables in the constructor.

52	        sales[products][salesmen] = salesArray[products][salesmen];



The outputSales function has a similar problem.


Capitalized constants are often used to make them stand out.

19	        static const int PRODUCTS = 5;
20	        static const int SALESMEN = 4;



View Post#define, on 12 April 2012 - 08:23 PM, said:

Hi, you are using the constants and not the variables in the constructor.

52	        sales[products][salesmen] = salesArray[products][salesmen];



The outputSales function has a similar problem.


Capitalized constants are often used to make them stand out.

19	        static const int PRODUCTS = 5;
20	        static const int SALESMEN = 4;




Thanks #define...I will go over my code and pay closer attention to my variables. Good tip tho!


I took a look at it. I capitalized the constants but it changed nothing. Unless you are seeing something else, I checked every variable and constant and couldn't find a problem
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 974
  • View blog
  • Posts: 3,390
  • Joined: 19-February 09

Re: 2-dim array

Posted 12 April 2012 - 09:10 PM

Hi, here is your constructor :-

46	//Constructor
47	SalesSummary::SalesSummary(const int salesArray[][salesmen])
48	{
49	    for ( int product = 0; product < products; product++ )
50	        for( int salesman = 0; salesman < salesmen; salesman++ )
51	 
52	        sales[products][salesmen] = salesArray[products][salesmen];
53	}// end constructor




and your constants :-

    //constants
        static const int products = 5;
        static const int salesmen = 4;




If I change the constants to their values, perhaps you can see the problem.

//Constructor
SalesSummary::SalesSummary(const int salesArray[][4])
{
    for ( int product = 0; product < 5; 
                                    product++)
        for( int salesman = 0; salesman < 4;
                                    salesman++ )
	 
        sales[5][4] = salesArray[5][4];
}// end constructor


This post has been edited by #define: 12 April 2012 - 09:13 PM

Was This Post Helpful? 0
  • +
  • -

#7 teamsilvamma  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 140
  • Joined: 17-January 12

Re: 2-dim array

Posted 12 April 2012 - 09:19 PM

View Post#define, on 12 April 2012 - 11:10 PM, said:

Hi, here is your constructor :-

46	//Constructor
47	SalesSummary::SalesSummary(const int salesArray[][salesmen])
48	{
49	    for ( int product = 0; product < products; product++ )
50	        for( int salesman = 0; salesman < salesmen; salesman++ )
51	 
52	        sales[products][salesmen] = salesArray[products][salesmen];
53	}// end constructor




and your constants :-

    //constants
        static const int products = 5;
        static const int salesmen = 4;




If I change the constants to their values, perhaps you can see the problem.

//Constructor
SalesSummary::SalesSummary(const int salesArray[][4])
{
    for ( int product = 0; product < 5; 
                                    product++)
        for( int salesman = 0; salesman < 4;
                                    salesman++ )
	 
        sales[5][4] = salesArray[5][4];
}// end constructor



Ok you're losing me here HA HA I changed it in my code and nothing changed. Which I kind of expected that because the variables that were in place already had those values assigned to it.. Did I miss something?
Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 974
  • View blog
  • Posts: 3,390
  • Joined: 19-February 09

Re: 2-dim array

Posted 12 April 2012 - 09:39 PM

The variable product is not the same as the constant products.
What is used here the variables or the constants? If they are constants will they vary?

52          sales[products][salesmen] = salesArray[products][salesmen];




The array indexes go from [0][0] to [4][3]. The indexes [5][4] are outside the array.

09        sales[5][4] = salesArray[5][4];




What you want is to set each element (by using the proper variables):-

sales[0][0] = salesArray[0][0];
sales[0][1] = salesArray[0][1];
sales[0][2] = salesArray[0][2];
sales[0][3] = salesArray[0][3];
sales[1][0] = salesArray[1][0];
sales[1][1] = salesArray[1][1];
...
sales[4][3] = salesArray[4][3];


This post has been edited by #define: 12 April 2012 - 09:40 PM

Was This Post Helpful? 0
  • +
  • -

#9 teamsilvamma  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 140
  • Joined: 17-January 12

Re: 2-dim array

Posted 13 April 2012 - 06:41 AM

View Post#define, on 12 April 2012 - 11:39 PM, said:

The variable product is not the same as the constant products.
What is used here the variables or the constants? If they are constants will they vary?

52          sales[products][salesmen] = salesArray[products][salesmen];




The array indexes go from [0][0] to [4][3]. The indexes [5][4] are outside the array.

09        sales[5][4] = salesArray[5][4];




What you want is to set each element (by using the proper variables):-

sales[0][0] = salesArray[0][0];
sales[0][1] = salesArray[0][1];
sales[0][2] = salesArray[0][2];
sales[0][3] = salesArray[0][3];
sales[1][0] = salesArray[1][0];
sales[1][1] = salesArray[1][1];
...
sales[4][3] = salesArray[4][3];


So it should be then

sales[4][3] = salesArray[4][3];


How is this messing up the tabular format? I dont get that.

I changed the array parameters and it still not tabulating. Remember I'm not too worry with values right now. All I want is my array to create a tabulated format with 5 rows and 4 columns. Then I worry about the values. One thing at a time

This post has been edited by teamsilvamma: 13 April 2012 - 06:44 AM

Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 974
  • View blog
  • Posts: 3,390
  • Joined: 19-February 09

Re: 2-dim array

Posted 13 April 2012 - 11:10 AM

View Postteamsilvamma, on 13 April 2012 - 03:41 PM, said:

Remember I'm not too worry with values right now. All I want is my array to create a tabulated format with 5 rows and 4 columns. Then I worry about the values. One thing at a time



Ok, I was looking at the values first. :)


In the outputSales function you want to run one for loop within the other. You need to use braces, so that for each product all the salespeople will be listed .

    for (int prod = 0; prod < products; prod++)
    {
       cout << "Product " << setw(2) << prod + 1;

       for (int salespeople = 0; salespeople < salesmen; salespeople++)
          cout << setw(8) << sales[products][salesmen];
    }


Was This Post Helpful? 1
  • +
  • -

#11 teamsilvamma  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 140
  • Joined: 17-January 12

Re: 2-dim array

Posted 13 April 2012 - 11:49 AM

View Post#define, on 13 April 2012 - 01:10 PM, said:

View Postteamsilvamma, on 13 April 2012 - 03:41 PM, said:

Remember I'm not too worry with values right now. All I want is my array to create a tabulated format with 5 rows and 4 columns. Then I worry about the values. One thing at a time



Ok, I was looking at the values first. :)


In the outputSales function you want to run one for loop within the other. You need to use braces, so that for each product all the salespeople will be listed .

    for (int prod = 0; prod < products; prod++)
    {
       cout << "Product " << setw(2) << prod + 1;

       for (int salespeople = 0; salespeople < salesmen; salespeople++)
          cout << setw(8) << sales[products][salesmen];
    }




Gotcha! This reply came right on time with someone else that is helping me and you both coincided on the brackets. I actually had this problem before, I dont know why it didnt occurred to me to check the brackets. My orofessor told me once, before you check the words, check the punctuation, brackets, and operators. They seem to be responsible for 90% of code error

Thanks. If I get stuck again with the code I'll repost. I'm sure I'll hit another snag ;-)
Was This Post Helpful? 0
  • +
  • -

#12 teamsilvamma  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 140
  • Joined: 17-January 12

Re: 2-dim array

Posted 15 April 2012 - 05:15 AM

Hi,

Stuck again! HA HA. I assigned the values of prod1...5 so that I can add the totals at the end of each row. The qty sold at the end of each column is hardcoded. Well, two problems, I don't know how to add each individual column and each individual row. :( but worse than that, if you run my program you'll see that it's returning memory locations in the table instead of the Qtys hardcoded in the array. SO I can't add the columns or rows until I have real values in the array locations.

EDIT: I actually came back and edited the class code for your viewing. I fixed the table output. It shows the real values now PHEW! that is a relief. But it came with a cost. Now I need to figure out how to add a row header that says Product 1....5. :(

Also can you check out the commented code in trying to generate a total at the end of all rows and columns? I'm still stuck there

Here's my codes. Thanks in advance


CLASS
/********************************
* SalesSummary.cpp
* Luis Silva
*
*********************INSTRUCTIONS******************************
* Use a two-dimmensional array sales to store all total sales
* A company has salesperson 1-4 who sale products 1-5
* Once a day they report their sales passing a slip containing:
        a) The salesperson number
        B)/> The product number
        c) The total value of the product sold that day

* Assuming last month's information is available, write a program that
  summarizes the total sales by salesperson by product.
* After processing all info print it in a tabular format with
  columns = salesperson and rows = products.
* Print totals at the end of columns and rows

**************************OUTPUT*****************************

              Salesman 1    Salesman 2    Salesman 3   Salesman 4    Total

Product 1     5             3             4            1             $34.00

Product 2     1             3             2            4             $210.00

Product 3     0             2             1            1             $94.00

Product 4     3             4             2            1             $100.00

Product 5     2             1             3            5             $111.00


Total         11            13            12           12


*************************************************************/
#include <iostream>
#include <iomanip>

using namespace std;

#include "salessummary.h"

SalesSummary::SalesSummary()
{
    prod1 = 2.99;
    prod2 = 3.99;
    prod3 = 6.99;
    prod4 = 12.99;
    prod5 = 8.99;
}//end Constructor

//Constructor containing array
SalesSummary::SalesSummary(const int salesArray[][4])
{
//creating array table
    for(int i = 0; i < PRODUCTS; i++)
        for( int j = 0; j < SALESMEN; j++)
        {
            sales[i][j] = salesArray[i][j];
        }
    cout << endl;
}// end constructor


double SalesSummary::getTotalValue(const double*, double)
{
    int rowSum[5] = {0};

    for(int n = 0; n != 6; ++n)
    {
        //rowSum[j] += sales[i][j];
        //cout.width(10);
        cout << rowSum[n] << " ";
    }
}//end getTotalValue

int SalesSummary::getTotalQty()
{
    int colSum[4] = {0};

    for (int s = 0; s != 5; ++s)
    {
        //colSum[i] += sales[i][j];
        //cout.width(10);
        cout <<colSum[s]<<"";
    }
}//end getTotalQty


void SalesSummary::outputSales()
{
	//Salesmen header and total header
	cout << "\t";
	for (int salespeople = 0; salespeople < SALESMEN; salespeople++)
       cout << "Salesman " << salespeople + 1 << "   ";

    cout << "   Total " << endl;
    cout << endl;

    //print array
    for(int i = 0; i != 5; ++i)
    {
        for(int j = 0; j != 4; ++j)
        {
            cout.width(12);
            cout << sales[i][j] << "  ";
        }
        cout << endl; //for proper spcaing in table
    }
    cout << "\nTotal " << endl;
}//end outputSales

//Products header
//	if(j == 0)
//		cout << "Product " << i + 1 << "   ";
//		cout << sales  << '\t';

/*  double prod1Total = prod1 * j++;
    double prod2Total = prod2 * j++;
    double prod3Total = prod3 * j++;
    double prod4Total = prod4 * j++;
    double prod5Total = prod5 * j++;
        if(j == 3 && i == 0)
            cout << prod1Total;
        if(j == 3 && i == 1)
            cout << prod2Total;
*/



MAIN
/********************************
* main.cpp
* Luis Silva
*
* main file to run class and header
*************************************************************/
#include <iostream>
#include "salessummary.h"

using namespace std;

int main()
{

    int salesArray[SalesSummary::PRODUCTS][SalesSummary::SALESMEN] = {{5, 3, 4, 1},
                                                                      {1, 3, 2, 4},
                                                                      {0, 2, 1, 1},
                                                                      {3, 4, 2, 1},
                                                                      {2, 1, 3, 5}};


    SalesSummary Summary(salesArray);
    Summary.outputSales();

    return 0;
}




HEADER
/***************************
* SalesSummary.h
* Luis Silva
*
* header file for SalesSummary. cpp
****************************/

#ifndef SALESSUMMARY_H
#define SALESSUMMARY_H



class SalesSummary
{
    public:
    //constants
        static const int PRODUCTS = 5;
        static const int SALESMEN = 4;

        SalesSummary();
        SalesSummary(const int [][SALESMEN]);

        int getTotalQty();
        double getTotalValue(const double [], const double);
        void outputSales();


    private:
        int sales[PRODUCTS][SALESMEN];
        double prod1, prod2, prod3, prod4, prod5;

};//end class

#endif // SALESSUMMARY_H


This post has been edited by teamsilvamma: 15 April 2012 - 06:34 AM

Was This Post Helpful? 0
  • +
  • -

#13 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 974
  • View blog
  • Posts: 3,390
  • Joined: 19-February 09

Re: 2-dim array

Posted 15 April 2012 - 11:11 AM

Hi, you could calculate the total quantity for a row, by passing the value for a row.

int SalesSummary::getTotalQtyOfRow(int row)
{
  // calculate the total quantity for a row
  return total;
}




If you return the total, then you could use the function to calculate the total for all the rows, if needed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1