Coverting a file into a 2D array then displaying it, all 0s?

Pages: 12
Mar 4, 2011 at 9:44pm
Here is my 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
61
62
63
64
65
66
67
68
69
//Libarys used.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <fstream>

//Using the standard namespace for files.
using namespace std;

//starting code for main program.
int main()
{
	// Setting the variables.
	int hold;
	int inputnumber = 0;
	double decimal[101] = {0};
	double grid[101][13] = {0};

	// Outputs a message on the screen.	
	cout << "Here are the special numbers in the range of 1-100\n";

	// Loop to create the array of the numbers in decimal form.
	for(int i = 1; i < 101; i++)
	{
		decimal[i] = 1./i;
	}

	// Inputs array into a text file
	ofstream output("output.txt");
    for(int i = 1;i < 101;i++)
    {

		output << decimal[i]<<endl;

	}

	// Converts the text file into a 2D array
    ifstream infile("output.txt");
	for (int i = 1; i < 101; i++)
	{
		for (int j = 1; j < 14; j++)
		{
			infile >> inputnumber;
			grid[i][j] = inputnumber;
		}
	}

	// Displays the array
	for (int i = 1; i < 101; i++)
	{

		for (int j = 1; j < 14; j++)
		{
			cout << grid[i][j] << endl;
		}
		cout << "\n";
	}




// Holds the screen up.
cin >> hold;

// Returns a value to end the code.
return(0);
}


I know its outputting to the file ok, and the file contains the correct numbers that i need to display on the screen, but it doesnt display right, anyone know why?

Thanks in advance
Mar 4, 2011 at 10:26pm
You probably need to close the file before you open it for reading.
Mar 4, 2011 at 10:35pm
You are writing decimal numbers that are all (barring the first) less than one to the file, and then reading them back into an int that will round them all down to zero.
Last edited on Mar 4, 2011 at 10:35pm
Mar 4, 2011 at 10:35pm
Just tried that, no luck.
Mar 4, 2011 at 10:38pm
I changed the input into a double which is displaying "0.01" 100 times with a few empty gaps inbetween, so getting there
Mar 4, 2011 at 10:44pm
Is that not what it's supposed to do?
Mar 4, 2011 at 10:46pm
No, its ment to input 100 numbers into output.txt (1/1, 1/2, 1/3, 1/4 ect.) which is does (i have opend the text file it creates and all the numbers are corect) then its ment to load all 100 numbers into a 2D array then display them normally (like they are in the file)
Mar 4, 2011 at 10:48pm
You are writing out 100 numbers and then reading back 1300 numbers.
Mar 4, 2011 at 10:52pm
My understand of 2d arrays is terrible, thats what im trying to work on lol what i was trying to do with print out 100 numbers but add everything into a array which is 13 numbers long for instance 1.1111 would be first row and 6 colums along, can you understand what im tyring to do?
Mar 4, 2011 at 10:55pm
Not really; if you're calculating 100 numbers, where are you going to get the other 1200 numbers you need to fill the 2D array?

As an aside, it is customary to start using your arrays at the element numbered zero, rather than one.
Last edited on Mar 4, 2011 at 10:57pm
Mar 4, 2011 at 10:59pm
Ok ill try to explain what im trying to do, im trying to build a 2d array like this and just display it

(100 down, 13 across)

[1][0][0][0][0][0][0][0][0][0][0][0][0]

[0][.][5][0][0][0][0][0][0][0][0][0][0]

[0][.][3][3][3][3][3][3][3][3][3][3][3]

[0][.][2][5][0][0][0][0][0][0][0][0][0]

[0][.][2][0][0][0][0][0][0][0][0][0][0]

they are the first 5 numbers in the file it creates.
Mar 5, 2011 at 5:02pm
Move line 44 before line 42.

Moschops is correct - you are inputing more numbers than are in your file.
The cin should be in the loop iterating the 100 numbers.
Mar 5, 2011 at 5:44pm
Ok, i moved the line 44 to 40 just before the loop but i dont understand what you mean about the cin? im not using one?
Mar 6, 2011 at 3:48am
oops meant the input stream, meant to say infile :)

Mar 7, 2011 at 12:07am
Do you mean line line 44 should be in the line 24 loop?
Mar 9, 2011 at 10:52pm
Bump from 4th page
Mar 10, 2011 at 6:51am
Line 44 should got right before the for loop on line 42.

38
39
40
41
42
43
44
45
46
47
	// Converts the text file into a 2D array
    ifstream infile("output.txt");
	for (int i = 1; i < 101; i++)
	{
	        infile >> inputnumber;
		for (int j = 1; j < 14; j++)
		{
			grid[i][j] = inputnumber;
		}
	}
Mar 10, 2011 at 2:06pm
I did that, doesnt change anything, still getting all 0s
Mar 10, 2011 at 2:32pm
Your basic algorithm is wrong for this task.

infile does not read the number in a single digit at a time; you get the whole number at once into inputnumber.

You must read in the number, and then break it into individual digits, as you have indicated you want to put one digit into each element of the array.

1
2
3
4
5
6
7
8
9
10
11
// Converts the text file into a 2D array
    ifstream infile("output.txt");
	for (int i = 1; i < 101; i++)
	{
	        infile >> inputnumber;
		// Here add code to examine the number.
                // If the number if less than one, put a zero into [i][0]
               // If the number is equal to 1, put a one into [i][0]
               //  Now put a decimal point into [i][1]
               // Now figure out the first number to put in after the decimal point etc etc.
	}


It seems a very strange thing to do. Why do you choose to manually create every digit in the output yourself? If you want every number to be output to 11 decimal places, you can control that easily and you don't need to manually build every decimal place yourself.

Alternatively, don't read in a whole number; read in single char , and you can then place those chars into your grid one at a time. Every so foten, you will need to spot when the number has ended, and fill the remaining elements of that grid line with zero.

Here is code that produces the output I think you want, but it doesn't manually build every deciaml place into a big grid; it just uses the provided library functions. Is this what you're looking for?

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

#include <iostream>
#include <iomanip>
#include <conio.h>
#include <fstream>

//Using the standard namespace for files.
using namespace std;

//starting code for main program.
int main()
{
	// Setting the variables.
	int hold;
	double inputnumber = 0;
	double decimal[101] = {0};
	double grid[101] = {0};

	// Outputs a message on the screen.	
	cout << "Here are the special numbers in the range of 1-100n";

	// Loop to create the array of the numbers in decimal form.
	for(int i = 1; i < 101; i++)
	{
		decimal[i] = 1./i;
	}

	// Inputs array into a text file
	ofstream output("output.txt");
    for(int i = 1;i < 101;i++)
    {
                output << fixed << setprecision(11); // Fix decimal places
		output << decimal[i]<<endl;

	}

	// Converts the text file into a 2D array
    ifstream infile("output.txt");
	for (int i = 1; i < 101; i++)
	{
		
			infile >> inputnumber;
			grid[i] = inputnumber;
		
	}

	// Displays the array
	for (int i = 1; i < 101; i++)
	{

          cout << fixed << setprecision(11); // Fix decimal places
          
          
		cout << grid[i] << endl;
		
		
	}




// Holds the screen up.
cin >> hold;

// Returns a value to end the code.
return(0);
}
Last edited on Mar 10, 2011 at 2:47pm
Mar 10, 2011 at 3:16pm
That creates a 1D array, i can do that myself, im trying to convert it into a 2D array that im having problems with, but im giving up now anyway so thanks for all your help.
Pages: 12