finding 2 of the largest numbers from an array... help me please!!!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: finding 2 of the largest numbers from an array... help me please!!!

  1. #1
    Join Date
    Mar 2009
    Posts
    13

    finding 2 of the largest numbers from an array... help me please!!!

    my assignment is to use an array with different values and find the 2 largest numbers from it and cout it

    heres my code so far

    #include<iostream>
    #include<iomanip>

    using namespace std;

    int main()
    {
    const int N = 15;
    int scores[N] = {10,20,30,40,50,60,70,80,90,100,110,120,130,140,150};
    int largest=0;
    int largest2 = 0;
    int i;
    int temp;

    for(i=0; i<N; i++)
    {
    if(scores[i]>scores[i+1])
    {
    largest = scores [i];
    temp = scores[i+1];
    scores[i+1] = scores[i];
    }

    }

    cout << "the largest number is " << largest << endl;

    system("PAUSE");
    return 0;
    }

    i was only able to do it for finding just the largest number but not the second largest number from the array

    please help me !!!

    thx in advance!!! :D

  2. #2
    Join Date
    May 2007
    Posts
    843
    Don't use system("PAUSE");

    I will do it find the largest from the array first then remove it and repeat to find another largest.

  3. #3
    Join Date
    Mar 2009
    Posts
    13
    well im using bloodshed dev-C++ right now and its like required to do so or else the program will start and end right away

    i was thinking the same way as u but i dont know how i should write the code exactly

    any further help will be most appreciated!

  4. #4
    Join Date
    Dec 2007
    Posts
    401
    system("pause") ; constitutes a vulnerability (an unsafe programming practice) which can be exploited by an attacker. a malicious attacker could replace the "pause" program on the machine with a program that does some kind of damage, and use it to cause trouble. spoofing a legitimate and harmless program is a common practice among malware.

    in addition, it also has the drawback that the argument string is not portable across different implementations. your code may work on windows, but may do something completely different (or not work at all) on unix.

    a portable and less risky way to get the same effect is:
    Code:
       std::cin >> std::ws ; // throw away white spaces remaining in the input buffer
       std::cin.get() ; // wait for the user to enter something
    to find the largest two values in an array, start by setting the two largest values to the first two elements of the array.
    Code:
    #include <iostream>
    #include <algorithm>
    
    int main()
    {
        const int N = 15 ;
        int scores[N] = {10,20,30,40,50,60,70,80,90,100,110,120,130,140,150} ;
    
        int largest = scores[0] ;
        int largest2 = scores[1] ;
        if( largest < largest2 ) std::swap( largest, largest2 ) ;
            // ...
    then loop through the elements in the array starting at position 2. if you find an element larger than the ones you have found so far, update the values accordingly.

    Code:
        for( int i = 2 ; i<N ; ++i )
        {
            if( scores[i] > largest )
            {
                largest2 = largest ;
                largest = scores[i] ;
            }
            else if( scores[i] > largest2 ) largest2 = scores[i] ;
        }
    Last edited by vijayan; 05-14-2009 at 03:59 AM.

  5. #5
    Join Date
    Mar 2009
    Posts
    13
    i figured it out last night

    ur code is a lot more simpler than mine tho

    thanks a lot!!!

  6. #6
    Join Date
    Dec 2003
    Posts
    3,366
    We are all doomed, since visual studio invokes the (potentially hijacked) pause command when you test your app from inside it (along with other commandline programs that are part of the suite, all of which could be trojans) :P

    Windows will not let you modify these files easily, the moment you do, it replaces them back with the original, not sure how hard it is to get past this; knowing microsoft, its probably not hard. Its a valid concern, but since its being invoked already, putting it in a program is no more of a risk than the developer already had. Clearly, leave it out of production code...

  7. #7
    Join Date
    Mar 2009
    Posts
    13
    Quote Originally Posted by jonnin View Post
    We are all doomed, since visual studio invokes the (potentially hijacked) pause command when you test your app from inside it (along with other commandline programs that are part of the suite, all of which could be trojans) :P

    Windows will not let you modify these files easily, the moment you do, it replaces them back with the original, not sure how hard it is to get past this; knowing microsoft, its probably not hard. Its a valid concern, but since its being invoked already, putting it in a program is no more of a risk than the developer already had. Clearly, leave it out of production code...
    right now im using bloodshed dev compiler and it pretty much requires the pause thing or else itll run and immediately turn off

    i also have j-grasp but im not that comfortable with it i guess

  8. #8
    Join Date
    Dec 2007
    Posts
    401
    system( argument_string ) hands argument_string to the command interpreter. the calling process waits for the shell to finish executing the command.

    on windows, if the argument string does not contain a full path, windows searches for the executable first in the directory which forms the startup directory of the process (from which the program loaded) and then the current directory for the process. system directories are searched only later. see http://msdn.microsoft.com/en-us/library/ms682425.aspx

    so the claim that "windows will not let you modify system files easily and therefore you are protected" is bogus; an attacker does not need to modify any system file - just place an executable file with the same name in the current directory.

    sure, system("PAUSE") ; will pause a program before it exits. and this pause is very useful when you are in an IDE that won't wait when the program you are testing is finished - the window closes taking all the output with it. but
    a. it is not portable (eg. works on windows, but not on linux).
    b. it has a vulnerability which could be exploited.
    c. if you are going to be a professional programmer, it's a bad habit that you'll have to break eventually anyway.

    there is a simple alternative available, which is both portable (uses only facilities provided by the standard C++ library and therefore works identically on every system) and safe. just replace system( "PAUSE" ) ; with
    Code:
    std::cin >> std::ws ; // throw away white spaces remaining in the input buffer
    std::cin.get() ; // wait for the user to enter something
    note 1: the basic issue is not with "pause" in particular, it is with the call to the system() function in general. as a programmer, one has to be pretty careful in using it.
    system( "abcd" ) ; is bad.
    system( "/usr/local/bin/abcd.exe" ) ; is much better.

    note 2: on current versions of windows, PAUSE is an intrinsic in cmd.exe, and the vulnerability is mitigated to a large extent. even so, why use a non-portable construct at all?
    Last edited by vijayan; 05-15-2009 at 04:37 AM.

  9. #9
    Join Date
    Mar 2009
    Posts
    13
    Quote Originally Posted by vijayan View Post
    system( argument_string ) hands argument_string to the command interpreter. the calling process waits for the shell to finish executing the command.

    on windows, if the argument string does not contain a full path, windows searches for the executable first in the directory which forms the startup directory of the process (from which the program loaded) and then the current directory for the process. system directories are searched only later. see http://msdn.microsoft.com/en-us/library/ms682425.aspx

    so the claim that "windows will not let you modify system files easily and therefore you are protected" is bogus; an attacker does not need to modify any system file - just place an executable file with the same name in the current directory.

    sure, system("PAUSE") ; will pause a program before it exits. and this pause is very useful when you are in an IDE that won't wait when the program you are testing is finished - the window closes taking all the output with it. but
    a. it is not portable (eg. works on windows, but not on linux).
    b. it has a vulnerability which could be exploited.
    c. if you are going to be a professional programmer, it's a bad habit that you'll have to break eventually anyway.

    there is a simple alternative available, which is both portable (uses only facilities provided by the standard C++ library and therefore works identically on every system) and safe. just replace system( "PAUSE" ) ; with
    Code:
    std::cin >> std::ws ; // throw away white spaces remaining in the input buffer
    std::cin.get() ; // wait for the user to enter something
    note 1: the basic issue is not with "pause" in particular, it is with the call to the system() function in general. as a programmer, one has to be pretty careful in using it.
    system( "abcd" ) ; is bad.
    system( "/usr/local/bin/abcd.exe" ) ; is much better.

    note 2: on current versions of windows, PAUSE is an intrinsic in cmd.exe, and the vulnerability is mitigated to a large extent. even so, why use a non-portable construct at all?
    cool good to know

    my brother did tell me back then to use the pause because im a beginner once i get better he did say that i wont use it much or at all

Similar Threads

  1. need help finding prime using array
    By yaoc1987 in forum C++
    Replies: 8
    Last Post: 04-29-2009, 09:59 PM
  2. Replies: 1
    Last Post: 05-31-2006, 03:14 AM
  3. finding numbers within a string
    By Phaelax in forum Java
    Replies: 0
    Last Post: 04-23-2006, 01:23 AM
  4. Replies: 0
    Last Post: 05-27-2005, 02:23 AM
  5. Replies: 2
    Last Post: 04-15-2005, 10:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center