I need help with this assignment, I dont know where to start. Here is what the assignment is:
Write a program that finds both the largest and smallest of several integers. Assume that the first integer read specifies the number of values remaining to be entered and that the first number is not one of the integers to compare. Your program should read only one value per input statement. For example, an input sequence might be

6 20 30 40 50 60 70

where the 6 indicates that the subsequent 6 values are to be compared.
Can someone please help? This is all i have come up with:

//Assign2: assign2_00.cpp
// project finds the largest and smallest numbers of 6 integers
#include <iostream>
using namespace std;

int main()
{
    int counter = 1;
    int number;

    while (counter<=6)
    {
        cout<<"Enter number:  "<<endl;
        counter++;
        cin>>number;
    }
    {
        if (number>=70)
            cout<<"largest number is:  "<<number<<endl;

        if (number <=20)
            cout<<"smallest number is:  "<<number<<endl;
    }


}

Dani AI

Generated

Short answer: read the first integer as the count (not a value to compare), then read exactly that many integers and track the smallest and largest as you go.

— the main problems in your code are (a) you hard-coded 6 instead of using the first number the user types, (b) you increment the counter before reading so you skip the first input, and (c) you only ever compare the last-read value against fixed thresholds (20 and 70) instead of maintaining running min/max. ’s reply shows a working approach but uses manual new/delete and has a memory‑free bug (if you use new[] you must use delete[]), so prefer a simpler pattern.

Simple, robust pattern to follow:

  • Read n (the count). If n <= 0 stop or report error.
  • Read the first data value and set both smallest and largest to it.
  • Loop n-1 times reading values and update smallest/largest with if (v < smallest) smallest = v; and if (v > largest) largest = v;.
  • Print results after the loop.

Example (concise and focused on the algorithm):

#include <iostream>

int main() {
    int n;
    if (!(std::cin >> n) || n <= 0) return 0;   // bad count -> exit
    int v;
    if (!(std::cin >> v)) return 0;             // no values -> exit
    int smallest = v, largest = v;
    for (int i = 1; i < n; ++i) {
        if (!(std::cin >> v)) break;
        if (v < smallest) smallest = v;
        if (v > largest)  largest = v;
    }
    std::cout << "Smallest: " << smallest << "\nLargest: " << largest << "\n";
}

Troubleshooting tips: validate n, check std::cin.fail() when non-numeric input is possible, print after the loop (not inside), and avoid off-by-one errors in the loop bound. Use std::vector if you must store inputs; otherwise you don’t need any dynamic allocation at all. ’s point about learning is valid—understand the algorithm above and then adapt the code for your assignment’s required style.

Recommended Answers

All 4 Replies

// NOTE: No error checking on input values
//


#include<iostream>
// use limits.h to get minimum and maximum values
#include "limits.h"

using namespace std;


int main(int argc, char* argv[])
{
   int iCount = 0;
   // set up initial comparison values
   int iMax = INT_MIN;  // ALMOST guaranteed to be bigger than this
   int iMin = INT_MAX;  // ALMOST guaranteed to be smaller than this
   // get the count of how many we should be comparing

   cout << "Enter the number of integers to read and compare "<< endl;
   cin >> iCount;
   if (iCount > 0) {
      // allocate an array to hold the values
      int *values = new int[iCount];
      if (values != NULL) {
         for (int index = 0; index < iCount; index++) {
            cout << "Enter Value " << index+1 << endl;
            cin >> values[index];
            if (values[index] >= iMax) {
               iMax = values[index];
            }
            if (values[index] <= iMin) {
               iMin = values[index];
            }
         }
         // free up the allocated memory
         delete values;
         cout << "Minimum value was " << iMin << endl;
         cout << "Maximum value was " << iMax << endl;
      } else {
         cout << "Error: Unable to allocate memory for " << iCount << " values." << endl;
      }

   } else {
      cout << "Nothing to do (Zero integers to compare)" << endl;
   }
	return 0;
}
commented: Good job! Now he can cheat his way to an A -3

Consider:

Assume that the first integer read specifies the number of values remaining to be entered and that the first number is not one of the integers to compare.

What is the purpose of the first number to be read? How would you use it?
What is the purpose of the 6 in your while statement?

Write a program that finds both the largest and smallest of several integers.

How would you do this on paper?


To chief427:
What grade to you expect to get for your code? What grade should lou38 get using your code? Please do not give working programs to students. We help people solve their problems so they learn, not write the code for them so they can cheat.

commented: Daniweb described in simplest words :) +2

My apologies. I simply thought someone had a problem and needed help.
Is this (site) all about helping students or is it about programming problems generally ?
I don't care what grade I get for my code, only that it works. If I have learnt one thing in 20+ years of programming it is that there is a significant difference between what is expected by an academic in an assignment and what is expected by an employer in your job.

My apologies. I simply thought someone had a problem and needed help.

And you were correct. As the OP stated "I need help with this assignment,..."


Is this (site) all about helping students or is it about programming problems generally ?

Both. But it's not about writing code for someone. It's about helping them solve their own problems and thereby learn.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.