Need some Hints on how to figure this out.

This is a discussion on Need some Hints on how to figure this out. within the C++ Programming forums, part of the General Programming Boards category; Code: void eMath::GetIndex() { for(int i=0;i<MAX_SIZE;i++) { if(eNumber[i]== 'I' ) Counter[0]++; if(eNumber2[i]== 'I' ) Counter[0]++; if(eNumber[i]== 'N' ) Counter[1]++; if(eNumber2[i]== ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Need some Hints on how to figure this out.

    Code:
    void eMath::GetIndex()
    {
    
    		for(int i=0;i<MAX_SIZE;i++)
    	{
    		if(eNumber[i]== 'I' )
    			Counter[0]++;
    		if(eNumber2[i]== 'I' )
    			Counter[0]++;
    		if(eNumber[i]== 'N' )
    			Counter[1]++;
    		if(eNumber2[i]== 'N' )
    			Counter[1]++;
    		if(eNumber[i]=='9')
    			Counter[2]++;
    		if(eNumber2[i]=='9')
    			Counter[2]++;
    		if(eNumber[i]=='L')
    			Counter[3]++;
    		if(eNumber2[i]=='L')
    			Counter[3]++;
    		if(eNumber[i]=='/')
    			Counter[4]++;
    		if(eNumber2[i]=='/')
    			Counter[4]++;
    		if(eNumber[i]=='O')
    			Counter[5]++;
    		if(eNumber2[i]=='O')
    			Counter[5]++;
    		if(eNumber[i]=='Y')
    			Counter[6]++;
    		if(eNumber2[i]=='Y')
    			Counter[6]++;
    
    	}
    
    }
    So i got this huge loop going through 2 arrays eNumber and eNumber2..i then store the amount of characters from each into one counter. The reason i'm doing this it's because it's supposed to add the characters together. so example if i input "III" and "III"
    the output should be "IIIIII." The only problem i'm having is getting the output done with the counter. I've made an array with all the characters
    Code:
    const char EG_NUMBERS[]={'I','N','9','L','/','O','Y'};
    So now i want to integrate the counter with this array. I'm having a hard time implementing it. I need it so that EG_NUMBERS outputs IIIIII, when it counts 6 "I"s. Sorry if my grammer is bad.

    EDIT:
    I was thinking of something along these lines. But K<length? of the counter?
    Code:
    	for(int k=0;k<;k++)
    		cout<<EG_NUMBERS[Counter[k]]<<endl;
    Last edited by zerlok; 02-25-2009 at 07:10 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    For loop seems like a good idea. You need the loop to run Counter times, and each loop should print out a character.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    I can't use Counter as the Loop size.
    Code:
    for(int i=0;i<Counter;i++)
    wont work because counter is an array.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, presumably you know which letter you're working on, so you would use the appropriate slot of Counter for whichever letter you're trying to print.

    If you're trying to do all the letters, then you would need another for loop for your for loop to live inside.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    Code:
    	for(int i=0;i<Counter[0];i++)
    	{	
    		cout<<"I";
    	}
    	for(int j=0;j<Counter[1];j++)
    	{
    		cout<<"N";
    	}
    i found out that this works but it's not very efficient...

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,193
    Quote Originally Posted by zerlok View Post
    Code:
    	for(int i=0;i<Counter[0];i++)
    	{	
    		cout<<"I";
    	}
    	for(int j=0;j<Counter[1];j++)
    	{
    		cout<<"N";
    	}
    i found out that this works but it's not very efficient...
    By what measure?

    The reason I ask is that I/O (eg writing to a stream) is "not very efficient" by many measures. Unless your values of Counter[0] and Counter[1] are very large, the optimisations you can do on the above are constrained practically.

    You could move the evaluation of Counter1[0] and Counter[1] out of the loops (if the array is not declared volatile), and use preincrement or rather than post-increment. Those measures might contribute to some level of efficiency gain although, in practice, a good quality compiler will often effectively do those optimisations anyway.

    If your measure of inefficiency is "using two loops, not one", then you're hosed. From a maintenance perspective it is better to use two simple but clear loops rather than one obscure one.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    Thanks for the response. The only reason i say its not efficient is because i have to write loop 7 times. So i thought of a new way of doing it.
    Code:
    	for(int i=0;i<7;i++)
    		for(int j=0;j<eSum[i];j++)
    			cout<<EG_NUMBERS[i];
    	cout<<endl<<endl;
    The loop is very efficient..
    Code:
    EG_NUMBERS[]={'I','N','9','L','/','O','Y'}
    prints it out very nicely all in one line

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 04:22 PM
  2. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:57 AM
  3. newb to C, cant figure something out.
    By Phate4219 in forum C Programming
    Replies: 16
    Last Post: 03-06-2006, 12:47 AM
  4. Can't figure out problem with code
    By Beast() in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 05:27 PM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM

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