Another problem...

This is a discussion on Another problem... within the C++ Programming forums, part of the General Programming Boards category; Ok, well I attempted another exercise in the book. It was to 'Write a program that reads in a sequence ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    20

    Unhappy Another problem...

    Ok, well I attempted another exercise in the book. It was to 'Write a program that reads in a sequence of positive numbers and prints out the total and average value. The end of the sequence should be signalled by entering -1'.

    This is what I came up with:

    Code:
    #include <iostream.h>
    
    int main()
    {
    	int x, y = 0, z = 0;
    	
    	while(x != -1)
    	{
    		x = 0;
    		cout << "Enter a number: ";
    		cin >> x;
    		z++;
    		y = x + y;
    	}
    	y = y + 1;
    	cout << "The average of those numbers is " << y / z;
    }
    Only when I enter -1 it messes up and says an error log is being created

    Thanks if you can help

    -Marlon

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    The code runs fine for me except for the following:

    <iostream.h> is an outdated and non-standard header that doesn't work on my compiler. You use x before it is initialized. You don't actually calculate the average correctly.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    When you enter -1 on the very first run, z is still zero, as you left it. Then you try to do this on the last line:

    Code:
    y / z




    By the way please justify this:
    Code:
    	y = y + 1;
    Last edited by jafet; 04-14-2006 at 11:37 PM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> When you enter -1 on the very first run, z is still zero, as you left it.
    Actually, in the posted code, the first input happens inside the loop where z++ is called, so z will never be zero in that code (unless the uninitialized x happens to end up with a value of -1 in the beginning).

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by jafet
    By the way please justify this:
    Code:
    	y = y + 1;
    Actually this is correct because the x-value of -1 ( the terminating value ) is still added to y. The code above compensates for that.
    Kurt

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Oh, I thought some break mechanism was in the loop. Must've been drunk

    I compiled it and entered "-1", and it worked fine.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    20
    I guess it must be this screwy PC... *Sigh*.

    ... As for all the other questions everyone else seems to have answered them before I could

    But thank you for the replies

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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