Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,019 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,480 people online right now. Registration is fast and FREE... Join Now!




Populate an array with data

 
Reply to this topicStart new topic

Populate an array with data

learning slowly
30 Nov, 2006 - 11:13 AM
Post #1

New D.I.C Head
*

Joined: 30 Nov, 2006
Posts: 3


My Contributions
I get the salary amount and now I need to populate the money [] array with the salary. Since there will be more then one person’s salary entered, I’m guessing I’ll need a counter = 0? To capture more then one? I'm totally lost and i'm so stressed i can't think straight and the project is due tonight, please help.



CODE

void wages ( int money[] )    // function definition skeleton only
{
    // variable declarations
    double         sales,                 // gross sales for the week
                commissionRate = 0.09,        // a percent of gross sales
                salary;                // for current salesperson
        

// input control loop here

do {
        cout <<"Enter employee gross sales ( -1 to end )\n";
        cin >> sales;    //keep gross in keyboard buffer
        
        salary = ((sales *.09) + 200);    //calculate salary
        cout<< setw(3)<< "Employee commission is $" << setw(6)<< salary << endl;
        
        //cout<< setw(24)<< salary << endl; //verify calculation
        

}
while(sales >= 0);
  
  
    
}


edit: modified title ~ jayman9
User is offlineProfile CardPM
+Quote Post

y0da
RE: Populate An Array With Data
30 Nov, 2006 - 11:18 AM
Post #2

D.I.C Head
Group Icon

Joined: 8 Nov, 2006
Posts: 62


Dream Kudos: 75
My Contributions
Drink some soda and take a nap then in a couple of hours go back to programming I took a look at it and didn't see where you are wrong in this snippit so far but then again I could be wrong...
User is offlineProfile CardPM
+Quote Post

learning slowly
RE: Populate An Array With Data
30 Nov, 2006 - 11:35 AM
Post #3

New D.I.C Head
*

Joined: 30 Nov, 2006
Posts: 3


My Contributions
I tried that, it was a lot worse then this before>:) I'm not understanding how the money [] array is or if it's getting populted with the salary.
The whole idea of that function is to populate the money[] array with the salary. After calculating salary I need to add it to the ith element of the money[] array. I need to have to add an integer counter to keep track of the current row number in the array.

User is offlineProfile CardPM
+Quote Post

BitByte
RE: Populate An Array With Data
30 Nov, 2006 - 11:38 AM
Post #4

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 3 times
My Contributions
Have a break from it for a while like yoda said. Go for a drink of coffee somewhere quiet and think it over in your head or write it down. How would you tackle the problem in real life. Then go back to your computer and try them out. Getting too worked up about it just makes it all the more frustrating.

What is the size of the array that you are passing to the function? You need to pass the the size of the array to the function aswell.
User is offlineProfile CardPM
+Quote Post

BitByte
RE: Populate An Array With Data
30 Nov, 2006 - 11:45 AM
Post #5

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 3 times
My Contributions
An array is just like a long line of boxes. Think of it like that. Your array starts out at zero to one less than the maximum size. So for 10 you have elements 0 to 9. Have yourself a counter initialized to zero. This will be the position in the array. Now get the salary amount and all you need to do is this

CODE
myArray[counter] = salary;


That will put your salary in position 0. Now just increase your counter at the bottom of your loop so the counter now represents the next position ( or box ) in the array.

Edit: Don't know what happened there, ended up with two of the same thing blink.gif

This post has been edited by BitByte: 30 Nov, 2006 - 12:40 PM
User is offlineProfile CardPM
+Quote Post

cipherence
RE: Populate An Array With Data
30 Nov, 2006 - 12:46 PM
Post #6

D.I.C Regular
Group Icon

Joined: 1 Apr, 2006
Posts: 260



Thanked: 1 times
Dream Kudos: 650
My Contributions
Man, i can't wait to get into college, with all these terms i have no idea what they mean. Totally stoked. but anyway, i can't see any problems with it either, so i think you should def. try what y0da said.
User is offlineProfile CardPM
+Quote Post

learning slowly
RE: Populate An Array With Data
1 Dec, 2006 - 05:08 AM
Post #7

New D.I.C Head
*

Joined: 30 Nov, 2006
Posts: 3


My Contributions
Can someone tell me if my void wages ( int money[] ) looks even close? I'm still stuck on my array. thanks





CODE

#include <iostream>

using std::cout;
using std::cin;
using std::endl;
using std::ios;

#include <iomanip>

using std::setw;
using std::setiosflags;
using std::setprecision;

//function prototypes
void wages( int [] );        // function to calculate each salesperson's salary + commission
                //   and to increment appropriate counter
void display( const int [] );    // function to display the cumulative results in array of counters

int main()    //
{
    //variable declarations
    int salaries [ 11 ] = { 0 };    // array of counters to track number of salespeople
                

    // calculate salaries and display results
    wages( salaries );
    display( salaries );

return 0;

} //end main

void wages ( int money[] )    
{
    // variable declarations
    double     sales,                 // gross sales for the week
        commissionRate = 0.09,        // a percent of gross sales
        salary;                // for current salesperson
        int counter;
        int saleries [ 10 ];
        saleries[counter] = salary;
        



    // input control loop here

do {
        cout <<"Enter employee gross sales ( -1 to end )\n";
        cin >> sales;    //keep gross in keyboard buffer
        
        salary = ((sales *.09) + 200);    //calculate salary
        cout<< setw(3)<< "Employee commission is $" << setw(6)<< salary << endl;

        for ( int i = 0; i < 10; i++ )
            saleries[ i ] = 0;    // set element at location i to 0
                
        //cout<< setw(24)<< salary << endl; //verify calcutation

        

}
while(sales >= 0);
  
  
    
}


void display ( const int dollars[] )        
{
    // variable declarations
//    int index;            // array subscript variable


//    for ( index = 0; index < saleries; index++ ) {

//        if (salary >= 200 && salary <= 299)
//            i1++;
//        if (salary >= 300 && salary <= 399)
//            i3++;
//        if (salary >= 400 && salary <= 499)
//            i4++;
//        if (salary >= 500 && salary <= 599)
//            i5++;
//        if (salary >= 600 && salary <= 699)
//            i6++;
//        if (salary >= 700 && salary <= 799)
//            i7++;
//        if (salary >= 800 && salary <= 899)
//            i8++;
//        if (salary >= 900 && salary <= 999)
//            i9++;
//        if (salary >= 1000)
//            i10++;

//    } // end for



} // end display






This post has been edited by learning slowly: 1 Dec, 2006 - 05:10 AM
User is offlineProfile CardPM
+Quote Post

sidharth
RE: Populate An Array With Data
1 Dec, 2006 - 07:27 AM
Post #8

New D.I.C Head
Group Icon

Joined: 10 Nov, 2006
Posts: 38


Dream Kudos: 550
My Contributions
u r doing right
but use
global counter
& put it to 0
& increment it every time u go one circle in (do, while) loop
where have u declare money[] array .Declare it in int main
change declaration of wages(saleries) to wages(money)
& then try

User is offlineProfile CardPM
+Quote Post

BitByte
RE: Populate An Array With Data
1 Dec, 2006 - 08:15 AM
Post #9

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 3 times
My Contributions
Here is how you pass an array to a function:

CODE
#include <iostream>

void printArray ( const int arr[], size_t sz );

int main()
{
    const size_t SIZE = 10;
    int myArray[SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    printArray( myArray, SIZE );

    return 0;
}

// Pass the array, and the size of the array.
void printArray ( const int arr[], size_t sz )
{
    for ( size_t i = 0; i < sz; i++ )
    {
        std::cout << "Array element " << i << " = " << arr[i];
        std::cout << std::endl;
    }
}


Have a look at that to see how it works.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 09:13PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month