8 Replies - 5014 Views - Last Post: 21 November 2008 - 08:37 PM Rate Topic: -----

#1 k2283944   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 04-November 08

Counting numbers in an Array

Posted 19 November 2008 - 07:31 PM

Well I'm having a problem counting the number of intergers that are read into my array from a file.

I have tried both
while(!infile.eof())
   count++;



and and if statement in the same format and have not been able to get it to work properly. I have completed the rest of my program and it works right if i set the arraySize = to the right number of intergers read into my array, so this is the only thing left to come up with.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Counting numbers in an Array

#2 Gilthanis   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 20-September 08

Re: Counting numbers in an Array

Posted 19 November 2008 - 08:43 PM

Well, you could do it the easy way - just initialize an integer to 0. Name it count or something... And every time that you read in a number and stick it in the array, just do count++;
That should do the trick. =)

- Gilly
Was This Post Helpful? 0
  • +
  • -

#3 k2283944   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 04-November 08

Re: Counting numbers in an Array

Posted 19 November 2008 - 10:03 PM

Heres my code that im using to read the intergers into my array
for ( i = 0; i < arraySize; i++ ){	
	
	infile >> myArray [ i ];
	}
 


do i need to put the count++ between the braces after the infile?
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6643
  • View blog
  • Posts: 30,932
  • Joined: 10-May 07

Re: Counting numbers in an Array

Posted 19 November 2008 - 10:17 PM

Since i is already incrementing why don't you just use that variable? By the time you exit your for loop count & i would have the same value. As well, the size of the array will always be what you initialized it as.

for example :

int example_array[10]; // <-- max size of 0 through 10



So no matter how I loop through it, or what I add to or remove from it, it's max size is ten.
Was This Post Helpful? 0
  • +
  • -

#5 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: Counting numbers in an Array

Posted 19 November 2008 - 10:18 PM

View Postk2283944, on 20 Nov, 2008 - 09:33 AM, said:

Heres my code that im using to read the intergers into my array
for ( i = 0; i < arraySize; i++ ){	
	
	infile >> myArray [ i ];
	}
 


do i need to put the count++ between the braces after the infile?


No, here i would be your count.

for ( i = 0; (i < arraySize) && (!infile.eof()); i++ )
{	
	infile >> myArray [ i ];
}



Was This Post Helpful? 0
  • +
  • -

#6 badjava   User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: Counting numbers in an Array

Posted 20 November 2008 - 12:01 AM

View Postno2pencil, on 19 Nov, 2008 - 09:17 PM, said:

Since i is already incrementing why don't you just use that variable? By the time you exit your for loop count & i would have the same value. As well, the size of the array will always be what you initialized it as.

for example :

int example_array[10]; // <-- max size of 0 through 10



errr 0 through 9!? ;)


@k2283944: So with Amit's example above this, the number of elements loaded in your array will be i.

And like Pencil said too, if it hits eof and exits out of the load array loop you are always going to have ten so that would be another simple if then check u could do.

This post has been edited by badjava: 20 November 2008 - 12:06 AM

Was This Post Helpful? 0
  • +
  • -

#7 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: Counting numbers in an Array

Posted 20 November 2008 - 02:19 AM

Quote

for ( i = 0; (i < arraySize) && (!infile.eof()); i++ )
{	
	infile >> myArray [ i ];
}


A variation of this, you may find useful is below.

If you expect the array size is large enough to hold the file+some
then it is good design to put the first 'test' you expect to fail ... first.

for( int i = 0; infile >> myArray [ i ] && i < arraySize; ++i ) ;

As previously mentioned ... the count is returned in i = numElements

Recall that the array will run from ...

myArray[ 0 ] ... to myArray[ numElements -1 ]

since i was incremented after the last 'test' above passed.
Was This Post Helpful? 0
  • +
  • -

#8 k2283944   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 04-November 08

Re: Counting numbers in an Array

Posted 20 November 2008 - 08:50 AM

Well what is my arraySize going to be? Because isnt the arraySize going to be whatever i will be(number of integers read into array) ??

here is all of my code
I've put ** for arraySize, because thats what im needing to calculate. There are 83 integers in my file, but my array is a size of 100. So i need a variable(arraySize) to keep up with how many are actually read into the array. If i set arraySize = 83 everything works fine.

#include <iostream>
#include <iomanip>
#include <fstream>


using namespace std;

int arraySize = **;
int myArray[ 100 ];
int maxNumber = 0;
int minNumber = 0;
int i;


int main()
{
  
ifstream infile;
infile.open ("inputNums.txt");

ofstream outfile;
outfile.open ("finalResults.txt");

if ( ! infile )
{
outfile << "Failure to open input file!" << endl;
system("pause");
return 0;
}

//Initialize Numbers in Array
for ( i = 0; i < arraySize; i++ ){
	myArray[ i ] = 0;
	}

//Read in numbers from file to Array
for ( i = 0; i < arraySize; i++ ){	
	infile >> myArray [ i ];
	}
	
// Find Max Number in Array
int max = 0;
int maxp = 0;

for ( i = 0; i < arraySize; i++ ){
	
	if ( myArray [ max ] < myArray [ i ] ){
	   max = i;
	   maxp = max;}
}
	   
maxNumber = myArray [ max ];


//Find Min Number in Array
int min = 0;
int minp;
for ( i = 0; i < arraySize; i++ ){
	if ( myArray [ min ] > myArray [ i ] ){
	   min = i;
	   minp = min;}
	   
}
	   
minNumber = myArray [min ];


cout << "The maximum value is " << maxNumber << " found at array index position " << maxp << "." << endl;
cout << "The minimum value is " << minNumber << " found at array index position " << minp << "." << endl;
cout << "There are " << arraySize << " values stored in the array." << endl;



infile.close();
outfile.close();

	  system("pause");
	  return 0;
}


This post has been edited by k2283944: 20 November 2008 - 09:07 AM

Was This Post Helpful? 0
  • +
  • -

#9 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: Counting numbers in an Array

Posted 21 November 2008 - 08:37 PM

View Postk2283944, on 20 Nov, 2008 - 07:50 AM, said:

Well what is my arraySize going to be? ...


This little demo may help ... :)

/*
    Array size demo ... wrt reading data from files ...
    
    Beginning students of CS may like to check here:
    [url="http://developers-heaven.net/forum/index.php/topic,46.0.html"]http://developers-heaven.net/forum/index.php/topic,46.0.html[/url]
*/

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
using namespace std;

// Globals ...

const int MAX = 5;
const char fName[]  = "myInts.txt";  // holds: 2 0 1
const char fName2[] = "myInts2.txt"; // holds: 2 0 1 4 -1 3  

// Recall that 'ary' is a local variable and holds
// the address of the first 'int' in the array
void initArray( int ary[], int numElements, int value )
{
    for( int i = 0; i < numElements ; ++i ) ary[i] = value; 
}

// If file not found or file not opened ok, exit program with error message 
// else read pass back the array (by ref) & return the number of int's read. 
int readIntoArray( const char fileName[], int ary[], int maxPossible )
{
    ifstream infile( fileName );
    if( !infile )
    {
        cout << "\nError!  Failed to open input file " 
             << fileName << "." << endl;
        system( "pause" );
        exit(1);
    } 
    
    // if reach here ..
    // then read the int's in the file into the Array 
    // and return the count in i
    int i =0;
    while( infile >> ary[ i ]  &&  i < maxPossible ) ++i;
    infile.close();
    return i;
}

void showArray( int ary[], int num )
{
    cout << "The array is: ";
    for( int i = 0; i < num ; ++i ) cout << left << setw(12) << ary[i];
     
    cout << endl;
}

void showArrayMaxMin( int ary[], int num )
{
    if( num <= 0 )
    {
        cout << "\nNo values in array.\n";
        return; // exit function right now ...
    }
        
    // use some actual values in the Array ... to start the testing
    int low = ary[0], lowp = 0,  hi= ary[0],  hip = 0;
    
    // now start at index = 1 ...
    for( int i = 1; i < num; i++ )
    {
        if( ary[ i ] < low ) { low = ary[ i ]; lowp = i; }
        if( ary[ i ] > hi  ) { hi  = ary[ i ];  hip = i; }
    } 
    cout << "\nThe maximum value of " << hi 
         << " is found at array index = " << hip << "."
         << "\nThe minimum value of " << low 
         << " is found at array index = " << lowp << "."
         << "\nThere are " << num << " \"used elements\""
         << " ... out of " << MAX << " \"possible elements\".\n";
}

int countIntsIn( const char fileName[] )
{
    ifstream infile( fileName );
    if( !infile )
    {
        cout << "\nError!  Failed to open input file " 
             << fileName << "." << endl;
        system( "pause" );
        exit(1);
    } 
    
    // if reach here ..
    // then count ... and return the count
    int count = 0, dummy;
    while( infile >> dummy ) ++count;
    infile.close();
    return count;
}


int main()
{
    
    // declare and reserve space for an array of MAX number of int's
    int myInts[ MAX ]; 
    
    cout << "Before initialization ...\n";
    showArray( myInts, MAX  );
            
    //Initialize all possible elements in the Array to 0
    initArray( myInts, MAX, 0 );
    
    cout << "\nAfter initialization ...\n";
    showArray( myInts, MAX  );
    
    
    // read file fName into array myInts up to a maximum of MAX int's
    int numInts = readIntoArray( fName, myInts, MAX ); 
    
    cout << "\n\nAfter 'readIntoArray( fName, myInts, MAX )' executed ...\n";
    showArray( myInts, numInts  );
    
    showArrayMaxMin( myInts, numInts );
    cout << "The number of int's in " << fName << " is "
         <<  countIntsIn( fName ) << endl << endl;
    
    
    // read file fName2 into array myInts up to a maximum of MAX int's
    numInts = readIntoArray( fName2, myInts, MAX ); 
    
    cout << "\nAfter 'readIntoArray( fName2, myInts, MAX )' executed ...\n";
    showArray( myInts, numInts  );
    
    showArrayMaxMin( myInts, numInts );
    cout << "The number of int's in " << fName2 << " is "
         <<  countIntsIn( fName2 ) << endl << endl;
    
    system("pause");
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1