printing arrays to file. in order
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: printing arrays to file. in order

  1. #1
    Join Date
    Jan 2014
    Posts
    12

    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

  2. #2
    Join Date
    Jan 2014
    Posts
    12

    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]

  3. #3
    Join Date
    Apr 1999
    Posts
    27,431

    Re: printing arrays to file. in order

    Quote Originally Posted by howardstark View Post
    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

  4. #4
    Join Date
    Apr 1999
    Posts
    27,431

    Re: printing arrays to file. in order

    Quote Originally Posted by howardstark View Post
    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

  5. #5
    Join Date
    Jan 2014
    Posts
    12

    Re: printing arrays to file. in order

    Quote Originally Posted by Paul McKenzie View Post
    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

  6. #6
    Join Date
    Apr 1999
    Posts
    27,431

    Re: printing arrays to file. in order

    Quote Originally Posted by howardstark View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center