Thread: c++ array program

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    1

    c++ array program

    This is my problem

    //now use outfile of cout to write to the file;
    // use "\t" to seprarte fields
    for (int k = 0; k<= m-1 ; k++
    outfile << pa [k] << "\t" << qa [k] << endl;
    }

    I need help in writing a solution to the above problem with the info provided. Whatever help or suggestions would help me to understand and then I can complete the rest of the program. This is an array type problem and there is much more than what I submitted. Thank you.

    Kim

    Here is the entire problem which is an inventory: sorting and updating using arrays also the Indat File or input file. I need to write a code to run this program, everything else is done with no errors or warnings. I hope this is enough info to help solve this problem, if this is not enough, let me know. Thanks

    #include <iostream.h>
    #include <math.h>
    #include <fstream.h>

    const int arraySize = 100;
    int pa[arraySize], qa[arraySize];

    void loadarray(int &m);
    void sortarray(int m);
    void updatearray(int m);
    void printarray(int m);
    int reorder(int m);
    void swap(int &r, int &s);

    main()
    {
    int n,m;
    n = 0;
    loadarray(n);
    cout << n+1 << " is the actual size of the array" << endl;
    sortarray(n);
    updatearray(n);
    printarray(n);
    m = reorder(n);
    cout << "number low " << m << endl;
    return 0;
    }

    void loadarray(int & m)
    {
    ifstream infile = "indat.txt";
    infile >> pa[m] >> qa[m];
    while (qa[m] >= 0)
    {
    m++;
    infile >> pa[m] >> qa[m];
    }
    m--;
    }

    void sortarray(int m)
    {
    for (int k = m-1;k >= 0;k--)
    for (int j = 0;j <= k;j++)
    if (pa[j] > pa[j+1])
    {
    swap(pa[j],pa[j+1]);
    swap(qa[j],qa[j+1]);
    }
    }

    void updatearray(int m)
    {
    int part, quant;
    cin >> part >> quant;
    int s = 0;
    while (part > pa[s] && s < m)
    s++;
    if (part == pa[s])
    qa[s] += quant;
    else
    cout << "bad part number " << part << endl;
    cin.get();
    cin.get();

    /*int c,p,q,k;
    cout << "enter code, part number, quantity" << endl;
    cin >> c >> p >> q;
    while ((c==0) || (c==1))
    {
    k=0;
    while (p > pa[k] && k < m)
    ++k;
    //now update inventory if part number found;
    //else bad part #
    //now ready for more input
    }*/
    }

    void printarray(int m)
    {
    ofstream outfile = "dataout.txt";
    // now use outfile instead of cout to write to the file;
    // use "\t" to separate fields
    for (int r = 0;r <= m;r++)
    cout << pa[r]<< "\t" << qa[r] << endl;
    }
    int reorder(int m)
    {
    int r;
    r=0;
    cout << "reorder list" << endl;
    // code the for loop to find items for which quantity is less than five
    // increment the return value r every time an item is put on the ;ist
    r += 10;
    return r;
    }

    void swap(int &r, int &s)
    {
    int temp = r;
    r = s;
    s = temp;
    }


    Indat File


    7777 45
    6666 52
    8888 12 9999 81 2222 7 4444 41 5555 33 1111 22 3333 97 2121 45 3131 125 1121 15 4141 61 8141 97 8181 14 9119 12 9191 117 9889 42 6889 73 1234 15 2345 110 3456 83 4567 15 5678 7 6789 55 7890 88 8901 32 9000 -10
    Last edited by kmasterk; 11-05-2002 at 11:22 AM.

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Talking

    Show us what you have done and we will help.
    Mr. C: Author and Instructor

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    29
    Your piece of code is too small.. we don't know what pa[] and qa[] are, maybe they are too small so the index k is out of range (i don't know what relation is between m and size of these arrays).
    Also you have some missing ) and {. It makes no good to post such pieces of code.. maybe you should read the FAQ about it..

    And what's 'outfile of cout' ? If outfile is a file, then it has nothing to do with cout. And if it's the standard output, it's not a good idea to name it as 'file'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Array Program
    By alex1067 in forum C Programming
    Replies: 5
    Last Post: 04-15-2008, 06:26 AM
  3. help with small program. array issue
    By InvariantLoop in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 12:26 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM