I'm trying to write a program in C++ that will tell the user to input 5 numbers and it'll display the largest number and whether it was the 1st, 2nd 3rd...so on entry. any help? this is what I have so far

#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

const int MAXNUMS = 5;
    int count;
    double num;
int _tmain()

{

    cout << "This program will find the largest number of " << MAXNUMS << " numbers you enter.\n";
    cout << endl;
    count = 1;
    while (count <= MAXNUMS)
    {
        cout << "\n Enter a number: ";
        cin >> num;
        cout << "The number you entered is " << num;

            count++;

    cout << endl;
    return 0;

Dani AI

Generated

A single-pass scan is the simplest, fastest way to both find the largest value and remember which entry it came from. There is no need to store all five numbers or sort them. Read each value once, update a max variable when you see a larger value, and keep the 1-based index of that value. The example below shows that pattern and also checks input so you don't end up using an uninitialized value.

#include <iostream>

int main() {
    const int N = 5;
    double value;
    double maxValue = 0.0;
    int maxEntry = 0;

    for (int i = 0; i < N; ++i) {
        std::cout << "Enter number " << (i + 1) << ": ";
        if (!(std::cin >> value)) {
            std::cerr << "Invalid input\n";
            return 1;
        }
        if (i == 0 || value > maxValue) {
            maxValue = value;
            maxEntry = i + 1; // store 1-based entry number
        }
    }

    std::cout << "Largest number: " << maxValue << " (entry " << maxEntry << ")\n";
    return 0;
}

Notes and pitfalls related to earlier replies: and are right that arrays/vectors work, but sorting (as in 's example) is heavier than needed and can introduce off-by-one/out-of-bounds bugs if the inner loop runs past the last index. Also avoid nonportable DOS-era calls like clrscr()/getch() and prefer int main() over void main(). If compiling in Visual Studio with precompiled headers enabled, keep the precompiled header include as the first line; otherwise you can omit it. To choose the last occurrence of the maximum instead of the first, change the comparison to value >= maxValue.

Recommended Answers

All 6 Replies

Please wrap your code in "code" tags. Also, please give us a sample input, the current output, and the expected output.

I don't think you need this: #include "stdafx.h" You should use #include <cmath> instead of #include <math.h> Another rule is that you should pretty much never use using namespace std; Dave

Your going to have to make an array of 5 to store the inputs in so...

int nums[MAXNUMS];
count = 0;
// get input
while(count < MAXNUMS)
{
    cout << "Num: ";
    cin >> nums[count];
    cin.ignore();
}
// sort here

You should be using some sort of linear data-structure ( an array or a vector). Consider an array:

Arr         _______________________
    Pos:    | 0 | 1 | 2  | 3  | 4 |
            |___+___+____+____+___|
    Value:  | 1 | 5 | 10 | 23 | 8 |
            |___|___|____|____|___|

We find the largest number by the following algorithm:
Algorithm to find the maximum MAX of an array of length N.
1. Initialize: Set k=0 and MAX to Arr[k]
2. Check: Check if Arr[k]>Arr[k+1]
2.1 Yes: Set k=k+1
2.2 No: MAX=Arr[k+1]. Set k=k+1
3 Check overflow: Check if k==N-1
3.1 Yes: STOP
3.4 No: Go to Step 2

Hence you get the MAX as the largest value.

Always post your code in the post tags:
[code=cplusplus] //your code goes here

[/code]
PS; Did anyone noticed that I am quite good at making pictorials using simple ASCII characters?

well dont do this kind of hard working in a simple programm

  1. Take array of 5 elements a[5] as u can scan the 5 numbers.
  2. Also take another variable "temp" (for temporary use).
  3. Now swapping & comparing every element of a[5] get the largest number.

see in my programm...

#include<iostream.h>
#include<stdio.h>
#include<conio.h>

void main()
{
    int a[5],temp,i,j;
    clrscr();
    for(i=0;i<5;i++)
    {
        cout<<"Enter "<<i<<" element:-- ";
        cin>>a[i];
    }
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
            if(a[j]>a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    }


    cout<<endl<<"Maximam Number:--"<<a[j];
    getch();
}

copy\paste my prog in tour TC..

now suppose you have to find minimun number then
you just have to change the condication instead of " if(a[j]>a[j+1]) " u have to write " if (a[j]<a[j+1]) "....try it also
okay..

From:- Nirav Dangi "Proud to be an INDIAN" (Engineering Student).

commented: I am not agree with you. -1

zeus1216gw,

First, Your source code should be surrounded with bb code tags. Second, follow-up siddhant3s's post.

daviddoria>I don't think you need this: #include "stdafx.h"
-- OP uses Visual Studio - Visual C++.
daviddoria>Another rule is that you should pretty much never use using namespace std;
-- Don't put your own words.

nirav99>well dont do this kind of hard working in a simple programm. copy\paste my prog in tour TC..

-- !!TC!!
OP's program has a problems but I think you have more
problems then OP's program.

nirav99>From:- Nirav Dangi "Proud to be an INDIAN" (Engineering
Student).
-- Don't shout.

  1. Take array of 5 elements a[5] as u can scan the 5 numbers.
  2. Also take another variable "temp" (for temporary use).
  3. Now swapping & comparing every element of a[5] get the largest number.

see in my programm...

#include<iostream.h>
#include<stdio.h>
#include<conio.h>

void main()
{
    int a[5],temp,i,j;
    clrscr();

        for(i=0;i<5;i++)
    {
        cout<<"Enter "<<i<<" element:-- ";
        cin>>a[i];
    }

        for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
            if(a[j]>a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    }


    cout<<endl<<"Maximam Number:--"<<a[j];
    getch();
}

now suppose you have to find minimun number then
you just have to change the condication instead of " if(a[j]>a[j+1]) " u have to write " if (a[j]<a[j+1]) "....try it also
okay..

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.