C/C++ Get Help with C++ Homework: Solving Problems with User Input

  • Thread starter Thread starter FallArk
  • Start date Start date
  • Tags Tags
    C++ Homework
AI Thread Summary
The discussion revolves around a programming assignment that requires creating a code block to continuously prompt the user for numbers until a 0 is entered. The program must also handle the case where 0 is the first input, displaying "No data submitted!" and, upon termination, output the highest and lowest values of the entered numbers. The initial code provided successfully prompts for input but lacks the functionality to track the maximum and minimum values.Participants suggest using an array to store inputs but also highlight potential limitations regarding input size. An alternative approach is recommended, which involves initializing two variables to track the maximum and minimum values without using an array. The solution involves setting these variables equal to the first input and updating them based on subsequent inputs. The original poster confirms that they successfully implemented this solution.
FallArk
Messages
127
Reaction score
0
I ran into some problems when trying to finish my assignment. The objective of this assignment is to create a block of code that will keep asking the user to input numbers until the input is 0. And if the user input 0 for the first value it will say "No data submitted!". I got all those parts, but the assignment also requires me to let the program output the highest and lowest value of all the input when the user input 0 (the user ended the program).
This is what I have done so far:

Code:
#include <iostream>
using namespace std;
int main() {
    int input = 1;
    cout << "Enter a number: ";
    cin >> input;
    if (input == 0) {
        cout << "No data submitted!";
    }
        else {
            while (input != 0) {
                cout << "Enter a number: ";
                cin >> input;
            }
        }
        return 0;
}

I don't really know how to rank the numbers, I think I should use array, but I'm not sure if the user then can input an infinite amount of values. Thanks in advance!
 
Technology news on Phys.org
I wouldn't worry about an infinite amount of entries. Due to physical constraints the input would have to end (possibly in a buffer overrun) so limit the size of your array to what the machine can handle. Do you know how to do this? If not, I don't think it's unreasonable to limit your buffer size to, say, 1024 numbers having a value between -231 and 231 (for a 32-bit machine).

Having said that, can you now set up your array and write the code for finding the highest and lowest numbers?
 
I don't think I would use an array. I would initialize 2 variables...one for the maximum and one for the minimum and on the first input, set both of these equal to the input number. Then on subsequent inputs, if the input is less than the current minimum, set the minimum to the input value, and it the input is greater than the current maximum, set the maximum to the input value. :)
 
MarkFL said:
I don't think I would use an array. I would initialize 2 variables...one for the maximum and one for the minimum and on the first input, set both of these equal to the input number. Then on subsequent inputs, if the input is less than the current minimum, set the minimum to the input value, and it the input is greater than the current maximum, set the maximum to the input value. :)

That's what I did! I figured it out!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top