Array Torments

This is a discussion on Array Torments within the C++ Programming forums, part of the General Programming Boards category; I Keep Getting a Funny Result when i cout<<array[100]/customers(50)*100 the result is some weird number, 11478562 when it should be ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    48

    Array Torments

    I Keep Getting a Funny Result when i cout<<array[100]/customers(50)*100 the result is some weird number, 11478562 when it should be more like 20 or something in that region

  2. #2
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Post some code.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    48
    Here Is the Code

    Code:
    #include <iostream>
    using namespace std;
    int Another();
    int main()
    {
    
    	int code;
    	int code_arr[100];
    	int counter_arr[100];
    	int customers=0;
    	int i;
    
    	for(i=0; i<100; ++i)
    	{
    		code_arr[i]=0;
    	    counter_arr[i]=0;
    	}
    
    	while(Another())
    	{
    
    	cout<<"Enter Your Zip Code:";
    	cin>>code;
    	cout<<endl;
    
    	for(i=0; i<100; ++i)
    	{
    		if(code_arr[i]==code)
    		{
    			counter_arr[i]+=1;
    		    cout<<"Added Counter!!"<<endl;
    			++customers;
    			break;
    		}
    	
    	}
    	if(i>=100)
    		for(i=100; i>0; --i)
    		{
    			if(code_arr[i]==0)
    			{
    				code_arr[i]=code;
    			    counter_arr[i]+=1;
    				cout<<"Added Address!!"<<endl;
    				++customers;
    				break;
    			}
    		}
    
    
    
    	}
    
    	for(int l=0; l<100; ++l)
    		if(code_arr[l]>0)
    		{
    
    			cout<<"Zip Code #"<<l+1<<endl
    			    <<"Zip Code #"<<code_arr[l]<<endl
    		        <<"Number Of Zip Codes from this area : # "<<counter_arr[l]<<endl
    				<<"Total &#37; of total customers : "<<t<<endl<<endl;
    			
    		}
    		cout<<"Total Customers All Up : "<<customers<<endl<<endl;
    
    
    
    		
    
    	return 0;
    }
    int Another()
    {
    	int response;
    	cout<<endl;
    
    	cout<<"Would You Like to Process Another Zip Code : ";
    	cin>>response;
    
    	cout<<endl;
    
    
    	return response;
    }

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    48
    the last part is the worry, and i forgot to add it!!

    here is the problem
    Code:
    <<"Total &#37; of total customers : "<<counter_arr[l]/customers*100<<endl<<endl;

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Given that all these variables are int, I would expect you to get 0 as an answer. (Assuming counter_arr[l] < customers, that division will give 0, and 0*100 is 0.)

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    48
    i see, beginners ignorance, and lack of sleep, thanks for that....
    i see i got to use doubles i forgot that principal, maths in c++ is a different thing, you got to be aware of doubles and int etc as well

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    for(i=100; i>0; --i)
    {
        if(code_arr[i]==0)
    This causes out-of-bounds access.

    Code:
    <<"Total &#37; of total customers : "<<counter_arr[l]/customers*100<<endl<<endl;
    Because of integer division this should always print 0: smaller / larger = 0 if smaller and larger are integers.
    You can try multiplying by 100 first, or cast customers or counter_arr[l] or both to double to force floating point division.

    An easier way to write this kind of program is to use std::map (something like):
    Code:
        map<int, int> zip;
        do {
            cout<<"Enter Your Zip Code:";
            cin>>code;
            cout<<endl;
            ++zip[ code]; //this does the logic of your two for-loops
         } while (Another());
         //print statistics
    Last edited by anon; 06-12-2008 at 08:25 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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