Thread: Parallel Arrays-HELP!

  1. #1
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    Parallel Arrays-HELP!

    I'm using Bloodshed..
    I'm trying to make this program accept input for prices of 10 items, calculate and print sum, then the average price of all. I need to print the difference between the item price and average in their respective columns. I have NO idea where to get started with the 2d Arrays..I started with a regular array to test it, but I get crazy output- I noticed one thing.When the inputs are less than 20. or above, the output is fine..Can anyone Help??

    Code:
    #define Max 9
    #include <iomanip.h>
    #include <stdlib.h>
    
    main()
      {  float Price[Max], Sum, Avg;int C;
    
         for(C=0;C<Max;C++)
           {cout<<"\n Enter Price "<<C<<" : ";
            cin>>Price[C];
           }
         for(C=0;C<Max;C++)
           { Sum=Sum+Price[C];}
    
           Avg=Sum/9.0;
    
            cout<<"\n Sum = "<<Sum<<endl;
    
    
            cout.setf(ios::floatfield, ios::showpoint);
            cout.setf(ios::fixed);
            cout<<setprecision(2);
    
            cout<<Avg<<endl;
          system("PAUSE");
          return 0;
    }
    ***Output

    Enter Price 0 : 12.99

    Enter Price 1 : 13.99

    Enter Price 2 : 14.99

    Enter Price 3 : 15.99

    Enter Price 4 : 16.99

    Enter Price 5 : 17.99

    Enter Price 6 : 18.99

    Enter Price 7 : 19.99

    Enter Price 8 : 20.99

    Sum = 1.0442e+34
    1160222550475306377922293111193600.00
    Press any key to continue . . .

    Please be merciful
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802

    Does this work?

    Code:
    #define Max 9
    #include <iomanip.h>
    #include <stdlib.h>
    
    
    main()
    {
    	float Price[Max], Avg;
    	float Sum = 0;
    	int C;
    	for (C=0;C<Max;C++)
    	{
    		Price[Max] = 0; 
    	}
    
    	for (C=0;C<Max;C++)
    	{
    		cout<<"\n Enter Price "<<C<<" : ";
    		cin>>Price[C];
    	}
         	for(C=0;C<Max;C++)
    	{ 
    		Sum=Sum+Price[C];
    	}
    
    	Avg=Sum/9;
    
    	cout<<"\n Sum = "<<Sum<<endl;
    
    
    	cout.setf(ios::floatfield, ios::showpoint);
    	cout.setf(ios::fixed);
    	cout<<setprecision(2);
    
    	cout<<Avg<<endl;
    	system("PAUSE");
    	return 0;
    }
    Last edited by Dual-Catfish; 03-28-2002 at 03:40 PM.

  3. #3
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Don't forget to initilize your variables.

    int i;
    i++;

    Will produce undesired results.

    int i = 0;
    i++;

    Should give you the desired result.

  4. #4
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    Ahh Yes..it works like a charm..thanks a lot
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parallel arrays
    By beginner1 in forum C Programming
    Replies: 2
    Last Post: 05-19-2009, 08:53 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Parallel Arrays
    By kippwinger in forum C++ Programming
    Replies: 3
    Last Post: 06-26-2003, 03:18 PM
  5. parallel arrays
    By KeeNCPP in forum C++ Programming
    Replies: 1
    Last Post: 10-18-2001, 09:56 PM