Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Display Tables from Vectors


  • Please log in to reply

#1
Kevin39

Kevin39

    Member

  • Member
  • PipPip
  • 18 posts
Hello,

attempting to display a table from an array, was wondering if anyone could assist in this matter, learning C++ and attempting to finish this course.

Requirement is to display a table with information.

Here is the code i have so far.

/*This file Produces a table of total number of cars sold and the number of cars
*sold by each salesperson.
*
*Input: Number of Salespersons and Number of Cars sold
*Output: Total Number of Cars sold by each salesperson.
******************************************************************************/

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;
int main()
{
const int MODEL_CARS = 10;
const int NUM_SALESPERSONS = 8;
const int CARS_SOLD[MODEL_CARS][NUM_SALESPERSONS]
= { {0, 0, 2, 0, 5, 6, 3, 0},
{5, 1, 9, 0, 0, 2, 3, 2},
{0, 0, 0, 1, 0, 0, 0, 0},
{1, 1, 1, 0, 2, 2, 2, 1},
{5, 3, 2, 0, 0, 2, 5, 5},
{2, 2, 1, 0, 1, 1, 0, 0},
{3, 2, 5, 0, 1, 2, 0, 4},
{3, 0, 7, 1, 3, 5, 2, 4},
{0, 2, 6, 1, 0, 5, 2, 1},
{4, 0, 2, 0, 3, 2, 1, 0} };

{
for (int MODEL_CARS = 0; MODEL_CARS < 10; MODEL_CARS++)
{
for (int NUM_SALESPERSONS = 0; NUM_SALESPERSONS < 8;
NUM_SALESPERSONS++);
}
}

}

Thanks for any and all help.
  • 0

Advertisements


#2
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
hi Kevin39,

you are off to a good start.

1. let's start with a gotcha. whenever a for loop is immediately terminated with a semi-colon it becomes ineffective.

avoid doing this: for(...);

a good habit to form is to add the curly braces to the for loop when it is first written:
for( ... ) {
}

see if you can find this in your code

2. there are 2 unnecessary curly braces({}) surrounding the for loops. they can be removed. note - these are not 'incorrect' since they mark off a block of code containing the for loops. the code will still compile and run with the extra {}, but conventional coding would leave them out.

3. here is another conventional thing and can reduce debugging time. again, what you have done is perfectly legitimate. variables MODEL_CARS and NUM_SALESPERSONS are declared as contant ints at the start of the program and then redeclared as ints later on. the redeclared variables are seen only within the scope of the for loops. this could increase the time required to debug the code and lead to confusion since the code would compile. a more conventional approach is to use different variable names in the for loops. and you might use names such as 'model_car_number' and 'salesman_number'.

4. the only thing left is to add 2 cout statements - one with column headings(optional) and one with the car sales data.

if you get stuck, please post the step number and your new code.

bdlt
  • 0

#3
Kevin39

Kevin39

    Member

  • Topic Starter
  • Member
  • PipPip
  • 18 posts
I guess i should of explained it a little better, Your input has been very helpful and appreciated.

Here is what i need to display on the Screen.

SALESPERSON
MODEL : 1 2 3 4 5 6 7 8: Totals
------------------------------------------------
1 :Number of cars sold by Salespeople and the total.






Totals: Total cars sold per salesperson.

All this information is based on the Array or Vector.


Here is what i have so far after adjustments.

/*This file Produces a table of total number of cars sold and the number of cars
*sold by each salesperson.
*
*Input: Number of Salespersons and Number of Cars sold
*Output: Total Number of Cars sold by each salesperson.
******************************************************************************/

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;
int main()
{
const int MODEL_CARS = 10;
const int NUM_SALESPERSONS = 8;
const int CARS_SOLD[MODEL_CARS][NUM_SALESPERSONS]
= { {0, 0, 2, 0, 5, 6, 3, 0},
{5, 1, 9, 0, 0, 2, 3, 2},
{0, 0, 0, 1, 0, 0, 0, 0},
{1, 1, 1, 0, 2, 2, 2, 1},
{5, 3, 2, 0, 0, 2, 5, 5},
{2, 2, 1, 0, 1, 1, 0, 0},
{3, 2, 5, 0, 1, 2, 0, 4},
{3, 0, 7, 1, 3, 5, 2, 4},
{0, 2, 6, 1, 0, 5, 2, 1},
{4, 0, 2, 0, 3, 2, 1, 0} };

int ModelCars, Salesmen;

for (ModelCars = 0; ModelCars < 10; ModelCars++)
{
for (Salesmen = 0; Salesmen < 8; Salesmen++);
};
cout << setw(30)<< " Salesperson \n";
cout << setw(1) << " Model : 1 " << setw(4) << "2"
<< setw(4) << "3" << setw(4) << "4" << setw(4)<< "5"
<< setw(4) << "6" << setw(4) << "7" << setw(4)<< "8:"
<< setw(4) << " Totals\n";
cout << setw(1) << "-------------------------------------------------" "\n";
cout << CARS_SOLD[ModelCars][Salesmen];
cout << endl;
}

Thank you for your assistance.
  • 0

#4
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
most of the pieces are here. let's get what you have working, then add the code to calculate and display the total sales.
1. fix the for loop for( ... ); - see post #2 for details
2. the 'heading' code goes before the for loops
3. the CARS_SOLD code goes inside the inner for loop
4. remove the semi colon from '};'
5. adjust setw() as required to align the columns
6. post your new code starting with cout << setw(30)<< " Salesperson \n";
  • 0

#5
Kevin39

Kevin39

    Member

  • Topic Starter
  • Member
  • PipPip
  • 18 posts
I seem to getting something close to what i want but not sure how to align each individual line in the array with the Heading line.

but here is what i have so far.


cout << setw(30)<< " Salesperson \n";
cout << setw(1) << " Model : 1" << setw(2) << "2"
<< setw(2) << "3" << setw(2) << "4" << setw(2) << "5"
<< setw(2) << "6" << setw(2) << "7" << setw(3) << "8:"
<< setw(2) << " Totals\n";
cout << setw(1) << "-------------------------------------------------" "\n";


here is where the left column of Model Numbers starts
cout << setw(4) << "1";
cout << setw(8); for (Model_Num = 0; Model_Num < MODEL_CARS; Model_Num++)
{
for (Salesmen = 0; Salesmen < NUM_SALESPERSONS; Salesmen++)
cout << CARS_SOLD[Model_Num][Salesmen] << " ";
cout << endl;

};

I have the array displaying:

I have the top line displaying as it should.


The left column should display the Model Number then the number of models sold by the salesmen going from left to right.

The right column should display the Total vehicles per model
The bottom line should display the total vehicles per salesmen.
  • 0

#6
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
much better. let's try to line up the columns:


remove the following -
cout << setw(4) << "1"; 
cout << setw(8);


modify the inner for() loop to look like this -
  for (Salesmen = 0; Salesmen < NUM_SALESPERSONS; Salesmen++) {
	if( Salesmen == 0 ) { 
	  cout << setw(9) << " ";
	}  
	cout << CARS_SOLD[Model_Num][Salesmen] << " ";
  }



you might need to adjust setw(9)

once you are happy with the display, you can add a counter to sum the total sales of each salesman. the summing can be done on the line above or below cout << CARS_SOLD[Model_Num][Salesmen] << " ";.

the sum can then be displayed when the inner for() loop is completed

edit - humor me and please remove the semicolon from the curly brace for the outer for() loop
}; should be }

Edited by bdlt, 14 February 2006 - 02:46 PM.

  • 0

#7
Kevin39

Kevin39

    Member

  • Topic Starter
  • Member
  • PipPip
  • 18 posts
That displays the array under the column headers perfectly.

How would i now display the Model Number on the Left side of the Array inline with each row?

I will work on that and the Total entries based on you reply.

I want to thank you very much for your assistance today, you have been very helpful.

:tazz:
  • 0

#8
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
I'm happy to help and you seem to be able to figure out the details on your own - and that's great.

you can display the model number on this line - cout << setw(9) << " ";

the setw(9) will need to be modified
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP