Thread: Arrays

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

    Arrays

    I have some input from a file that i put into an array and i need to do two things with this array. First i need to know how to modify it so that when i output it only 9 values will appear per line, and secondly i need to know how to determine which value occurs most frequently and then output that #.
    Please help!
    Here's what i have so far:
    #include <iostream>
    #include <fstream>
    using namespace std;
    #define SIZE 200
    ifstream myin;
    ofstream myout;
    int main()
    {
    int x[SIZE],i=0,h=0;
    myin.open("numbers.txt",ios::in);
    while(myin>>x[i])
    {
    if(x[i]%5==0)
    {
    cout<<x[i]<<" ";
    h++;
    }
    i++;
    }
    cout<<endl;
    cout<<i<<endl;
    cout<<h<<endl;
    myin.close();
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    To output 9 values per line, try something like:
    while(myin>>x[i])
    {
    cout<<x[i]<<" ";
    i++;
    if ((i%9) == 0)
    cout << endl;
    }

    To determine which value occurs most frequently, you need another array to store a running count of how many times that number has occurred. Also your array size should be the size of the largest value in your file. For example, if the largest number in your file is 2000, then:

    int bucket[2000];

    You probably don't need the other array(x[]).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM