for loop problem What's Wrong???

This is a discussion on for loop problem What's Wrong??? within the C++ Programming forums, part of the General Programming Boards category; So the first half of my code to find the average works: Code: int main() { const int SIZE = ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    10

    for loop problem What's Wrong???

    So the first half of my code to find the average works:
    Code:
    int main()
    {
    	const int SIZE = 5; //max array size
    	double x[SIZE] = {1,5,6,8,10};
    	int num_points = 0; //array index
    
    	double sum = 0;
    	double avg;
    
    	for(int i=0;i<SIZE;i++) //for loop to find the sum of array
    	{
    	sum = sum + x[num_points];
    	num_points++;
    	}
    
    	avg = sum/SIZE;
    	cout<<"the average of the values in the array is:"<<endl;
    	cout<<avg<<endl;
    but the second half to see if each value in the array is >= or < the average does not work...
    can anyone tell me why?:
    Code:
    //for loop to find if each value is >= or < the array avg.
    	for(int k=0;k<SIZE;k++)
    	{
    		if(x[k]<avg)
    		cout<<setw(5)<<x[k]<<" is less than the average"<<endl;
    		else
    		cout<<setw(5)<<x[k]<<" is greater than or equal to the average"<<endl;
    	}
    
    	return 0;
    }
    THANKS

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I havent seen the "setw()" function yet so I dident know what header to include. I took it out and ran the prog which seemed to work fine for me ??? The rest of the code seems correct too.

  3. #3
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    setw() is in the header file iomanip
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Can't see anything wrong in there. Do you do anything in between ? And what exactly "does not work" ?
    (It's time you post your whole code)
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    10
    Thanks for your help guys...
    I for got to add the #include<iomanip> - duh!

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Stick to the C++ section. This is C++ code, not C.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Agreed - moved.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fullscreen toggling problem
    By hannibar in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2005, 07:06 PM
  2. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  3. strange problem, not sure what is wrong
    By Shadow12345 in forum C++ Programming
    Replies: 7
    Last Post: 07-23-2002, 11:46 PM
  4. Can't get program to work
    By theirishrover12 in forum C Programming
    Replies: 1
    Last Post: 06-08-2002, 11:10 AM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 07:34 PM

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