-
January 14th, 2014, 09:20 PM
#1
printing arrays to file. in order
hey guys i have 3 arrays (even, odd and negative), they are all different sizes, but they maximum elements they can have is 50. how do i write them to a file in this format
EVEN ODD NEGATIVE
2 3 -2
4 5 -4
6 7 -9
Code:
size = 50; //they dont all 50, its just that array cant be bigger than 50
for (int i =0; i < size; i++)
{
cout <<setw(5);
if (even[i] != 0)
{
myfile << eList[i];
myfile << endl;
}
else
myfile << " ";
}
for (int i =0; i < size; i++)
{
cout <<setw(5);
if (odds[i] != 0)
{
myfile << oList[i];
myfile << endl;
}
else
myfile << " ";
}
for (int i =0; i < size; i++)
{
myfile <<setw(5);
if (negatives[i] != 0)
{
myfile << nList[i];
myfile << endl;
}
else
myfile << " ";
}
i have no idea.. please help... i know the code above is wrong, but thats the best i got
-
January 14th, 2014, 09:23 PM
#2
Re: printing arrays to file. in order
the above format didnt print how i wanted too
[code]
//EVEN ODD NEGATIVE
// 2 3 -2
// 4 5 -4
// 6 7 -9
[code]
-
January 14th, 2014, 09:36 PM
#3
Re: printing arrays to file. in order
Originally Posted by howardstark
how do i write them to a file in this format
No different than if you wrote them to the console. The streaming operator << works basically the same for cout, files, etc.
Regards,
Paul McKenzie
-
January 14th, 2014, 09:44 PM
#4
Re: printing arrays to file. in order
Originally Posted by howardstark
i have no idea.. please help... i know the code above is wrong, but thats the best i got
Why not write a test program that outputs to the screen in that format?
Code:
#include <iostream>
using namespace std;
int main()
{
int values[] = {10,3, 4, 6, -3, 5};
int values2[] = { 34, 35, 768, 23, 0, 1};
int values3[] = { 9, -6, 45, 8, 6, 1};
// how would you output the values to the screen in this format?
// values[0] values2[0] values3[0]
// values[1] values2[1] values3[1]
// values[2] values2[2] values3[2]
//...
// values[5] values2[5] values3[5]
//...
}
There are 3 arrays, 6 values, and you have a console. Do you see the pattern of the output above? How would you write the loop to mimic the pattern that you see?
Basically, every program can be broken down in simple form. You're thinking of the whole program as one giant blob of code -- instead, you should break it down as above. Once you see how it's done with a simple program, then you apply it to a larger program.
Regards,
Paul McKenzie
-
January 14th, 2014, 09:54 PM
#5
Re: printing arrays to file. in order
Originally Posted by Paul McKenzie
Why not write a test program that outputs to the screen in that format?
Code:
#include <iostream>
using namespace std;
int main()
{
int values[] = {10,3, 4, 6, -3, 5};
int values2[] = { 34, 35, 768, 23, 0, 1};
int values3[] = { 9, -6, 45, 8, 6, 1};
// how would you output the values to the screen in this format?
// values[0] values2[0] values3[0]
// values[1] values2[1] values3[1]
// values[2] values2[2] values3[2]
//...
// values[5] values2[5] values3[5]
//...
}
There are 3 arrays, 6 values, and you have a console. Do you see the pattern of the output above? How would you write the loop to mimic the pattern that you see?
Basically, every program can be broken down in simple form. You're thinking of the whole program as one giant blob of code -- instead, you should break it down as above. Once you see how it's done with a simple program, then you apply it to a larger program.
Regards,
Paul McKenzie
i would but there are a total of 40 elements in all the arrays together.. so i was wondering is there a quicker way to do with a loop
-
January 14th, 2014, 11:05 PM
#6
Re: printing arrays to file. in order
Originally Posted by howardstark
i would but there are a total of 40 elements in all the arrays together.. so i was wondering is there a quicker way to do with a loop
Look at the pattern above. What do you see? There could be a million elements, how does that change the general pattern?
Again, look at each line. You are printing element 0 of each array, then the next line, you print element 1 of each array, then the next line element 2 of each array, etc. You should be able to formulate a loop from that information (Hint -- 0, 1, 2, 3, 4, 5, 6. etc.).
Regards,
Paul McKenzie
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|