Current location: Hot Scripts Forums » Programming Languages » C/C++ » reading input into an array


reading input into an array

Reply
  #1 (permalink)  
Old 04-16-04, 02:07 AM
seijuro seijuro is offline
New Member
 
Join Date: Apr 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
reading input into an array

I was wondering how I would go about reading in an input file of numbers and storing it in an array so that I could find out how many times a number occured. So far all I have is a way to read in the numbers and sum them up, but I don't know how to put it in an array. So far I have this..any help would be appreciated

Code:
int num = 0, sum = o, count = o;
ifstream fin("numbers.txt");
if(fin.fail())
{
  cout << "File does not exist" << endl;
  exit(EXIT_FAILURE);
}
fin >> num;
while(!fin.eor())
{
  sum += num;
  count++;
  fin >> num;
}
fin.close();

ofstream fout("results.txt");
fout << count << endl;

if(fout.fail())
{
  cout << "fout fail" << endl;
  exit(EXIT_FAILURE);
}
fout.close();
Reply With Quote
  #2 (permalink)  
Old 04-17-04, 12:39 AM
hyjacked hyjacked is offline
Wannabe Coder
 
Join Date: Nov 2003
Location: New Brunswick, Canada
Posts: 174
Thanks: 0
Thanked 0 Times in 0 Posts
arrays are similar to variables, except that you must refer to a specific place in the arrray when you want to save a value.

for example
int values[10];
would give you an array, called values, that can hold 10 elements, numbered 0 through 9.
in order to access the first element, you would refer to it by values[0], in order to refer to the fifth element, you would refer to it by values[4].

you can also cycle through an array by using a loop, and use the loop variable, call it i, as the index of the array, ie: values[i]

hope this has been helpful.
best of luck.
__________________
hyjacked
Reply With Quote
  #3 (permalink)  
Old 04-17-04, 06:18 AM
seijuro seijuro is offline
New Member
 
Join Date: Apr 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
I understand how to use arrays and how I can use the [] to access individual elements in an array. What I would like to know is there a way to count the occurences of each number in an array and then display it without writing too much code?
Reply With Quote
  #4 (permalink)  
Old 04-19-04, 07:18 PM
EvilHaider EvilHaider is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
To figure out how many times each number occurs you basically have to run through the entire array and 'note' the occurrances. For example if your array consists of numbers between 0 and 9. You could make 10 variables of type int initialized to 0: a,b,c...i,j. where a corresponds to 0, b to 1, and so on. As you are running through the array, you check the number, if it's 1, you increment b; if its 3, you increment d, and so on. When you're done with the array you will have the 10 variables that have a count of how many times the corresponding number appears in your array. This is somewhat messy though. An alternative approach would be to utilize a two dimensional array in place of the 10 variables, which would clean it up a bit - especially if you dont know ahead of time what numbers to expect in your array and have to deal with whatever number is presented (for example if the number 13 is found, how do u deal with that?). Does this help?
__________________
**
Tutorial Maniacs
**
Reply With Quote
  #5 (permalink)  
Old 04-20-04, 12:22 AM
hyjacked hyjacked is offline
Wannabe Coder
 
Join Date: Nov 2003
Location: New Brunswick, Canada
Posts: 174
Thanks: 0
Thanked 0 Times in 0 Posts
I'd say go with the 2d array approach, it's a lot neater than having variables.

for every element, check if it's in the list, if it is, increment the second cell for that number, if it is not, add it to the end of the array, and increment it's number.

In the end this should give you a list of distince elements, along with how many times they occured.
__________________
hyjacked
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional Array Sorting Kage PHP 6 12-12-05 04:19 PM
How do I write an array to a file? bark PHP 2 03-30-04 12:28 PM
help on array reefjames C/C++ 3 01-25-04 11:06 PM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM
asp: values in array not in order?? seala ASP 0 08-16-03 12:06 PM


All times are GMT -5. The time now is 01:22 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.