Thread: Need help with project

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    4

    Need help with project

    Here is the project im working on, im having a hard time figuring out an algorithm that will read the minimum and maximum values. Also when i am confused about the end of input signal(cntl+Z). My instructor also tells me that the program should instantly print the output and that i dont have to hit enter once I use it. I think he is completely wrong about this but I am not sure. My program also keeps assigning ^Z a numerical value.






    Project description:

    Design and implement a C++ program that works as follows.

    First read a sequence of integer values. Stop reading values when the user enters the end-of-input signal, which is usually a <Ctrl-D> or <Ctrl-Z> combination on most computers.

    Then print the following statistics:

    * the total number of values that were entered
    * the number of times the value 0 appeared
    * the number of positive values
    * the number of negative values
    * the number of even values
    * the number of odd values
    * the minimum value
    * the maximum value
    * the sum of all the values
    * the average of all the values (as a floating point number)
    * the product of all the values (as a floating point number)

    Your program should correctly handle unusual cases such as when all the values are negative, or all the values are positive, or the total number of values entered is 0.

    The only library your C++ program may include is <iostream>.

    Here is a sample run. The user input is shown in boldface red.
    Enter value: 3
    Enter value: 6
    Enter value: 12
    Enter value: -2
    Enter value: -5
    Enter value: -8
    Enter value: 7
    Enter value: 1
    Enter value: 2
    Enter value: 5
    Enter value: <Ctrl-Z>

    Number of values = 10
    Number of zero values = 0
    Number of positive values = 7
    Number of negative values = 3
    Number of even values = 5
    Number of odd values = 5
    Minimum value = -8
    Maximum value = 12
    Sum = 21
    Average = 2.1
    Product = -1209600

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    My suggestion is to store the numbers in a dynamic array. Once you do that, finding the minimum and maximum values should be rather easy.

    Also when i am confused about the end of input signal(cntl+Z).
    In a way this allows you to simplify how you read the input values. You can use, say:
    Code:
    int num;
    while (std::cin >> num)
    {
    	// ...
    }
    Then expect the user to break out of the otherwise infinite input loop.

    My instructor also tells me that the program should instantly print the output and that i dont have to hit enter once I use it. I think he is completely wrong about this but I am not sure.
    I think your instructor is wrong too.

    Your program should correctly handle unusual cases such as when all the values are negative, or all the values are positive, or the total number of values entered is 0.
    Another thing you may want to consider is what if invalid input is entered, e.g. an alphabetic character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    4
    sorry this was my effort so far



    Code:
    #include <iostream>
    using namespace std;
    void main()
    {
    	int input,int positive=0,int negative=0;
    	int odd=0,int even=0,int total_zeros=0,int total_numbers=0;
        
    
    
        while (!cin.eof()) 
    	{
        
        cout <<"enter value:\n";
    	cin >>input;
    
    	cout <<"enter value:\n";
    	cin >>input;
    	
        if ((input%2)==0)
        even ++;
    	else
    	odd ++;
    	
        if (input >= 0)
        positive ++;
    	else
    	negative ++;
    	
    	if (input ==0)
    	total_zeros ++;
    	else
    	total_numbers ++;
    
    	}
    	
    	cout <<"positive"<<positive<<endl;
    	cout <<"negative"<<negative<<endl;
    	cout <<"even"<<even<<endl;
    	cout <<"odd"<<odd<<endl;
        cout <<"total zeros"<<total_zeros<<endl;
        }

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    4
    In a way this allows you to simplify how you read the input values. You can use, say:

    Code:
    int num;
    while (std::cin >> num)
    {
    	// ...
    }
    when i use this method it doesnt recognize the first number i input, it does recognize all the subsequent ones. should i put the cin>>input inside the brackets of the loop?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It might be because you have "cin >>input" twice in your program above. The only place that should be is inside the while control: while (cin >> input).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM