| | |
I need some help...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2009
Posts: 13
Reputation:
Solved Threads: 0
This is what I need to do:
Use a single-subscripted array to solve the following problem: A company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9% of $5000 or a total of $650. Write a program (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson’s salary is truncated to an integer amount):
a) $200-$299
b) $300-$399
c) $400-$499
d) $500-$599
e) $600-$699
f) $700-$799
g) $800-$899
h) $900-$999
i) $1000 and over
Summarize the results in tabular format
This is what I've got:
I am fairly new to C++, and am not sure how to fix it. I have used QBasic and C before, but not extensively. The program sort of works...but it doesn't display the proper results in the function. If I put a regular cout << counter[whatever]; into the main function, it displays the proper results...any ideas how I can transfer the correct results into the function? I have read a lot about passing arrays into functions, but can't figure out what exactly to add to my program to make it do what it is supposed to. Any help would be greatly appreciated, thank you! =)
~TheSilverFox
Use a single-subscripted array to solve the following problem: A company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9% of $5000 or a total of $650. Write a program (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson’s salary is truncated to an integer amount):
a) $200-$299
b) $300-$399
c) $400-$499
d) $500-$599
e) $600-$699
f) $700-$799
g) $800-$899
h) $900-$999
i) $1000 and over
Summarize the results in tabular format
This is what I've got:
cpp Syntax (Toggle Plain Text)
#include <iostream> using std::cout; using std::cin; using std::endl; #include <iomanip> void displayResults( ); int main() { int sales; double salary; int counter[ 9 ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; cout << "Enter a sales amount: (negative to end)"; cin >> sales; while (sales > 0){ salary = (sales * .09) + 200; if ( (salary >= 200) && (salary < 300) ) ++counter[ 0 ]; else if ( (salary >= 300) && (salary < 400) ) ++counter[ 1 ]; else if ( (salary >= 400) && (salary < 500) ) ++counter[ 2 ]; else if ( (salary >= 500) && (salary < 600) ) ++counter[ 3 ]; else if ( (salary >= 600) && (salary < 700) ) ++counter[ 4 ]; else if ( (salary >= 700) && (salary < 800) ) ++counter[ 5 ]; else if ( (salary >= 800) && (salary < 900) ) ++counter[ 6 ]; else if ( (salary >= 900) && (salary < 1000) ) ++counter[ 7 ]; else ++counter[ 8 ]; cout << "Enter a sales amount: (negative to end)"; cin >> sales; } displayResults(); system("pause"); return 0; } void displayResults( ) { int counter[ 9 ]; cout << " Range Number" << endl; cout << "$200 - $299\t " << counter[ 0 ] << endl; cout << "$300 - $399\t " << counter[ 1 ] << endl; cout << "$400 - $499\t " << counter[ 2 ] << endl; cout << "$500 - $599\t " << counter[ 3 ] << endl; cout << "$600 - $699\t " << counter[ 4 ] << endl; cout << "$700 - $799\t " << counter[ 5 ] << endl; cout << "$800 - $899\t " << counter[ 6 ] << endl; cout << "$900 - $999\t " << counter[ 7 ] << endl; cout << "Over $1000 \t " << counter[ 8 ] << endl; }
I am fairly new to C++, and am not sure how to fix it. I have used QBasic and C before, but not extensively. The program sort of works...but it doesn't display the proper results in the function. If I put a regular cout << counter[whatever]; into the main function, it displays the proper results...any ideas how I can transfer the correct results into the function? I have read a lot about passing arrays into functions, but can't figure out what exactly to add to my program to make it do what it is supposed to. Any help would be greatly appreciated, thank you! =)
~TheSilverFox
Last edited by Tekmaven; Jun 19th, 2009 at 7:25 pm. Reason: Code Tags
The slot determination is easy, below range, range, over range.
C++ Syntax (Toggle Plain Text)
int counter[ 9 ]; i = counter[ n ] ; is the value so... int *pCnt = &counter[ n ]; Gives you a pointer to the integer located at cell [n]. Or try this. int *pCnt = counter; while( n-- ) { int i = *pCnt++; }
•
•
Join Date: Jun 2009
Posts: 13
Reputation:
Solved Threads: 0
I tried adding that into my code:
void displayResults( )
{
int counter[ 9 ];
int n;
int i;
i = counter[ n ] ;
int *pCnt = &counter[ n ];
cout << " Range Number" << endl;
cout << "$200 - $299\t " << &counter[ 0 ] << endl;
cout << "$300 - $399\t " << &counter[ 1 ] << endl;
cout << "$400 - $499\t " << &counter[ 2 ] << endl;
cout << "$500 - $599\t " << &counter[ 3 ] << endl;
cout << "$600 - $699\t " << &counter[ 4 ] << endl;
cout << "$700 - $799\t " << &counter[ 5 ] << endl;
cout << "$800 - $899\t " << &counter[ 6 ] << endl;
cout << "$900 - $999\t " << &counter[ 7 ] << endl;
cout << "Over $1000 \t " << &counter[ 8 ] << endl;
}
and it just caused my .exe to crash. I wasn't 100% sure how to add it in properly though. Do I need to declare n better? Where do I use i? Thank you in advance...
void displayResults( )
{
int counter[ 9 ];
int n;
int i;
i = counter[ n ] ;
int *pCnt = &counter[ n ];
cout << " Range Number" << endl;
cout << "$200 - $299\t " << &counter[ 0 ] << endl;
cout << "$300 - $399\t " << &counter[ 1 ] << endl;
cout << "$400 - $499\t " << &counter[ 2 ] << endl;
cout << "$500 - $599\t " << &counter[ 3 ] << endl;
cout << "$600 - $699\t " << &counter[ 4 ] << endl;
cout << "$700 - $799\t " << &counter[ 5 ] << endl;
cout << "$800 - $899\t " << &counter[ 6 ] << endl;
cout << "$900 - $999\t " << &counter[ 7 ] << endl;
cout << "Over $1000 \t " << &counter[ 8 ] << endl;
}
and it just caused my .exe to crash. I wasn't 100% sure how to add it in properly though. Do I need to declare n better? Where do I use i? Thank you in advance...
First problem that array is empty (not initialized).
Second, you only want the value, not the address for printing!
Second, you only want the value, not the address for printing!
C++ Syntax (Toggle Plain Text)
// YOU need t fill counter // and initialize n like in a loop reference or something. //i = counter[ n ] ; int *pCnt = &counter[ n ]; for (n=0; n < 9; n++) { counter[n] = n; } cout << " Range Number" << endl; cout << "$200 - $299\t " << counter[ 0 ] << endl; cout << "$300 - $399\t " << counter[ 1 ] << endl; cout << "$400 - $499\t " << counter[ 2 ] << endl; cout << "$500 - $599\t " << counter[ 3 ] << endl; cout << "$600 - $699\t " << counter[ 4 ] << endl; cout << "$700 - $799\t " << counter[ 5 ] << endl; cout << "$800 - $899\t " << counter[ 6 ] << endl; cout << "$900 - $999\t " << &counter[ 7 ] << endl; cout << "Over $1000 \t " << counter[ 8 ] << endl; }
•
•
Join Date: Jul 2005
Posts: 1,755
Reputation:
Solved Threads: 283
void displayResults( );
change this to:
void displayResults(int *, int);
Change declaration of counter to this:
const int SIZE = 9;
int counter[SIZE];
call displayResults() like this:
dipsplayResults(counter, SIZE);
Try to define it on your own.
change this to:
void displayResults(int *, int);
Change declaration of counter to this:
const int SIZE = 9;
int counter[SIZE];
call displayResults() like this:
dipsplayResults(counter, SIZE);
Try to define it on your own.
Last edited by Lerner; Jun 19th, 2009 at 5:57 pm.
Klatu Barada Nikto
•
•
Join Date: Jun 2009
Posts: 13
Reputation:
Solved Threads: 0
Ok. This is what the function reads now:
The program works with no compiler errors, but the results show just 0-8 in sequential order for the Number corresponding to each range. I believe that is because somewhere, a pointer isn't pointing to the original int counter[9]; I created...I'm not sure which parts (if any) should be omitted or edited...
Thank you very much for your quick responses and help! I greatly appreciate it!!
cpp Syntax (Toggle Plain Text)
void displayResults(int*, int) { int n; const int SIZE = 9; int counter[SIZE]; int *pCnt = &counter[ n ]; for (n=0; n < 9; n++) { counter[n] = n; } cout << " Range Number" << endl; cout << "$200 - $299\t " << counter[ 0 ] << endl; cout << "$300 - $399\t " << counter[ 1 ] << endl; cout << "$400 - $499\t " << counter[ 2 ] << endl; cout << "$500 - $599\t " << counter[ 3 ] << endl; cout << "$600 - $699\t " << counter[ 4 ] << endl; cout << "$700 - $799\t " << counter[ 5 ] << endl; cout << "$800 - $899\t " << counter[ 6 ] << endl; cout << "$900 - $999\t " << counter[ 7 ] << endl; cout << "Over $1000 \t " << counter[ 8 ] << endl; }
Thank you very much for your quick responses and help! I greatly appreciate it!!
Last edited by Tekmaven; Jun 19th, 2009 at 7:26 pm. Reason: Code Tags
Sorry I think I confused you
C++ Syntax (Toggle Plain Text)
void displayResults(int*pCnt, int , nCnt) { cout << " Range Number" << endl; cout << "$200 - $299\t " << *(pCnt+0) << endl; cout << "$300 - $399\t " << *(pCnt+1) << endl; etc.
•
•
Join Date: Jul 2005
Posts: 1,755
Reputation:
Solved Threads: 283
In this program it's probably easier to write out the display as opposed to using a loop so you could follow this outline:
You want to pass counter as an argument to display(), not declare it in display().
C++ Syntax (Toggle Plain Text)
void display(int *); int main() { const int SIZE = 9; int counter[SIZE] = {0}; //do dah display(counter); //finish up } void display(int * counter) { cout << " Range Number" << endl; cout << "$200 - $299\t " << counter[0] << endl; cout << "$300 - $399\t " << counter[1] << endl; //etc }
You want to pass counter as an argument to display(), not declare it in display().
Klatu Barada Nikto
•
•
Join Date: Jun 2009
Posts: 13
Reputation:
Solved Threads: 0
Ok, I did that, wildgoose. The compiler is now giving me an error that the line
int *pCnt = &counter[ n ];
shadows a parameter. I can't delete the declaration, because that's what links it to the original array... Also, I assumed that when you typed
void displayResults(int*pCnt, )
it should be
void displayResults(int*pCnt, int )
so that's what I made it. Was that wrong?
int *pCnt = &counter[ n ];
shadows a parameter. I can't delete the declaration, because that's what links it to the original array... Also, I assumed that when you typed
void displayResults(int*pCnt, )
it should be
void displayResults(int*pCnt, int )
so that's what I made it. Was that wrong?
Last edited by TheSilverFox; Jun 19th, 2009 at 6:32 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: c++ "error C4716: 'F_Ite' : must return a value"
- Next Thread: Debug Error Run-Time Check Failure #2
Views: 507 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






