storing a manipulated array in a new array

This is a discussion on storing a manipulated array in a new array within the C++ Programming forums, part of the General Programming Boards category; Hello all, what I'm trying to do is after counting all the letters in my file I'm getting all the ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    storing a manipulated array in a new array

    Hello all, what I'm trying to do is after counting all the letters in my file I'm getting all the percentages of each letter. Next I have to have a running total for the percentages. For example if
    Code:
    A: 0.0860 and B: 0.0151 than the running total
    for B would be (.0860+.0151) or B: 0.1011
    and so on throughout the whole alphabet. To do this I was trying to have a seperate array in the last for loop that would store all the single percantages...but the following is not working:
    Code:
    percentageArray[i] = lettersArray[i]/(double)sum;
    so two questions: is there a way to store the percentages only into a seperate array?

    here is the part of the code that deals with this:

    Code:
    	for ( i=0; i<LIMIT; i++ )
    	{
    		lettersArray[i] = 0;
    	}
    
    	cout << "The input file contains: \n";
    	
    	while( inStream >> c)
    	{
    		lettersArray[toupper(c)]++; //count each instance of every character
    		//cout << c << " ";
    	}
    
    	cout << endl;
    
    	cout << "The number of each letter in the input file (darwin.txt) are: \n";
    
    	for( i='A'; i<='Z'; i++ ) //to get only alphabetical characters
    	{
    			cout << (char)i << ":" << setw(4) << setfill(' ') << lettersArray[i] << "; " ;
    			sum += lettersArray[i]; //get total of all characters
    	}
    
    	cout << "\n\nThe percentages for the input file are: \n";
    
    		//set the percentages for input
    	for( i='A'; i<='Z'; i++)
    	{
    		cout << (char)i << ":" << lettersArray[i]/(double)sum<< "; " ;
    		
    	}
    thanks,

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Offhand I don't see anything wrong, does the letter counting work just fine?

    When you say:
    Code:
    percentageArray[i] = lettersArray[i]/(double)sum;
    doesn't work, what is it doing? Is percentageArray a double?

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    yes percentArray[] is double, the code doesn't give me an error but later when I want to display percentArray[]

    Code:
    cout << percentArray[LIMIT];
    what I get is 0.0000

    axon

    edit------

    the letter counting works just fine...the individual percentages work fine as well. The only thing I can't figure out is how to have a running total for the percentages.
    Last edited by axon; 03-04-2003 at 11:59 AM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Well, you haven't stored anything in percentArray[LIMIT], you've only stored values in percentArray['A'] through percentArray['Z'].
    To get the total you need to do something like so:
    Code:
    totalPercentArray[i] = lettersArray[i]/(double)sum+totalPercentArray[i-1];
    Then printing out totalPercentArray['Z'] should give you 1.000 I hope

  5. #5
    Bubbaga
    Guest
    Why do you want a running total of the percentages?

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    PJYelton, in your code how would you account for the percentage of 'A'? and yes the total for 'Z' should be 1.0000.

    and in your code, what size shout TotalPercentArray[] be? 25, for every letter?

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  7. #7
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    OK Salem, here is the whole function with which you've helped me before:

    Code:
    void countAllTheLetters()
    {
    	cout.setf(ios::fixed);
    	cout.setf(ios::showpoint);
    	cout.precision(4);
    
    	const int LIMIT = 128;
    	int sum = 0;
    
    	ifstream inStream;
    	
    	int lettersArray[LIMIT];
    	double totalPercentArray[25];
    	
    	char c;
    	int i;
    
    	inStream.open("darwin.txt");
    
    	if( inStream.fail() )
    	{
    		cout << "The input file, failed to open. Exiting....\n\n";
    		exit(-1);
    	}
    
    	for ( i=0; i<LIMIT; i++ )
    	{
    		lettersArray[i] = 0;
    	}
    
    	cout << "The input file contains: \n";
    	
    	while( inStream >> c)
    	{
    		lettersArray[toupper(c)]++; //count each instance of every character
    		//cout << c << " ";
    	}
    
    	cout << endl;
    
    	cout << "The number of each letter in the input file (darwin.txt) are: \n";
    
    	for( i='A'; i<='Z'; i++ ) //to get only alphabetical characters
    	{
    			cout << (char)i << ":" << setw(4) << setfill(' ') << lettersArray[i] << "; " ;
    			sum += lettersArray[i]; //get total of all characters
    	}
    
    	cout << "\n\nThe percentages for the input file are: \n";
    
    		//set the percentages for input
    	for( i='A'; i<='Z'; i++)
    	{
    		cout << (char)i << ":" << lettersArray[i]/(double)sum << "; " ;
    		totalPercentArray[i] = (lettersArray[i]/(double)sum+totalPercentArray[i-1]);
    
    
    		
    	}
    	cout << endl;
    
    	cout << "Total number of characters: " << sum ;
    
    }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  8. #8
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Yeah, if you did it my way, then you would need it to be of size 26 (is this what you meant instead of 25?). But make sure that if you declare it as size 26, that you don't write percent['A'] because that will be out of bounds, you'd have to do percent[i - 'A'].

    To account for the first letter, do one of two things. Either make a check with an if statement that adds zero instead of the value at i-1 if i=='A', or make the array of size 27 and have 'A' start at spot 1 instead of 0.
    Last edited by PJYelton; 03-04-2003 at 12:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing an array of structures?
    By Sparrowhawk in forum C Programming
    Replies: 4
    Last Post: 12-15-2008, 02:07 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Storing multiple string in array
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 03-15-2004, 06:49 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21