need assistance on setting up arrays

I am trying to learn arrays and having issues creating the arrays I need to keep a program running. I am asking for help from anyone who can show me how to set up the arrays i need. I need a total of four arrays. I have composed a very basic idea I would like to get running and fully develop my program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// *****************************************************************************
// Program: Payroll
// Author:  
// Date:    04/11/2012
// Purpose: The program should relate the data in each array through the 
//          subscipts. The program should display each employye number and ask 
//          the user to enter tha employees hours and pay rate. IT should then 
//          calculate the gross wages for that employee and store in the wages.
//          After the data has been entered for all employees the program should
//          display each employee identification number and gross wages.
//          Input Validation: DO not Accept negative values for hours, or
//          numbers less than 6.00 for pay rate.
// *****************************************************************************

# include <iostream>
using namespace std;

int main()
{
    int EMPID[7]={5658845,4520125,7895122,8777541,8451277,1302850,7580489};
    int hours[EMPID];
    double payRate[EMPID];
    double wages[EMPID];
       
    // get the hours worked by each employee.
    cout << "Enter the hours worked by " << EMPID << " employee: ";
    cin >> hours[0];
    cin >> hours[1];
    cin >> hours[2];
    cin >> hours[3];
    cin >> hours[4];
    cin >> hours[5];
    cin >> hours[6];
    
    // get the pay rate for each employee.
    cout << "Enter the pay rate for " << EMPID << " employee: ";
    cin >> payRate[0];
    cin >> payRate[1];
    cin >> payRate[2];
    cin >> payRate[3];
    cin >> payRate[4];
    cin >> payRate[5];
    cin >> payRate[6];
    
    // compute gross pay
    wages[0]=hours[0]*payRate[0];
    wages[1]=hours[1]*payRate[1];
    wages[2]=hours[2]*payRate[2];
    wages[3]=hours[3]*payRate[3];
    wages[4]=hours[4]*payRate[4];
    wages[5]=hours[5]*payRate[5];
    wages[6]=hours[6]*payRate[6];
    
    // display employee number and gross pay
    cout<<"Employee Number " <<" Gross wages " << endl;
    cout<< EMPID[0] << wages[0] << endl;
    cout<< EMPID[1] << wages[1] << endl;
    cout<< EMPID[2] << wages[2] << endl;
    cout<< EMPID[3] << wages[3] << endl;
    cout<< EMPID[4] << wages[4] << endl;
    cout<< EMPID[5] << wages[5] << endl;
    cout<< EMPID[6] << wages[6] << endl;
    
    system("pause");
    return 0;
}

hi dsustudent59

dsustudent59 wrote:
I am asking for help from anyone who can show me how to set up the arrays i need. I need a total of four arrays.


you already have the 4 arrays in the above code. you need an explanation on the arrays, or are the codes not working?
Last edited on
It would look a lot better, and be easier to understand, written as:
1
2
3
4
5
// get the hours worked by each employee.
for(int x=0;x<7;x++)
{
    cout << "Enter the hours worked by " << EMPID[x] << " employee: ";  // Will also print workers ID #
    cin >> hours[x]; // Steps through each of the seven wokers 

Do pretty much the same for pay rate section, compute gross etc.
Errors on line 21, 22 and 23. Tip: Inside [].
altbdoor

I can not seem to get them to work. I get complie errors with lines 20-23. A explanation would be useful as well.

whitenite

thank you for showing me this. I was considering setting the program up as a nested for loop once I was able to check and verify no problems with the values.

EssGeEich

from the examples I have it is suppose to be done inside the []. Am I wrong in thinking this. If so, please show the correct way.
@dsustudent59

1
2
3
int main()
{
    int EMPID[7]={5658845,4520125,7895122,8777541,8451277,1302850,7580489};

when we declare arrays, it is a must to know the size of the array. for example, if we need say, three chars in an array, we have char x[3]. your code above achieves that, by creating an array of 7 int of EMPID.

1
2
3
    int hours[EMPID];
    double payRate[EMPID];
    double wages[EMPID];

here's where things go wrong. as said, size of array must be declared. when you declare these three arrays, you declare them the size of EMPID. erm. there is no size EMPID! hence, the compilation error on line 20-23. as pointed out by @EssGeEich, something is wrong with the insides of the square brackets.

the correct way is to declare them as follow
1
2
3
    int hours[7];
    double payRate[7];
    double wages[7];

since you declared 7 EMPID, i assume you want to store the information of 7 employees. hence all of them are declared as array of 7 ints as well.

hope this helps
Exactly. Because declaring
 
int hours[EMPID];

1. Does not work. EMPID is a variable.
2. Even if it was going to work, it is going to generate as many integers as the Memory Address of EMPID.
You may have:
 
int hours[sizeof(EMPID) / sizeof(int)];

Or, even better:
 
int hours[sizeof(EMPID) / sizeof(EMPID[0])];
Last edited on
Ok. First thank you to everyone.
I have pieced together the program the way I am wanting to have it done. I am not sure it when work. PLease share your input and show me how i should code it to ensure functionaity.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// *****************************************************************************
// Program: Payroll
// Author:  
// Date:    04/11/2012
// Purpose: The program should relate the data in each array through the 
//          subscipts. The program should display each employye number and ask 
//          the user to enter tha employees hours and pay rate. IT should then 
//          calculate the gross wages for that employee and store in the wages.
//          After the data has been entered for all employees the program should
//          display each employee identification number and gross wages.
//          Input Validation: DO not Accept negative values for hours, or
//          numbers less than 6.00 for pay rate.
// *****************************************************************************

# include <iostream>
using namespace std;

int main()
{
    int EMPID[7]={5658845,4520125,7895122,8777541,8451277,1302850,7580489};
    int hours[sizeof(EMPID) / sizeof(EMPID[0])]; 
    double payRate[7];
    double wages[7];
       
   for(int ctr=0;ctr < EMID; hours[0]++; payRate[0]++; wages[0]++; ctr++);
 {
  cout << "Enter the hours worked by " << EMPID << " employee: ";
  cin >> hours[0];
  cout << "Enter the pay rate for " << EMPID << " employee: ";
  cin >> payRate[0];
  wages[0]=hours[0]*payRate[0];
  } 
    
    // display employee number and gross pay
    cout<<"Employee Number " <<" Gross wages " << endl;
    cout<< EMPID[0] << wages[0] << endl;
    cout<< EMPID[1] << wages[1] << endl;
    cout<< EMPID[2] << wages[2] << endl;
    cout<< EMPID[3] << wages[3] << endl;
    cout<< EMPID[4] << wages[4] << endl;
    cout<< EMPID[5] << wages[5] << endl;
    cout<< EMPID[6] << wages[6] << endl;
    
    system("pause");
    return 0;
}
@dsustudent59
1
2
3
4
5
6
7
8
for(int ctr=0;ctr < EMID; hours[0]++; payRate[0]++; wages[0]++; ctr++);
 {
  cout << "Enter the hours worked by " << EMPID << " employee: ";
  cin >> hours[0];
  cout << "Enter the pay rate for " << EMPID << " employee: ";
  cin >> payRate[0];
  wages[0]=hours[0]*payRate[0];
  } 

something is very wrong here. are you not well-versed in for loop and array operations? if so, please read up on here http://www.codingunit.com/cplusplus-tutorial-arrays-arrays-and-loops

in short, hours[0]++; payRate[0]++; wages[0]++; ctr++ shouldn't be there. you'd understand if you read up on the tutorial. and to display the employee number and wages, you could use a for loop as well.

hope this helps.
Sorry I am very new to programming and am not well versed with loops or array. The link does have useful information. I have changed the code to better fit. what I am trying to figure out is the program should relate the data in each array through the subscipts. The program should display each employee number and ask the user to enter tha employees hours and pay rate. It should then calculate the gross wages for that employee and store in the wages .After the data has been entered for all employees the program should display each employee identification number and gross wages. I am close to having a prototype program. I will be ver grateful for help in ensuring the arrays function as they are needed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

// *****************************************************************************
// Program: Payroll
// Author:  
// Date:    04/11/2012
// Purpose: The program should relate the data in each array through the 
//          subscipts. The program should display each employye number and ask 
//          the user to enter tha employees hours and pay rate. IT should then 
//          calculate the gross wages for that employee and store in the wages.
//          After the data has been entered for all employees the program should
//          display each employee identification number and gross wages.
//          Input Validation: DO not Accept negative values for hours, or
//          numbers less than 6.00 for pay rate.
// *****************************************************************************

# include <iostream>
using namespace std;

int main()
{
    int EMPID[7]={5658845,4520125,7895122,8777541,8451277,1302850,7580489};
    int hours[EMPID];
    double payRate[EMPID];
    double wages[EMPID];
    
    for(int counter=0; counter < EMPID; counter++)
         for(int ctr=0; counter < EMPID; ctr++)
         {
              cout << "Enter the hours worked by " << EMPID << " employee: ";
              cin >> hours[0];
              cout << "Enter the pay rate for " << EMPID << " employee: ";
              cin >> payRate[0];
         }
    
    // display employee number and gross pay
    cout<<"Employee Number " <<" Gross wages " << endl;
    cout<< EMPID[0] << wages[0] << endl;
    cout<< EMPID[1] << wages[1] << endl;
    cout<< EMPID[2] << wages[2] << endl;
    cout<< EMPID[3] << wages[3] << endl;
    cout<< EMPID[4] << wages[4] << endl;
    cout<< EMPID[5] << wages[5] << endl;
    cout<< EMPID[6] << wages[6] << endl;
    
    system("pause");
    return 0;
}

@dsustudent59

all the EMPIDs have returned! lol. okay i'll try my best to explain then.

you wanted to have an array of 7 data, with data types of int, double, etc...

so you first made
int EMPID[7]={5658845,4520125,7895122,8777541,8451277,1302850,7580489};
which is right, we have 7 employees here


1
2
3
    int hours[EMPID];
    double payRate[EMPID];
    double wages[EMPID];

considering how you wanted 7 data for 7 employee, why not declare 7 of them? why declare them to have size of EMPID? it is probably complex to understand, but the simplest level to understand is the statement from @EssGeEich
1. Does not work. EMPID is a variable.

you cannot declare an array with the size of the variable. as smart as a computer may seem, it didn't know that you meant it to be 7.

hence, declare all of them as 7. int hour[7], etc etc[7]... simply because you needed 7 of them. if 10? then 10. if 100? then 100.

these are called one-dimensional array. someday you'd learn two-dimensional and structures, but let's not get into those yet ;)


1
2
3
4
5
6
7
8
    for(int counter=0; counter < EMPID; counter++)
         for(int ctr=0; counter < EMPID; ctr++)
         {
              cout << "Enter the hours worked by " << EMPID << " employee: ";
              cin >> hours[0];
              cout << "Enter the pay rate for " << EMPID << " employee: ";
              cin >> payRate[0];
         }

it seems like you have grappled the idea of for loops and array, as seen from the bold line above. but you were using it wrongly.

an array consist of elements, starting the count from zero. for example
int EMPID[7]={5658845,4520125,7895122,8777541,8451277,1302850,7580489};
element 0 in EMPID is 5658845
element 1 in EMPID is 4520125
element 6 in EMPID is 7580489

wait what happened to my seven? array elements start their count from zero. zero to six, is equivalent from one to seven. humans count from one, computers count from zero.

long message indeed. i hope you can confirm with me that you understand these wall of text before i proceed :)

hope this helps.
Your comments are very useful and I thank you for your time. I do apologize for being sold difficult I do suffer from SLD which stands for specific learning disabilities. This is something I've always been intrigued by. Okay so I went into and change variables to read number seven. In trying to recompiled programs for testing I get error messages tell me it is forbidding the use of the counter on line 25. What I'm trying to do is have a loop inside of another loop. The inside loop should prompt the user for data and performs calculations. It go back to the outer loop and repeats for all employees. After the last loop, I would like it to display on the console the employee id number and the grosspay. I have a solid idea on the output that I want. I do need assistance in getting the glue set up correctly if you would be so kind to steer me in the right direction I would be very appreciative.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// *****************************************************************************
// Program: Payroll
// Author:  
// Date:    04/11/2012
// Purpose: The program should relate the data in each array through the 
//          subscipts. The program should display each employye number and ask 
//          the user to enter tha employees hours and pay rate. IT should then 
//          calculate the gross wages for that employee and store in the wages.
//          After the data has been entered for all employees the program should
//          display each employee identification number and gross wages.
//          Input Validation: DO not Accept negative values for hours, or
//          numbers less than 6.00 for pay rate.
// *****************************************************************************

# include <iostream>
using namespace std;

int main()
{
    int EMPID[7]={5658845,4520125,7895122,8777541,8451277,1302850,7580489};
    int hours[7];
    double payRate[7];
    double wages[7];
    
    for(int counter=0; counter < EMPID; counter++)
         for(int ctr=0; counter < EMPID; ctr++)
         {
              cout << "Enter the hours worked by " << EMPID << " employee: ";
              cin >> hours[0];
              cout << "Enter the pay rate for " << EMPID << " employee: ";
              cin >> payRate[0];
         }
    
    // display employee number and gross pay
    cout<<"Employee Number " <<" Gross wages " << endl;
    cout<< EMPID[0] << wages[0] << endl;
    cout<< EMPID[1] << wages[1] << endl;
    cout<< EMPID[2] << wages[2] << endl;
    cout<< EMPID[3] << wages[3] << endl;
    cout<< EMPID[4] << wages[4] << endl;
    cout<< EMPID[5] << wages[5] << endl;
    cout<< EMPID[6] << wages[6] << endl;
    
    system("pause");
    return 0;
}

              
@dsustudent59

don't thank me yet ;) let's move towards improving your understanding while making this work, shall we? :)

i believe you've understood the concept of array elements as stated above, and the concept of for loops.

in order to traverse through an array, that is, to go from element 0 to nth element, we usually use a for loop. here's a basic syntax of a for loop
1
2
for (/*start*/; /*condition*/; /*update*/
    /*action!*/


alrighty, with this, lets compare with the code you have below
1
2
    for(int counter=0; counter < EMPID; counter++)
         for(int ctr=0; counter < EMPID; ctr++)

int counter is zero as start, right. we must start from element 0.

counter less than EMPID? remember what i said about EMPID? EMPID (even here) is a variable. you should place the max number of elements in here.

word of caution. lets say i have int x[3], okay? here's a common mistake by most C students (in my class that is). a for loop runs until the condition is false. observe the condition below
1
2
3
4
for (int count=0; count<=3; count++)
{
    //action!
}

note the less than or equals to sign. lets start! count starts from zero.
so, count is 0. is 0 less than or equals to 3? true. perform action, update.
so, count is now 1. is 1 less than or equals to 3? true. perform action, update.
so, count is now 2. is 2 less than or equals to 3? true. perform action, update.
so, count is now 3. is 3 less than or equals to 3? true. perform action, update.
so, count is now 4. is 4 less than or equals to 3? false. stop loop.

how many times did the action take place? FOUR times!
whats the size of your array? THREE!
what are the elements in the array? count[0], count[1] and count[2]!
you did the loop 4 times, accessing 0, 1, 2 and 3.

there is no count[3], is it? no. there isn't! but you've done the loop 4 times! you should get a runtime error here.

the fix is to use less than only. so it only goes, 0, 1, and finally 2, accessing all the elements of my count array! voila :D

i believe this could help a lot in your code, but one more problem...

1
2
    for(int counter=0; counter < EMPID; counter++)
         for(int ctr=0; counter < EMPID; ctr++)

why two for loops? you only need one :)

dsustudent59 wrote:
What I'm trying to do is have a loop inside of another loop. The inside loop should prompt the user for data and performs calculations. It go back to the outer loop and repeats for all employees. After the last loop, I would like it to display on the console the employee id number and the grosspay.

no, you still do not need a for loop, in a for loop. nested for loops are complex levels of iteration, and you certainly do not need them here.

here's a logic example. i hope this helps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# include <iostream>
using namespace std;

int main()
{
    int EMPID[7]={5658845,4520125,7895122,8777541,8451277,1302850,7580489};
    int hours[7];
    double payRate[7];
    double wages[7];
    //we were right up to here :D

    for (/*count=0*/; /*count less than...?*/; /*add count*/)
    {
        //get the employee working hours

        //get the employee payrate

        //calculate the wages. basic formula is to have 'wages=hour*payrate'. 
        //try implementing the array logic we've learned. 
        //remember, EMPID[0], has worked for hours[0] hours 
        //and his payrate is payrate[0]. his wages should be in wages[0]
    }

    //we're done with the calculation. let's show them out. 
    //note, always separate calculation and display processes. 
    //they may get more complex!
    for (/*count=0*/; /*count less than...?*/; /*add count*/)
    {
        //show the wages!
    }
     system("pause");
    return 0;
}
@altbdoor
you cannot declare an array with the size of the variable. as smart as a computer may seem, it didn't know that you meant it to be 7.

The size of a pre-declared array (
1
2
int Array[7];
sizeof(Array) // <- I mean this 

) INCLUDES the number of items. What i mean is:
1
2
3
int Array[7];
std::cout << sizeof(Array) << "=" << sizeof(int) << "*7" << std::endl;
std::cout << sizeof(int) << "=" << sizeof(Array) << "/7" << std::endl;

And, sizeof returns CONSTANT values. So you can declare arrays with sizeof.

EDIT: Sizeof's result is measured in bytes. So, 4 (sizeof(int) on most pcs), means 4 bytes.
Last edited on
@EssGeEich

i understand that the method you mentioned was possible. i had hoped to focus on making @dsustudent59 understand the basic concept of array with number as declarations before delving into sizeof. i apologize if i had cause any confusion.
altbdoor wrote:
i had hoped to focus on making dsustudent59 understand the basic concept of array with number as declarations before delving into sizeof.

Whoops, i didn't tought of it.
Topic archived. No new replies allowed.