// Lab 3(c) Salesperson
// sales.cpp
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::fixed;
#include <iomanip>
using std::setprecision;
using std::setw;
int main()
{
const int PEOPLE = 5;
const int PRODUCTS = 6;
const int SIZE = 5;
double sales [SIZE]= {0,1,2,3,4};
double value;
double totalSales;
double productSales[ PRODUCTS ] = { 0.0 }; // line 25
int salesPerson;
int product;
cout << "Enter the salesperson (1 - 4), "
<< "product number (1 - 5)\nand total sales."
<< "Enter -1 for the salesperson to end input.\n";
cin >> salesPerson;
// process sales
while ( salesPerson != -1 )
{
cin >> product >> value;
/* Write a statement that adds values to the
sales array */
cin >> salesPerson;
} // end while
// table header: describes output and prints
// column header(product numbers 1-5)
cout << "\nThe total sales for each sales person "
<< "are displayed\nat the end of each row,"
<< "and the total sales for each\nproduct "
<< "are displayed at the bottom of each column.\n"
<< setw( 10 ) << 1 << setw( 10 ) << 2
<< setw( 10 ) << 3 << setw( 10 ) << 4
<< setw( 10 ) << 5 << setw( 12 ) << "Total\n" << fixed
<< setprecision( 2 );
// nested loop structure: prints salesperson number
// followed by the amounts sold for each product
for ( int i = 1; i<5 ; ++i )
{
totalSales = 0.0;
// print salesperson number
cout << i;
// inner loop: prints amounts sold for each product
for ( int j = 1; j<=6; ++j )
{
totalSales=totalSales + j;
// print sales for each salesperson for each product
cout << setw( 10 )
<< sales[ i ][ j ];
productSales= productSales + sales;
} // end for
// print the last column item (total sales of each
// product). The totalSales value is 9.99 under
// "Total" in the output box. After this value is
// printed, the next table line can be created
cout << setw( 10 ) << totalSales << '\n';
} // end for
// header for last row
cout << "\nTotal" << setw( 6 ) << productSales[ 1 ];
// prints last row which displays total sales
// for each product
for ( int j = 2; j < PRODUCTS; ++j )
cout << setw( 10 ) << productSales[ j ];
cout << endl;
return 0;
} // end main
Help with array
Page 1 of 16 Replies - 1356 Views - Last Post: 05 April 2010 - 07:02 AM
#1
Help with array
Posted 05 April 2010 - 05:55 AM
Hi just wondering could anyone help me with a problem i'm having? this is a program to print out a table showing which employees have sold there product and how much they've made but i'm havin a problem with the array anyone got any ideas?
Replies To: Help with array
#2
Re: Help with array
Posted 05 April 2010 - 06:17 AM
How about telling us what's the problem?
#3
Re: Help with array
Posted 05 April 2010 - 06:22 AM
well i get these errors when i try to compile it
lab3©.cpp(65) : error C2109: subscript requires array or pointer type
lab3©.cpp(67) : error C2110: '+' : cannot add two pointers
And im also not quite sure what i hav to do here
/* Write a statement that adds values to the
sales array */
lab3©.cpp(65) : error C2109: subscript requires array or pointer type
lab3©.cpp(67) : error C2110: '+' : cannot add two pointers
And im also not quite sure what i hav to do here
/* Write a statement that adds values to the
sales array */
#4
Re: Help with array
Posted 05 April 2010 - 06:25 AM
<< sales[ i ][ j ];
sales is a one-dimensional array, and it can't have two subscripts, as there are no rows and columns. Use only one subscript, like sales[i] or sales[j]. Also, arrays in C/C++ should be indexed from 0 -> N - 1.
#5
Re: Help with array
Posted 05 April 2010 - 06:28 AM
thank you sarmanu but is there a way to add rows and columns as i think it might be something i have to do.
#6
Re: Help with array
Posted 05 April 2010 - 06:31 AM
Declaring a two-dimensional array is easy. Here, I will show you how to add a statically allocated two-dimensional array:
A method of printing a matrix:
where my_matrix is your two-dimensional array, number_of_rows and number_of_columns are self-explanatory.
// a is a two-dimensional array (simply called "matrix"). // it can hold a maximum of 20 rows and columns int a[20][20]; // How to access an element: write down the name of the matrix, // then, using two subscripts, you access the row and the column: a[0][0] = 34; // add 34 on line 0 and column 0 a[0][1] = 10; // add 10 on line 0 and column 1 a[2][4] = 0; // add 0 on line 2 and column 4
A method of printing a matrix:
for (int i = 0; i < number_of_rows; i++)
{
for (int j = 0; j < number_of_columns; j++)
std::cout << my_matrix[i][j] << " ";
std::cout << "\n";
}
where my_matrix is your two-dimensional array, number_of_rows and number_of_columns are self-explanatory.
This post has been edited by sarmanu: 05 April 2010 - 06:37 AM
#7
Re: Help with array
Posted 05 April 2010 - 07:02 AM
thank you very much for that it was very well explained i have put in that info and everything is fine now except for the one error im now getting
cpp(67) : error C2110: '+' : cannot add two pointers
the info there before i started the project was
/* Write a statement that adds the current sales
element to productSales */
i inserted
is this wrong?
cpp(67) : error C2110: '+' : cannot add two pointers
the info there before i started the project was
/* Write a statement that adds the current sales
element to productSales */
i inserted
productSales= productSales + sales;
is this wrong?
Page 1 of 1

New Topic/Question
Reply



MultiQuote



|