Thread: What Do I Do?

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    19

    What Do I Do?

    I tried asking this on the c++ board but no luck.

    here is my problem. I have a program that ask a user to enter some numbers it then will spit out the max and min and number of numbers he entered.

    After that it will ask them if they would like to run the program again. If they choose yes how do I clear out the numbers the entered before.

    ex: First run

    Numbers Entered: 1, 2, -1, 9
    -You entered 4 numbers
    - Largest is 9
    - Smallest is -1

    Second Run:

    Numbers Entered: 6, -8, 3, 5
    - You entered 8 numbers //Should say 4
    - Largest is 9 //Should say 6
    - Smallest is -8 //This is right.


    This is only my second program I have written so I am still a novice at this and can not figure out what to do. What is the code and where do I put it in this program?


    /* Name: Nicholas Dillon Febuary 21, 2002
    *
    *
    * Assign2.cpp
    *
    * This program finds the max and the min out of a group of numbers.
    *
    *
    * input:
    *
    * numbers
    *
    * ouput:
    *
    * max - largest number entered
    * min - smallest number entered
    *
    *
    */

    #include <iostream.h>


    int main()
    {
    const int MAX = 50; //Max size of array
    int count = 0; //Counts # of items entered
    int index = 0; //loop variable
    int numbers_array[MAX]; //Array to store numbers
    char ch = 'Y'; //Run again variable
    int max; //Maximum Variable
    int min; //Minimum Variable
    int i = 0; //variable


    do
    {
    while(ch == 'Y' || ch == 'y')
    {

    cout << "Please enter a number: "; endl;
    cin >> numbers_array[index];
    index++;
    count++;
    cout << "Please enter a number: "; endl;
    cin >> numbers_array[index];
    index++;
    count++;
    endl;
    cout << "Do you want to enter another number?(Y/N) ";
    cin >> ch;
    }

    min = numbers_array[0];
    max = numbers_array[0];

    while (i < count)
    {
    if (numbers_array[i] < min)
    {
    min = numbers_array[i];
    }
    else if(numbers_array[i] > max)
    {
    max = numbers_array[i];
    }

    i++;
    }

    cout << "You entered a total of " << count;
    cout << " numbers." <<endl;
    cout << "The largest number you entered was " << max << endl;
    cout << "The smallest number you entered was " << min << endl;
    cout << "Would you like to run program again?(Y/N)";
    cin >> ch;
    }
    while (ch == 'Y' || ch == 'y');

    return 0;
    }


    Thanks for the help! I dont knwo what the hell to do!


  2. #2
    Unregistered
    Guest
    technically there is no way to "clear" the elements entered into numbers_array[] once they have been altered, but since you are keeping track of index each time through the loop just assign 0 to index and count at the start of each time through the program rather than or in addition initializing them to 0 at the time of declaration. If there additional values in the array above element index -1 from a prior pass through then they won't be used anyway.

    The other way is to use dynamic memory to declare the array, and delete the memory after each pass through the program and reassinging memory to the array at the start of each loop rather than before the loop.

    One last approach would be to assign all elements of the array a nonsensical value at the start of each loop through the program, but that is probably the least practical of all the solutions.

  3. #3
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    not sure if this has been answered or not... and I'm not sure about how to do it in C++, but why not just reinitialize your array?

    I think your problem is that you're not resetting your counters when running the request for numbers again... hence, you're just adding array elements...

    you might just want to run through a loop in your array setting everything back to zero.. !? But I wouldn't recommend that method..

    (this was my answer on the GD board.. PLEASE DO NOT CROSS POST... you will get an answer at some point!!)

  4. #4
    0x01
    Join Date
    Sep 2001
    Posts
    88

    Try.

    Code:
    #include "iostream.h"
    #include "stdlib.h"
    
    #define MAX	50
    #define TRUE	1
    
    void Cmp_Int();
    
    int main()
    {
    	char ch;
    
    	cout << "\n Type x to see stats and quit";
    	cout << "\n\n ---\n\n";
    
    	do
    	{
    		Cmp_Int();
    
    		cout << "\n             Again?[y/n] : ";
    		cin >> ch;
    
    		cout << "\n\n";
    	}
    	while(ch == 'y' || ch == 'Y');
    
    	return 0;
    }
    
    void Cmp_Int()
    {
    	int numbers[MAX],
    		count = -1, min, max, i = 0;
    	char nbr[30];
    
    	while(TRUE)
    	{
    		cout << " Enter number[" << ++count << "] : ";
    		cin >> nbr;
    
    		if(nbr[0] == 'x' || nbr[0] == 'X')
    			break;
    
    		numbers[count] = atoi(nbr);
    	}
    
    	min = numbers[0];
    	max = numbers[0];
    
    	while(i < count)
    	{
    		if(numbers[i] < min)
    			min = numbers[i];
    
    		if(numbers[i] > max)
    			max = numbers[i];
    		
    		i++;
    	}
    
    	// Stats
    	cout << "\n ---\n";
    	cout << "\n Numbers entered         : "<< count <<"";
    	cout << "\n Largest  number entered : "<< max <<""  ;
    	cout << "\n Smallest number entered : "<< min <<""  ;
    }
    I went ahead and wrote my own version, but with your code it was just a matter of re-assigning your variables. Your code/program just kept adding them up.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    short answer: add the following line..

    count = 0;

    just below the lines that say:

    cout << "would you like to go again";
    cin >> ch;

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  6. #6
    0x01
    Join Date
    Sep 2001
    Posts
    88

    um....

    Uraldor : he's going to have to re-assign more than that ...

    Heres what I got when i compiled and executed Nick Dillion's program.
    ----
    Please enter a number: 9
    Please enter a number: 9
    Do you want to enter another number?(Y/N) y
    Please enter a number: 8
    Please enter a number: 8
    Do you want to enter another number?(Y/N) n
    You entered a total of 4 numbers.
    The largest number you entered was 9
    The smallest number you entered was 8

    ** It did everything fine above, but then I hit 'y' to try it again
    Would you like to run program again?(Y/N)y

    Please enter a number: 7
    Please enter a number: 7
    Do you want to enter another number?(Y/N) y
    Please enter a number: 5
    Please enter a number: 5
    Do you want to enter another number?(Y/N) n

    // uh oh. we have a problem
    You entered a total of 8 numbers. // the 8 is wrong - you were fixing this part by adding the count = 0 line

    // but he still has this to deal with
    The largest number you entered was 9 // wrong output
    The smallest number you entered was 5 // and again, wrong output
    Would you like to run program again?(Y/N)n
    ----

    So you see, he would have to make all his variables of type 'int' = 0, includeing all the subscripts in 'numbers_array[MAX]' inorder for the program to run properly the next time around, so i just wrote my own version.

    weee!

  7. #7
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    Originally posted by Uraldor
    short answer: add the following line..

    count = 0;

    just below the lines that say:

    cout << "would you like to go again";
    cin >> ch;
    that's the right approach... the only thing is that you need to add these two lines as well:

    index = 0;
    i = 0;

    ...one more thing: the while loop asks for two numbers rathen than one, so a part of it should probably be deleted/moved. I did that here:
    Code:
    #include <iostream.h> 
    
    int main()
    { 
        const int MAX = 50;     //Max size of array 
        int count = 0;          //Counts # of items entered 
        int index = 0;          //loop variable 
        int numbers_array[MAX]; //Array to store numbers 
        char ch = 'Y';          //Run again variable 
        int max;                //Maximum Variable 
        int min;                //Minimum Variable 
        int i = 0;              //variable 
    
        //loop the program 
        while(ch == 'Y' || ch == 'y')
        {
            cout << "Please enter a number: "; endl;
            cin >> numbers_array[index];
            index++;
            count++;
    
            //collect numbers
            while(ch == 'Y' || ch == 'y')
            {
                cout << "Please enter a number: "; endl;
                cin >> numbers_array[index];
                index++;
                count++;
                endl;
                cout << "Do you want to enter another number?(Y/N) ";
                cin >> ch;
            }
    
            min = numbers_array[0];
            max = numbers_array[0];
    
            //check for min and max    
            while (i < count)
            {
                if (numbers_array[i] < min)
                    min = numbers_array[i];
                else if(numbers_array[i] > max)
                    max = numbers_array[i];
                i++;
            } 
    
            //print results
            cout << "\n\n\tYou entered a total of " << count; 
            cout << " numbers." <<endl; 
            cout << "\tThe largest number you entered was " << max << endl; 
            cout << "\tThe smallest number you entered was " << min << endl; 
            cout << "\n\nWould you like to run program again?(Y/N)"; 
            cin >> ch;
            
            //reset ALL counters!
            count = 0;
            index = 0; 
            i = 0;
        } 
    
        return 0;
    }
    This is my signature. Remind me to change it.

Popular pages Recent additions subscribe to a feed