Open Side Menu Go to the Top

02-06-2013 , 11:50 PM
So, my prof wanted us to dynamically create an array, have the user input numbers, and output the smallest number the user entered. My solution works, and I think shows that I understand the concepts the prof was trying to teach. Further, I think I'm a lot closer to understanding pointers than I was two weeks ago. Here is my code. How would you have solved this in a superior way? What would you have done differently and why?

Code:
#include <cstdlib>
#include <iostream>
#include <new>

using namespace std;

void getInput (int *piUserInput);
void getNumbers(int[], int);
int findSmallestInput(int [], int);

int main()
{
    int iSmallest = 0;
    int *piUserInput = new int(0);
    int* numberArray = 0;
        
    // Create a function for getting user input   
    getInput(piUserInput);
    int end = *piUserInput;  
        
    numberArray = new int[*piUserInput];
    
    //create array that gets that amount of numbers
    getNumbers(numberArray, end);

    // exterior loop runs to swap every number since the largest 
    // is in the last slot in the Array.
    iSmallest = findSmallestInput(numberArray, end);

    cout << "The smallest number entered was " << iSmallest << "." << endl;

    delete piUserInput;
    delete []numberArray;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void getInput(int *piUserInput)
{
    cout << "How many numbers would you like to enter: ";
    cin >> *piUserInput;
}

void getNumbers(int numberArray[], int end)
{
    int n=0;  
    for(int i = 0; i < end; i++)
        {
            cout << "Please enter #" << i+1 << ": ";
            cin >> n;
            numberArray[i] = n;
        }
}

int findSmallestInput(int numberArray[], int end)
{
    int temp = 0;
        for (int j = end -1; j>0; j--)   
        {
            // swap numbers     
            for (int i = 0; i < end; i++)
            {
                if (numberArray[i] > numberArray[i+1])//compare and swap
                {
                    temp = numberArray[i+1];
                    numberArray[i+1] = numberArray[i];
                    numberArray[i] = temp;
                }// end compare and swap if
            }// closes for loop that swaps
            end--;
        }// closes for loop that counts down
    return numberArray[0];
}
Another c++ array question Quote
Another c++ array question
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Another c++ array question
02-07-2013 , 12:03 AM
Why sort the array if you only care about the smallest number?

Code:
min = numberArray[0];
for(i=1; i < end; i++)
{
    if (min < numberArray[i]) min = numberArray[i];
}
return min;
Another c++ array question Quote
02-07-2013 , 03:23 AM
dynamically allocating piUserInput makes no sense. The code will work but it's not right. You know it's always going to be exactly one int and it never needs to leave the function it's defined in.

Your names need work. getInput() is bad because the function doesn't just get input. Don't use very generic names like getInput unless the function is doing something very generic like getting generic input. This function is for finding out how many numbers there will be. The name should reflect that.

In general when dealing with users you don't want to force them to decide stuff like how many numbers in advance. Instead they get to do what they want and you just deal with it. Obviously the problem definition trumps being nice to users, but I like to think about the user point of view -- what interface would allow them to enter a list of arbitrary size? how would they terminate it? The natural friendly way that comes to mind is to print the list and answer after each entry along with a prompt to exit.
Another c++ array question Quote
02-07-2013 , 10:27 AM
Quote:
Originally Posted by Xhad
Why sort the array if you only care about the smallest number?

Code:
min = numberArray[0];
for(i=1; i < end; i++)
{
    if (min < numberArray[i]) min = numberArray[i];
}
return min;
because I couldn't make it work. I tried because I knew that my sort was always putting the highest value in the highest array element. When I get home I'll try it with this solution, seems better.
Another c++ array question Quote
02-07-2013 , 10:34 AM
Quote:
Originally Posted by Chips Ahoy
dynamically allocating piUserInput makes no sense. The code will work but it's not right. You know it's always going to be exactly one int and it never needs to leave the function it's defined in.
I feel like that was part of the assignment to ensure that students understand dynamic allocation.

Quote:
Originally Posted by Chips Ahoy
Your names need work. getInput() is bad because the function doesn't just get input. Don't use very generic names like getInput unless the function is doing something very generic like getting generic input. This function is for finding out how many numbers there will be. The name should reflect that.
That's an easy enough adjustment to make, thanks for the tip.

Quote:
Originally Posted by Chips Ahoy
In general when dealing with users you don't want to force them to decide stuff like how many numbers in advance. Instead they get to do what they want and you just deal with it. Obviously the problem definition trumps being nice to users, but I like to think about the user point of view -- what interface would allow them to enter a list of arbitrary size? how would they terminate it? The natural friendly way that comes to mind is to print the list and answer after each entry along with a prompt to exit.
In order to do that I would just wrap everything in something like?

while(true){
printArray when user enters number}
if user enters exit continue;

I'll try this as well when I get home tonight.
Another c++ array question Quote
02-07-2013 , 11:08 AM
You could just assume they are done if they enter anything but a number or negative sign. This way they could exit with q, e, quit, exit, finished, done or whatever they think "should" exit the program.
Another c++ array question Quote
02-07-2013 , 11:31 AM
Quote:
Originally Posted by Shoe Lace
You could just assume they are done if they enter anything but a number or negative sign. This way they could exit with q, e, quit, exit, finished, done or whatever they think "should" exit the program.
The tradeoff for this is that the user might end up exiting by accident if they make a small typo.

Not saying Shoe's wrong or you shouldn't do it that way - just something else to think about.
Another c++ array question Quote
02-07-2013 , 11:42 AM
Quote:
Originally Posted by aisflat439
So, my prof wanted us to dynamically create an array, have the user input numbers, and output the smallest number the user entered. My solution works, and I think shows that I understand the concepts the prof was trying to teach. Further, I think I'm a lot closer to understanding pointers than I was two weeks ago. Here is my code. How would you have solved this in a superior way? What would you have done differently and why?

Code:
#include <cstdlib>
#include <iostream>
#include <new>

using namespace std;

void getInput (int *piUserInput);
void getNumbers(int[], int);
int findSmallestInput(int [], int);

int main()
{
    int iSmallest = 0;
    int *piUserInput = new int(0);
    int* numberArray = 0;
        
    // Create a function for getting user input   
    getInput(piUserInput);
    int end = *piUserInput;  
        
    numberArray = new int[*piUserInput];
    
    //create array that gets that amount of numbers
    getNumbers(numberArray, end);

    // exterior loop runs to swap every number since the largest 
    // is in the last slot in the Array.
    iSmallest = findSmallestInput(numberArray, end);

    cout << "The smallest number entered was " << iSmallest << "." << endl;

    delete piUserInput;
    delete []numberArray;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void getInput(int *piUserInput)
{
    cout << "How many numbers would you like to enter: ";
    cin >> *piUserInput;
}

void getNumbers(int numberArray[], int end)
{
    int n=0;  
    for(int i = 0; i < end; i++)
        {
            cout << "Please enter #" << i+1 << ": ";
            cin >> n;
            numberArray[i] = n;
        }
}

int findSmallestInput(int numberArray[], int end)
{
    int temp = 0;
        for (int j = end -1; j>0; j--)   
        {
            // swap numbers     
            for (int i = 0; i < end; i++)
            {
                if (numberArray[i] > numberArray[i+1])//compare and swap
                {
                    temp = numberArray[i+1];
                    numberArray[i+1] = numberArray[i];
                    numberArray[i] = temp;
                }// end compare and swap if
            }// closes for loop that swaps
            end--;
        }// closes for loop that counts down
    return numberArray[0];
}
What happens if the user hits ctrl c or otherwise aborts instead of inputting any numbers? It's not good.

Last edited by adios; 02-07-2013 at 11:58 AM.
Another c++ array question Quote
02-07-2013 , 12:18 PM
One more thing, you should be using argc and argv (as chipsahoy implies) to read the user input. Just want you to ace this one.
Another c++ array question Quote
02-07-2013 , 12:57 PM
Quote:
Originally Posted by jjshabado
The tradeoff for this is that the user might end up exiting by accident if they make a small typo.

Not saying Shoe's wrong or you shouldn't do it that way - just something else to think about.
I was thinking key + enter, not just the key on its own. Would need a little more work to make sure each number is ok, assuming it's space delimited but I could see that check needing to be done no matter what if the final solution ends up letting the user enter more than 1 number at once in one way or another.
Another c++ array question Quote
02-08-2013 , 03:26 PM
Hey guys. The assignment was handed in on Wednesday at like 10pm (around the time I posted it here). I got full credit for the assignment. Nevertheless I made a few of the changes you guys suggested - need all the practice I can get. Here are the results.

I struggled with the array that Xhad suggested as he wrote it but used this version that I think works okay.
I added a simple while statement so if you enter anything that isn't a number it re-asks. I think thats a good practice and good suggestion.
I haven't learned anything about argc and argv so they remain out of the program for now. Of course, now I have to learn what they mean since they came up in this post.
Shoe Lace makes the suggestion of giving them an exit path rather than entering a number. I agree that may be a better program but I think that because the nature of the program was academic, following the request of the prof makes more sense.

Code:
#include <cstdlib>
#include <iostream>

using namespace std;

void getInput (int *piUserInput);
void getNumbers(int[], int);
int findSmallestInput(int [], int);
int moreElegantSort (int[], int);

int main()
{
    int iSmallest = 0, iAlternateSmallest = 0;
    int *piUserInput = new int(0);
    int* numberArray = 0;
        
    // Create a function for getting user input   
    
    getInput(piUserInput);
    int end = *piUserInput;  
        
    numberArray = new int[*piUserInput];
    
    //create array that gets that amount of numbers
    getNumbers(numberArray, end);

    // exterior loop runs to swap every number since the largest 
    // is in the last slot in the Array.
    iSmallest = findSmallestInput(numberArray, end);

    cout << "The smallest number entered was " << iSmallest << "." << endl;
    
    iAlternateSmallest = moreElegantSort (numberArray, end);
    
    cout << "An alternate answer says the smallest number is " << iAlternateSmallest << "." << endl;
    
    delete piUserInput;
    delete []numberArray;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void getInput(int *piUserInput)
{
    
    cout << "How many numbers would you like to enter: ";
    while (!(cin >> *piUserInput)){
        cin.clear();
        cin.ignore(256,'\n');
        cout << "How many numbers would you like to enter: ";
        }//end while
}

void getNumbers(int numberArray[], int end)
{
    int n=0;  
    for(int i = 0; i < end; i++)
        {
            cout << "Please enter #" << i+1 << ": ";
            cin >> n;
            numberArray[i] = n;
        }
}

int findSmallestInput(int numberArray[], int end)
{
    int temp = 0;
        for (int j = end -1; j>0; j--)   
        {
            // swap numbers     
            for (int i = 0; i < end; i++)
            {
                if (numberArray[i] > numberArray[i+1])//compare and swap
                {
                    temp = numberArray[i+1];
                    numberArray[i+1] = numberArray[i];
                    numberArray[i] = temp;
                }// end compare and swap if
            }// closes for loop that swaps
            end--;
        }// closes for loop that counts down
    return numberArray[0];
}

//Here I'm passing by value instead of using "end"
//which makes it a touch more readable.
int moreElegantSort (int numberArray[], int iSize)
{
    int min = 0;
    // this sets min equal to whatever is in element 0
    min = numberArray[0];
    // this will loop as many times as the user inputs 
    for (int i = 0; i < iSize; i++)
    {
        // this as long as min(element 0) is greater than
        // the element held in [i] thats fine, but if it 
        // isn't, then min is no longer element zero but is
        // now element [i] the loop continues and return min
        if (min > numberArray[i]) min = numberArray[i];
    }   
    return min;
    
}
This week I'm creating objects for the first time. Once my homework is complete I'll post it again. I appreciate the feedback as it helps me learn and practice.
Another c++ array question Quote
02-08-2013 , 03:51 PM
Chances are you'll learn about argc/argv very soon in your course, maybe even in the next lecture or 2 and that will end up being a really clean and usable solution if you're dealing with a command line app.
Another c++ array question Quote
02-08-2013 , 06:55 PM
Quote:
Originally Posted by aisflat439
Hey guys. The assignment was handed in on Wednesday at like 10pm (around the time I posted it here). I got full credit for the assignment. Nevertheless I made a few of the changes you guys suggested - need all the practice I can get. Here are the results.

I struggled with the array that Xhad suggested as he wrote it but used this version that I think works okay.
I added a simple while statement so if you enter anything that isn't a number it re-asks. I think thats a good practice and good suggestion.
I haven't learned anything about argc and argv so they remain out of the program for now. Of course, now I have to learn what they mean since they came up in this post.
Shoe Lace makes the suggestion of giving them an exit path rather than entering a number. I agree that may be a better program but I think that because the nature of the program was academic, following the request of the prof makes more sense.

Code:
#include <cstdlib>
#include <iostream>

using namespace std;

void getInput (int *piUserInput);
void getNumbers(int[], int);
int findSmallestInput(int [], int);
int moreElegantSort (int[], int);

int main()
{
    int iSmallest = 0, iAlternateSmallest = 0;
    int *piUserInput = new int(0);
    int* numberArray = 0;
        
    // Create a function for getting user input   
    
    getInput(piUserInput);
    int end = *piUserInput;  
        
    numberArray = new int[*piUserInput];
    
    //create array that gets that amount of numbers
    getNumbers(numberArray, end);

    // exterior loop runs to swap every number since the largest 
    // is in the last slot in the Array.
    iSmallest = findSmallestInput(numberArray, end);

    cout << "The smallest number entered was " << iSmallest << "." << endl;
    
    iAlternateSmallest = moreElegantSort (numberArray, end);
    
    cout << "An alternate answer says the smallest number is " << iAlternateSmallest << "." << endl;
    
    delete piUserInput;
    delete []numberArray;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void getInput(int *piUserInput)
{
    
    cout << "How many numbers would you like to enter: ";
    while (!(cin >> *piUserInput)){
        cin.clear();
        cin.ignore(256,'\n');
        cout << "How many numbers would you like to enter: ";
        }//end while
}

void getNumbers(int numberArray[], int end)
{
    int n=0;  
    for(int i = 0; i < end; i++)
        {
            cout << "Please enter #" << i+1 << ": ";
            cin >> n;
            numberArray[i] = n;
        }
}

int findSmallestInput(int numberArray[], int end)
{
    int temp = 0;
        for (int j = end -1; j>0; j--)   
        {
            // swap numbers     
            for (int i = 0; i < end; i++)
            {
                if (numberArray[i] > numberArray[i+1])//compare and swap
                {
                    temp = numberArray[i+1];
                    numberArray[i+1] = numberArray[i];
                    numberArray[i] = temp;
                }// end compare and swap if
            }// closes for loop that swaps
            end--;
        }// closes for loop that counts down
    return numberArray[0];
}

//Here I'm passing by value instead of using "end"
//which makes it a touch more readable.
int moreElegantSort (int numberArray[], int iSize)
{
    int min = 0;
    // this sets min equal to whatever is in element 0
    min = numberArray[0];
    // this will loop as many times as the user inputs 
    for (int i = 0; i < iSize; i++)
    {
        // this as long as min(element 0) is greater than
        // the element held in [i] thats fine, but if it 
        // isn't, then min is no longer element zero but is
        // now element [i] the loop continues and return min
        if (min > numberArray[i]) min = numberArray[i];
    }   
    return min;
    
}
This week I'm creating objects for the first time. Once my homework is complete I'll post it again. I appreciate the feedback as it helps me learn and practice.
Even though the OS will often clean up resources in user mode your solution has a potential for a memory leak. You dynamically allocate memory before the user makes each selection. If the user aborts the program the deletes don't get executed. This is easily fixed by passing a pointers to your methods and allocatimg memory dynamicallyafter the user has made their selections in your example.
Another c++ array question Quote
02-08-2013 , 07:05 PM
Memory leaks can be very difficult to find btw
If you ever get involved in certain types of software development keeping them out of your code is critical. Microsoft has devoted a lot of their debugging tools to detecting memory leaks in kernel mode software for instance.
Another c++ array question Quote
02-08-2013 , 07:08 PM
If Valgrind was a chick I would marry her.
Another c++ array question Quote
Another c++ array question
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Another c++ array question

      
m