Thread: for loop problem What's Wrong???

  1. #1
    Registered User
    Join Date
    Aug 2007

    for loop problem What's Wrong???

    So the first half of my code to find the average works:
    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];
    	avg = sum/SIZE;
    	cout<<"the average of the values in the array is:"<<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?:
    //for loop to find if each value is >= or < the array avg.
    	for(int k=0;k<SIZE;k++)
    		cout<<setw(5)<<x[k]<<" is less than the average"<<endl;
    		cout<<setw(5)<<x[k]<<" is greater than or equal to the average"<<endl;
    	return 0;

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    On me hyperplane
    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
    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

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Toronto, Ontario, Canada
    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
    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
    Stick to the C++ section. This is C++ code, not C.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

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, 08: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, 08:34 PM