First 2-D Array

Pages: 123
Hello again, I have another program which introduced our class to 2D array's. Our book has very little information about 2D, and I'm a little confused.

Below I am trying to just simply read the file into my "out" file. But I keep getting junk in my array. I've tried following the somewhat same routine in my notes but its just not functinoing properly.

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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;
const int ROW=13, COl=14;
void read(int[][COl],int&);
void print(int[][COl], int&);

ofstream out("Wild.out");
ifstream inf("Wild.in");



void read(int ary[][COl],int& counter)
{
    out<< setprecision(2) << fixed;
    int x=0;
    while(inf >> ary[ROW][COl])
    {
       for(int y=0;y<COl;y++)
        {
            x++;
        }
        counter=x;
    }


}
void print(int ary[][COl])
{
    for(int x=0; x<ROW;x++)
    {
        for(int y=0; y<COl;y++)
        {
            out << ary[x][y] << ", ";
        }
        out << endl;
    }


}
int main()
{
    int counter,ary[ROW][COl];
    read(ary,counter);
    print(ary);




    out.close();
    inf.close();
    return 0;
}
Look at this snippet:

1
2
3
4
5
void read(int ary[][COl],int& counter)
{
    out<< setprecision(2) << fixed;
    int x=0;
    while(inf >> ary[ROW][COl])


Do you realize that you're attempting to access your array out of bounds in that last line (see your print function to see how to properly access array elements)?

Remember that arrays start at zero and end at size - 1, not size.

Next since you're using arrays you need to insure not to overflow the array sizes in your data entry loop.

Next this snippet:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
...
ofstream out("Wild.out");
ifstream inf("Wild.in");

...

int main()
{
    int counter,ary[ROW][COl];
    read(ary,counter);  // Why are you passing an uninitialized value (counter) to this function?
    print(ary);




    out.close();  // Not needed, and really not a good idea. Let the class destructors 
    inf.close();  // do their jobs, especially with horrible global variables.
    return 0;
}

Not only are you going past the boundaries of your 2D array, as jlb points out, you are reading all your data into a single element. The rest of the elements of your array are full of uninitialized garbage values.
I'm just copying waht I have in my notes. I'm not exactly sure what I am doing.
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
void read(ifstream& inf,int ary[][COl])
{
    out<< setprecision(2) << fixed;

    for(int x=0;x<ROW;x++)
    {
       for(int y=0;y<COl;y++)
        {
            inf >> ary[x][y];
            if(!inf)
            {
                cout << "Error Reading file for element "<< x << ", "
                     << y << endl;
            }
        }
    }
}
void print(int ary[][COl])
{
    for(int x=0; x<ROW;x++)
    {
        for(int y=0; y<COl;y++)
        {
            out << ary[x][y] << ", ";
        }
        out << endl;
    }
}

Can you show me via syntax, how I am messing up?
Your new functions will read the array, but they don't match the signature of the original functions. For example, the original read() function had this signature:
void read(int[][COl],int&);
and the new one has this:
void read(ifstream& inf,int ary[][COl])
Why does the new one take the stream as a parameter? Why does it not take the int&? What is the purpose of the int&?

You're probably thinking that these questions are pedantic, and in some ways your right. What I'm really getting at here is that you need to be crystal clear about what the parameters (and return value, if applicable) of your function are. After all, if the meaning of the parameters is vague, then how can you possibly write code that uses them correctly? Decide what these parameters are, write it down in a comment before the function. Then write the code to match.

In other words, design your software first, then write the code.
Hello CodeNovice01,

In addition to what dhayden has said I would have to ask why are you passing a global variable to the function? The file stream was defined as a global variable, so the function already has access to it.

Or did you change something that you did not show?

I believe I said this once before. The "ifstream" and "ofstream" should be defined in the function that need it or in "main" and passed to the function.

The code should look more like this:
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
void read(int ary[][COl], int& counter) // <--- Counter is never used.
{
	//out << setprecision(2) << fixed; // <--- Should be in the "print" function not here. And Not needed.

	std::string inFileName{ "Wild in.txt" };

	std::ifstream inFile(inFileName);

	if (!inFile)
	{
		std::cout << "\n File " << std::quoted(inFileName) << " did not open" << std::endl;
		//std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread". Optional.
		/*return 1;*/  exit(1);  // If not in "main".
	}

	for (int row = 0; row < ROW; row++)
	{
		for (int col = 0; col < COl; col++)
		{
			//inFile >> ary[row][col];

			if (!(inFile >> ary[row][col]))
			{
				cout << "Error Reading file for element " << row << ", "
					<< col << endl;
			}
		}
	}
}

The only problem is that if the input file is less than 13 rows your output could look like this:

10, 12, 81, 33, 53,  1, 34, 98,  1, 27, 98, 26, 52, 33
34, 49, 61, 55, 61, 15, 96, 43, 64, 79, 96, 61, 71, 80
83, 72, 79, 66, 53, 51, 30, 29, 27, 10, 73, 49, 53, 78
88, 31,  4, 22, 65, 17, 85, 56, 42, 84, 96, 75, 36, 56
96, 98, 32, 73, 35, 23, 42, 37, 24, 67, 86, 53,  5, 50
41, 81,  0, 32, 59, 88, 68, 40, 33,  7, 33, 70, 37, 55
39, 52, 96, 67, 26, 97, 45, 92, 32, 10,  5,  8, 25, 13
10, 12, 81, 33, 53,  1, 34, 98,  1, 27, 98, 26, 52, 33
34, 49, 61, 55, 61, 15, 96, 43, 64, 79, 96, 61, 71, 80
83, 72, 79, 66, 53, 51, 30, 29, 27, 10, 73, 49, 53, 78
88, 31,  4, 22, 65, 17, 85, 56, 42, 84, 96, 75, 36, 56
-858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460
-858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460


Now if you had initialized the array when you defined it in "main" int counter{}, ary[ROW][COl]{}; the output would be:

10, 12, 81, 33, 53,  1, 34, 98,  1, 27, 98, 26, 52, 33
34, 49, 61, 55, 61, 15, 96, 43, 64, 79, 96, 61, 71, 80
83, 72, 79, 66, 53, 51, 30, 29, 27, 10, 73, 49, 53, 78
88, 31,  4, 22, 65, 17, 85, 56, 42, 84, 96, 75, 36, 56
96, 98, 32, 73, 35, 23, 42, 37, 24, 67, 86, 53,  5, 50
41, 81,  0, 32, 59, 88, 68, 40, 33,  7, 33, 70, 37, 55
39, 52, 96, 67, 26, 97, 45, 92, 32, 10,  5,  8, 25, 13
10, 12, 81, 33, 53,  1, 34, 98,  1, 27, 98, 26, 52, 33
34, 49, 61, 55, 61, 15, 96, 43, 64, 79, 96, 61, 71, 80
83, 72, 79, 66, 53, 51, 30, 29, 27, 10, 73, 49, 53, 78
88, 31,  4, 22, 65, 17, 85, 56, 42, 84, 96, 75, 36, 56
 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0



I also changed the print function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void print(int ary[][COl], const int counter) // <--- Counter is never used.
{
	ofstream out("Wild out.txt");

	for (int row = 0; row < ROW; row++)
	{
		for (int col = 0; col < COl; col++)
		{
			out << std::setw(2) << ary[row][col] << (col + 1 < COl ? ", " : "\n");
		}

		//out << endl;
	}
}

Now here checking the status of the file stream is not as necessary because if the file does not exist it will create the file. If the file name had a path to the file then checking the stream states, as I did in the "read" function, would help. Many times I have seen an improper path keep a file from opening or being created.

Again file extensions of "in" and "out" are cute, but the file is a text file and the file extension should reflect this. Better to learn a good habit using the correct file extension than to try to unlearn a bad habit.

Andy
I'm not sure what is going on. I altered things via the video my professor uploaded, and i'm still getting garbage in my array.
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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

void read(int[][14], int&);
void print(int[][14],int);
void header();

ifstream inf;
ofstream out;


void read(int ary[][14], int& n)
{
    int r=0;
    while(inf >> ary[r][0])
   {
        for(int c=1;c<13;c++)
        {
            inf >> ary[r][c];
            r++;
        }
    n=r;
    }
}

void header()
{
    out << "Salesman ID  " << "  " << "   \n";
}

void print(int ary[][14], int cnt)
{
    for(int x=0; x<cnt;x++)
    {
       for(int y=0; y<14;y++)
        {
            out << ary[x][y]  << " , ";
        }
        out << endl;
    }
}
int main()
{
    inf.open("Wildin.txt");
    if(!inf)
        cout << "error opening file";
    out.open("Wildout.txt");


    int cnt, salesmen[15][14];
    read(salesmen,cnt);
    header();
    print(salesmen,cnt);

   return 0;
}

OUTPUT:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Salesman ID       
2021 , 15371 , 469762076 , 0 , 9109504 , 2008463235 , 8 , 7208096 , 2008344448 , 1 , 9113168 , 8 , 2008345081 , -482698871 , 
9113160 , 9109504 , 0 , 83886085 , 2008481792 , 9109504 , 1971536980 , 0 , 0 , 9113184 , 0 , 1 , 9109504 , 3 , 
9110200 , 8 , 0 , 5 , 28 , 469762076 , 0 , 9113160 , 5 , 1 , 0 , 0 , 9110192 , 1 , 
0 , 5 , 16842780 , 469762076 , 9109696 , 7208232 , 8 , -1802093543 , 0 , 16907468 , 9113184 , 0 , 2008640768 , 8 , 
16842752 , 9113172 , 4417004 , 7208280 , 2008588160 , -1802093543 , -2 , 7208188 , 2008640731 , 0 , 2008640770 , 9113168 , 9109504 , 0 , 
9113192 , 0 , 9109504 , 0 , 7208172 , 2008341526 , 4199040 , 7244 , 9110928 , 1 , 9113460 , 9113408 , 4239420 , 4984576 , 
4979800 , 7244 , 9110928 , 1 , 9113460 , 9113408 , 4207765 , 4978388 , -482699223 , 0 , 2008482249 , 0 , -482699207 , 9113408 , 
4409216 , 0 , -482699255 , 0 , 4234597 , 4978504 , 0 , 3904 , 2008482249 , 9110928 , 4199040 , 7208616 , 4976612 , 4978504 , 
4978504 , 6 , 5010528 , 0 , 7208556 , 7208332 , 5010528 , 0 , 4199040 , 7208616 , 4280244 , 0 , 6 , 4978504 , 
5010528 , 4976612 , 4199040 , 7244 , 9110928 , 1 , 7208556 , 9113408 , 2008482249 , 9110928 , 0 , 6 , 16 , 0 , 
-482699031 , 9113408 , 4409216 , 0 , 4976612 , 28 , 9116912 , 9116940 , 1741521603 , 7208520 , 1969189250 , 9109504 , 0 , 9116912 , 
-1480645078 , 28 , 9116912 , 9116940 , 4357336 , 0 , 0 , 32 , 9116912 , 9116944 , 1741521603 , 7208584 , 1969189250 , 9109504 , 


INFILE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2021  15371.7 31660.1 1950.28 25420.3 26744.2 29213.9 8022.51 30317.7 12677.9 2430.61 14591.5 32974.8 17353.2
1178  27352.5 28583.3 30570.9 23609.4   20661   33381 16392.5 19339.5 31867.9 16165.2  7635.1 11454.8 26701.3
5141  22977.5 9582.07 3783.97  7096.1 31259.8 9007.33 15359.9 8655.28 19197.6 10245.3 19846.7 14194.6 14267.2
8347  11046.2 1989.22 28403.4 12368.8   26048 20386.1 25574.2 11926.6 22469.5 33300.9 3614.23   29331 20758.7
6577  1629.14 20494.8 11973.8 3306.99 28215.4 2634.61 15626.3 33516.4 10389.7 20598.9 17999.3 6075.04 14371.7
2617  27756.7 24807.3 27176.1 29743.2 28108.3 5430.77 23123.1 3171.31 30190.3 24862.2  4078.1 20184.7 20719.3
2891  29704.7 16901.2 31960.8 26913.6 5122.53 21332.8 10371.1 9008.73 2575.58 33699.6 1460.37 18881.1 20793.2
7538  2255.97 14672.2 17505.8 24828.5 1610.38 20239.8 2482.95 24967.5 13105.6 28000.7 10293.9 12562.3 12939.4
2571  2498.33   15681 1364.25 12879.4 10246.7 8742.04 23562.4 16441.1 9474.13 1743.16 30240.8 24142.7 15701.6
9872  25349.7 15479.5   20428 32086.9 9661.93 15055.7 18367.5 13452.4 25951.5 7719.95 5048.39    7989 18020.8
5250  32475.2 30367.7 32545.2 2458.19 8096.35 14861.1 28754.7 19290.8 8278.71 15156.3 7571.34 32354.4 19350.8
4422  10716.9 25222.7 12523.2 21708.5 3902.61 26446.3 25864.2   32109 32603.6 4215.98 33425.3 19752.3 18636.8
1864    18041 23927.6 20282.6   20192 17539.9 30025.9   27814   14618 24522.7 17791.8 33056.6 27311.7 25219.7

Last edited on
Only the first number in each row of your input file is an integer. The rest are floats. So ary needs to be float (or double, which means double-precision float).

It would make more sense to use a struct with an int for the salesman id and an array of double for the 13 floating point numbers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const int MaxSalesmen = 50; // or whatever maximum you want
const int NumSales = 13;

struct Salesman
{
    int id;
    double sales[NumSales];
};

int main()
{
    Salesman sm[MaxSalesmen];
    // ...
}

We haven't gone over structs, pointers, vectors, yet. The functions need to be in the format i have, I just wasn't sure why it's not reading the rest of the file. I'll go change int to float and see what changes.
CHanged things to float and now it prints nothing. I'm not too concerned at this moment about the decimal value, as long as it prints the numbers I want it to print. I've been working on getting this damn code to read for 3 days and still no luck.

I'm stuck, and would like to see the syntax of my format altered to read the data I need. The format I have is what the professor wants.

Thank you
Here's how you might do it with floats. Note that the input and output streams don't need to be global (and really shouldn't be).

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
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

const int MaxRows = 50, Cols = 14;

int read(istream& in, float ary[][Cols])
{
    int r = 0;
    for (int id; in >> id; ++r)
    {
        if (r == MaxRows)
        {
            cout << "Table overflowed\n";
            break;
        }
        ary[r][0] = id;
        for (int c = 1; c < Cols; c++)
        {
            in >> ary[r][c];
        }
    }
    return r;
}

void print(ostream& out, float ary[][Cols], int rows)
{
    out << fixed;
    for (int r = 0; r < rows; ++r)
    {
        out << setprecision(0); // print id without decimals
        out << setw(4) << ary[r][0];
        out << setprecision(2); // print others with 2 decimals
        for (int c = 1; c < Cols; c++)
        {
            out << setw(9) << ary[r][c];
        }
        out << endl;
    }
}

int main()
{
    ifstream in("Wildin.txt");
    if (!in)
    {
        cout << "Error opening the input file\n";
        return 1;
    }

    ofstream out("Wildout.txt");
    if (!out)
    {
        cout << "Error opening the output file\n";
        return 1;
    }

    float salesmen[MaxRows][Cols];
    int rows = read(in, salesmen);
    print(out, salesmen, rows);
}

What was going on with my code that made it only print the first two values and rest junk?
I assume the problem with the code you posted above (http://www.cplusplus.com/forum/beginner/267497/#msg1151101) is that you tried to read floats into ints. As soon as it hit the first decimal point it would stop reading since a decimal point is illegal in an int.

But I don't know why this happened: "Changed things to float and now it prints nothing." You would have to post the code for me to look at, but maybe you've already figured out what went wrong there.
What was going on with my code


Let's say your file looks like this, file.txt:
1.2    3.4


The following program...
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <fstream>

int main()
{
    std::ifstream fin("file.txt");
    
    int number;
    while (fin >> number)
    {
        std::cout << number << '\n';
    }
}

....will print:
1


______________________________________

On the other hand, the following program...
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <fstream>

int main()
{
    std::ifstream fin("file.txt");
    
    double number;
    while (fin >> number)
    {
        std::cout << number << '\n';
    }
}

...will print:
1.2
3.4
Last edited on
The functions need to be in the format i have
void read(int ary[][14], int& n)
Infile:
2021 15371.7 31660.1 1950.28 25420.3 26744.2 29213.9 8022.51 30317.7 12677.9 2430.61 14591.5 32974.8 17353.2
[...]

This is very strange. The input file contains floating point values but the function, which you say you cannot change, reads the data into an array of integers. So either the function prototype is wrong or the input file is wrong or you're meant to convert the floating point numbers to integers.

Here is your latest read() function, properly indented:
1
2
3
4
5
6
7
8
9
10
11
12
13
void read(int ary[][14], int& n)
{
    int r=0;
    while(inf >> ary[r][0])
    {
        for(int c=1;c<13;c++)
        {
            inf >> ary[r][c];
            r++;
        }
        n=r;
    }
}


- Line 9 increases r (the row number) after reading every number.
- Line 11 sets n (the final number of rows) after reading every row.

Okay, line 11 will result in the right value being stored in n, but it's inefficient. You only need to do it once at the end.

When I fix these issues and change the salesmen to an array of double and change read() and print() to take arrays of doubles instead of arrays of ints, the code works.
Hello CodeNovice01,

I altered things via the video my professor uploaded

That is good. Is it possible to share the link to this video? At least this may answer some questions in place of trying to figure out what you can and can not do or need to do.

To go along with what jlb, Furry Gury and dhayden have said. Take a look at this bit of code:
1
2
3
4
5
6
7
8
9
10
11
12
13
void read(int ary[][14], int& n)
{
    int r=0;
    while(inf >> ary[r][0])
   {
        for(int c=1;c<13;c++)
        {
            inf >> ary[r][c];
            r++;
        }
    n=r;
    }
}

As you can see using better variable names along with some blank lines and proper indenting the code could be something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void read(int ary[][14], int& numsInArr)
{
	int row = 0;

	while (inf >> ary[row][0])
	{
		for (int col = 1; col < 13; col++)
		{
			inf >> ary[row][col];
				row++;
		}

		numsInArr = row;
	}
}

As jlb, Furry Gury and dhayden have pointed out this makes it easier to see how "row" is being incremented past the boundary of the array.

The following code will show how changing where "row" and "numsInArr" are incremented . Although "numsInArr", "cnt" back in (main)", turn out to be variables that are given a value, but never used anywhere.

That said, looking at your last code posted it looks like you are going backwards from what you started with.

Using everything I could from what you have posted I did come up with this:
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
constexpr int ROW{ 15 };
constexpr int COL{ 14 };

void read(double (&ary)[ROW][COL], int&);
void print(double ary[][COL], int cnt);
void header(ofstream& out);



void read(double (&ary)[ROW][COL], int& numsInArray)
{
	ifstream inf;

	inf.open("Sales.txt");

	if (!inf)
	{
		cout << "error opening file";
		exit(1);
	}

	int row = 0;

	while (row < ROW)
	{
		if (!(inf >> ary[row][0]))
		{
			cout << "Error Reading file for element " << row << ", "
				<< std::setw(2) << 0 << " It does not exist." << endl;
		}
		else
			numsInArray++;

		//********** An alternative. *********
		/*
		if (inf >> ary[row][0])
			numsInArray++;
		else
		{
			cout << "Error Reading file for element " << row << ", "
				<< std::setw(2) << 0 << " It does not exist." << endl;
		}

		Same could be used in the for loop.
		*/

		for (int col = 1; col < COL; col++)
		{
			if (!(inf >> ary[row][col]))
			{
				cout << "Error Reading file for element " << row << ", "
					<< std::setw(2) << col << " It does not exist." << endl;
			}
			else
				numsInArray++;
		}

		row++;

		if(!inf)
			std::cout << '\n';
	}
	
	//std::cout << std::endl; // <--- Used as a break point for testing.

}

I put back lines 1 and 2 because you should be using this instead of the magic numbers that you are using, as seen in your last code.

Since the "ifstream" is only used in the "read" function it is better to open and close the stream in the function.

Notice the if statement on line 16. I added the {}s and the "exit" to stop the program. With what you have if you print the error message the function would continue trying to read a stream that is not working and you would very likely end up with nothing in your array except the garbage that was there when the array was defined back in "main".

This way if the stream did not open then you would leave the program and have to fix the problem.

With "headings" and "print" functions I did this:
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
void header(ofstream& out)
{
	out << "Salesman\n   ID" << std::string(6,' ') << "Other" << std::string(4, ' ') << "Headings" << "\n";
}

void print(double ary[][COL], int cnt)
{
	ofstream out;

	out.open("Sales Table.txt", std::ios::app);

	if (!out)
	{
		cout << "error opening file";
		exit(2);
	}

	header(out);

	out << std::fixed << std::setprecision(2);

	for (int row = 0; row < ROW; row++)
	{
		for (int col = 0; col < COL; col++)
		{
			if (col == 0)
				out << "  " << std::setw(4) << static_cast<int>(ary[row][col]) << "    ";
				//out << std::left << std::setw(8) << (int)(ary[row][col]); // <--- An alternative.
			else
				out << std::right << std::setw(8) << ary[row][col] << ", ";

			//std::cout << std::setw(8) << ary[row][col] << ", ";
			//std::cout << ary[row][col] << ", ";
		}

		out << endl;
		//std::cout << std::endl;
	}
}

Defining the "ifstream" and "ofstream" as global variables the "ofstream" did not work for me using VS2017. When I defined the "ofstream" in the "print" function and passed it to the "header" function it worked as it should.

Also it makes more sense to define and open the "ofstream" in the "print" function along with calling the "header" function from within the "print" function.

The rest of the function should explain its self.

In "main" you have double salesmen[ROW][COL];. This is an uninitialized array and could produce a possible output of:

Salesman ID       
 2021.00, 15371.70, 31660.10,  1950.28, 25420.30, 26744.20, 29213.90,  8022.51, 30317.70, 12677.90,  2430.61, 14591.50, 32974.80, 17353.20, 
 
-92559631349317830736831783200707727132248687965119994463780864.00, -92559631349317830736831783200707727132248687965119994463780864.00, -92559631349317830736831783200707727132248687965119994463780864.00, -92559631349317830736831783200707727132248687965119994463780864.00, -92559631349317830736831783200707727132248687965119994463780864.00, -92559631349317830736831783200707727132248687965119994463780864.00, -92559631349317830736831783200707727132248687965119994463780864.00, -92559631349317830736831783200707727132248687965119994463780864.00, -92559631349317830736831783200707727132248687965119994463780864.00, -92559631349317830736831783200707727132248687965119994463780864.00, -92559631349317830736831783200707727132248687965119994463780864.00, -92559631349317830736831783200707727132248687965119994463780864.00, -92559631349317830736831783200707727132248687965119994463780864.00, -92559631349317830736831783200707727132248687965119994463780864.00, 


The last two lines are the garbage that the "cout" statement is trying to turn into a "double" based on using "std::fixed" to print the number. Otherwise they would be in scientific notation. The actual number could be "-9.2559631349317831e+61". This may be different on your computer.

Now if you would initialize your array as:
1
2
double salesmen[ROW][COL] 
{};
The output would be:

Salesman ID       
 2021.00, 15371.70, 31660.10,  1950.28, 25420.30, 26744.20, 29213.90,  8022.51, 30317.70, 12677.90,  2430.61, 14591.50, 32974.80, 17353.20, 
   0.00,     0.00,     0.00,     0.00,     0.00,     0.00,     0.00,     0.00,     0.00,     0.00,     0.00,     0.00,     0.00,     0.00, 
    0.00,     0.00,     0.00,     0.00,     0.00,     0.00,     0.00,     0.00,     0.00,     0.00,     0.00,     0.00,     0.00,     0.00, 

With the last two line being (0)s zeros.

This should cover most of what has been said.

Andy
Thank you all for the responses, they're very helpful in my understanding. I'm going to post the assignment in hope that someone can assist in understanding waht I am not.

IN this program is says I have to compute this year's monthly averages, but I only have one infile of data and thats last years monthly averages. How does one compute this years without any data?


The “Wild and Wooley” company has a large salesman staff and the company needs to gather info about the salesman’s yearly sales results. You have been tasked with this assignment. There are no more than 15 salesmen on staff and for each salesman the company has recorded for each month of the year, each salesman’s total monthly sales. Also recorded for each salesman is his ID and his last year’s average monthly sales. You are to compute for each salesman, this year’s average monthly sales. Since everyone generally has a bad month, and the company is nice, you will also calculate a second monthly average sales dropping his one worst monthly sales.

The information to be reported are the following: Print out each salesman’s ID, his last year’s average monthly sales, the current year’s monthly sales, the current year’s average monthly sales minus his worst month all in a table format (columns, headers, ect.). Also to show in the table are the salesmen who had a 10% increase for this year’s sales over last years. (use 12 month averages). These salesmen will be slated for a year in bonus. Also, show my worst salesman whose two-year average is the lowest of all the salesmen, again to be shown in the table.

List out each salesmen information in a nice table format. (The columns must be lined up.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Your input will be found in input file: “WildSalemen.dat” 

The format of the input file:
                 ID    JanSales   Feb   Mar ….   Dec   LastYrAvg

Ex input:  284  2500.00  8000.00  3050.00 7300.00 …. 1700.00    5525.00
Fields        ID    Jan            Feb         Mar      Apr               Dec       PrevYrAvg

A last minute update from the main accounting office:  
They need the total sales for each month to see how the company
 is doing for the year.  
Generate a second table with the additional information:

Format:      
		     Total Monthly Sales
   	January        February        March    ……                                        Dec
     	25000.00      82000.00     78525.00     …….                              19880.00
Last edited on
I'm unable to upload the video. It's unable to be downloaded.
Hello CodeNovice01,


IN this program is says I have to compute this year's monthly averages, but I only have one infile of data and thats last years monthly averages. How does one compute this years without any data?


Given this:

The format of the input file:
                 ID    JanSales   Feb   Mar ….   Dec   LastYrAvg

2021 15371.7 31660.1 1950.28 25420.3 26744.2 29213.9 8022.51 30317.7 12677.9 2430.61 14591.5 32974.8 17353.2

it tells me that the first number is the ID and the next 12 numbers, the ones underlined, are the monthly averages for this year with the last number being last years average.


The “Wild and Wooley” company has a large salesman staff and the company needs to gather info about the salesman’s yearly sales results.

You have been tasked with this assignment.

There are no more than 15 salesmen on staff and for each salesman the company has recorded for each month of the year, each salesman’s total monthly sales.

Also recorded for each salesman is his ID and his last year’s average monthly sales. (The last number in the row)

You are to compute for each salesman, this year’s average monthly sales. (uses the first 12 numbers).

Since everyone generally has a bad month, and the company is nice, you will also calculate a second monthly average sales dropping his one worst monthly sales. (find the lowest monthly sales, add all 12 months sales and subtract the lowest then divide by 11)




The information to be reported are the following:

1) Print out each salesman’s ID, his last year’s average monthly sales, the current year’s monthly sales, the current year’s average monthly sales minus his worst month all in a table format (columns, headers, ect.).

2) Also to show in the table are the salesmen who had a 10% increase for this year’s sales over last years. (use 12 month averages).

These salesmen will be slated for a year in bonus.

3) Also, show my worst salesman whose two-year average is the lowest of all the salesmen, again to be shown in the table.


Here I would start with #1 first before getting into any calculations that may be needed. For now you can hard code numbers into the output and change them to function calls later. Most times I find setting up the output makes the calculations easier to figure out later.

Andy
Pages: 123