Question about Arrays

I am working an assignment for my C++ class. I am trying to write a program with arrays but when I print the array out I am receiving numbers like -4.8e-299 and 7e-310. Does anyone know why I would be getting that type of number with an array. It prints out most of the array except it has those numbers in them. I cant post code up right now because I do not have wifi to do it. Thanks for any help.
Where is the code?
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
#include <iostream>
#include <iomanip>
using namespace std;


int person = 0;
int product = 0;
int sale = 0;
int saleTotal = 0;



int main()
{
    const int PERSON = 5, PRODUCT = 6;
    double sales[ PRODUCT ] [PERSON]= {0};
    double productSale[PRODUCT] = {0};
    int j, i;
    
    cout << "Enter sales person number (1-4), enter product number (1-5) and total sales amount: ";
    cin >> person;
    
    while(person != -1)
    {
   
        cin >> product >> sale;
        sales[person][product] += sale;
             cin >> person;
        
    }// end while
    
    cout << setw(10) << 1 << setw(10) << 2 << setw(10) << 3 << setw(10) << 4 << setw(10) << "Total \n";
    
    for (i =1; i<= PRODUCT; ++i)
    {
        cout << i ;
        
        for(j =1; j< PERSON; ++j)
        {
           saleTotal += sales[i][j];
           
            cout << setw(10) << sales [i][j]  ;
           
            
        }// end inner for loop
        
       
        
        
        cout << setw(10) << setprecision(2) << saleTotal << '\n';

        
    }// end outer for loop
    
    
    
    
    
 // end while
}// end main 


Here is my code. At least what I have so far.
Using constants to initialize and array if fine, you cannot use the same constants for an index, because it will be out of bounds.

1
2
3
4
5
const int PERSON = 5, PRODUCT = 6;

// indexes range from PERSON: 0 - 4, PRODUCT: 0 - 5
// This translates to sales[5][6] += sale;
sales[person][product] += sale;


This is also reading one 1 past the last index, and skipping the first one:

for (i =1; i<= PRODUCT; ++i)

But how is PERSON and person the same? One is all caps and the other is all lowercase. Can I not do that? person is initialized to 0 at the top of my code. Also in my code, I cant seem to figure out how to add each column up and add each row together before it prints out. RIght now as it is, it is adding all the numbers together. Any advise would be great.
Last edited on
Sorry, I missed the case difference. You are still going out of bounds in the for loop though. Try:

for (i = 0; i < PRODUCT; ++i)

Starting at 0 in the inner loop would probably help also.
Even if I do that change, the only thing it does is add another row and column. It still doesn't get rid of the 7e-310 in the last row. I am just trying to get rid of that last row of garbage for the total of the columns. Make sense?
You are getting garbage for one of two reasons. One, you are going out of bound at some point. Two, you are not initializing that particular index to anything.

I have not tried to run your code, but if it is not one of those reasons, I would be surprised.

<edit>
Making the changes I suggested, this is the output I get running it:

Enter sales person number (1-4), enter product number (1-5) and total sales amou
nt: 1
4
56
-1
         1         2         3         4   Total
0         0         0         0         0         0         0
1         0         0         0         0        56        56
2         0         0         0         0         0        56
3         0         0         0         0         0        56
4         0         0         0         0         0        56
5         0         0         0         0         0        56
Press any key to continue . . .


<edit 2>
The reason you have too many columns should be obvious if you look at what you did for a minute.
Last edited on
Is it only me that is getting the garbage? I am normally getting the garbage in row 5. I don't understand why you aren't getting it. I don't want to see or use 0 in my code. That is why I started at 1 for I and j.
Last edited on
Arrays start at 0. As does everything else in programming. There is no way to get around it. What is it about 0 that bothers you so much?

<edit>
BTW, is closer to what you are expecting for output?

Enter sales person number (1-4), enter product number (1-5) and total sales amou
nt: 2
4
76
-1
         1         2         3         4   Total
0         0         0         0         0         0
1         0         0         0         0         0
2         0         0         0         0         0
3        76         0         0         0        76
4         0         0         0         0        76
Press any key to continue . . .
Last edited on
I know that arrays start at 0. But for my assignment it is to print out 1-4 and a total for the columns and 1 - 5 for the rows. That si why I don't want to print out the 0. But even when I make the changes you suggested, I still get the weird -2.6e-29 numbers in the last row. I don't know how or what I am doing wrong. I am using XCode to write and compile my program. Could there be something wrong with XCode?
Last edited on
That should not matter. The problem is you wanting to avoid something that every programmer has to deal with. It is not hard to add 1 to 0 in your code when needed, trying to work around it just creates problems, as you noticed.

I really do not understand what your final output should be, or input, for that matter, but I am not going to write it for you either. I changed it to 5 rows and 4 columns in that last printout if that helps any. As long as you try to avoid using 0 for indexes, you will spend more time trying to make it work than anything else, you are pushing a boulder uphill.
Your right. I think I figured it out. I was stupid and even after your suggestion to change the for (i =1; i<= PRODUCT; ++i) I was still looking at your code and saw for (i =0; i< PRODUCT; ++i) I didn't notice that I should of taken the = out. A dumb mistake on my part. Thank you for your help.

So essentially what I was doing was printing out the element of the array that I was not even trying to use. SO what you said about not initializing an element is what was happening. I just did not realize how I was doing it until now. Thanks again.
Last edited on
Glad you figured it out.
Topic archived. No new replies allowed.