Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C++ Lowest 2 Values in Array

  • 27-08-2009 11:54AM
    #1
    Moderators, Computer Games Moderators Posts: 3,096 Mod ✭✭✭✭


    Hey, i'm trying to find the lowest 2 values in an array, I have to do this without adding additional data members. This is what I have so far:

    #include<iostream>
    using namespace std;
    int main()
    {

    int marks[10] = {9,8,7,6,5,4,3,2,1};
    int i;

    for(i=0;i<10;i++)
    {
    if(marks < marks[0])
    {
    marks[0] = marks;
    }
    }

    cout<<"The Lowest number is "<<marks[0]<<endl;

    for(i=1;i<10;i++)
    {
    if(marks < marks[1])
    {
    marks[1] = marks;
    }
    }

    cout<<"The second lowest number is "<<marks[1];
    }


    Cheers

    Edit - Yeah forgot a question, when i run this it just returns two zeros as both of the lowest members, what am i doing wrong? I dont want to use any sorting algorithms as this is only a 4 mark question on the paper!


Comments

  • Registered Users, Registered Users 2 Posts: 40,038 ✭✭✭✭Sparks


    Is there a question in there?
    Oh, and you've got a design bug. What happens if the second-lowest-value element in the array is the first element of the array?


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    check out your sorts - bubblesort/quicksort etc

    there are many ways of doing this


  • Registered Users, Registered Users 2 Posts: 40,038 ✭✭✭✭Sparks


    Sorting's a waste of time for that task though.


  • Closed Accounts Posts: 217 ✭✭nVid


    Its mostly about making your own algorithm,

    But the second loop will miss the first element I[0].

    Try an && with || statment, get creative.


  • Registered Users, Registered Users 2 Posts: 40,038 ✭✭✭✭Sparks


    ktulu123 wrote: »
    this is only a 4 mark question on the paper!
    Why would it matter that it's only four marks if this is an old paper you're using for study?


  • Advertisement
  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    thanks sparks - my laziness is profound sometimes :)


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    see attachment for tutorial

    mods can pull this down if not permitted


  • Moderators, Computer Games Moderators Posts: 3,096 Mod ✭✭✭✭ktulu123


    Sweet got it out by realizing that once I got the lowest value i had to ignore it in the second array!
    Cheers for the help!

    #include<iostream>
    using namespace std;
    int main()
    {

    int marks[10] = {9,8,7,6,5,4,3,2,1};
    int i;

    for(i=0;i<9;i++)
    {
    if(marks < marks[0])
    {
    marks[0] = marks;
    }
    }

    cout<<"The Lowest number is "<<marks[0]<<endl;

    for(i=1;i<8;i++)
    {
    if(marks < marks[1])
    {
    marks[1] = marks;
    }
    }

    cout<<"The second lowest number is "<<marks[1];
    }


  • Registered Users, Registered Users 2 Posts: 40,038 ✭✭✭✭Sparks


    That's still broken code ktulu.
    Change marks[] to this and try it:

    int marks[10] = {1,0,2,3,4,5,6,7,8,9};

    (and your marks[] definition creates an array with 10 spaces and 9 values - for bonus points, what happens when you access the tenth space in the array?)

    and what happens with this array?
    int marks[10] = {1,1,1,2,3,4,5,6,7,8};


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    Sparks wrote: »

    (and your marks[] definition creates an array with 10 spaces and 9 values - for bonus points, what happens when you access the tenth space in the array?)
    returns memory location
    BP Pleeeze :)


  • Advertisement
  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    Sparks wrote: »

    and what happens with this array?
    int marks[10] = {1,1,1,2,3,4,5,6,7,8};

    stops in line of code
    if(marks < marks[0])

    another BP pleeze :)

    do i still have it or what


  • Registered Users, Registered Users 2 Posts: 3,945 ✭✭✭Anima


    This forum isn't generally a place for gloating mate. Just because you might know something someone else doesn't know, doesn't warrant you posting like that. Grow up.


  • Registered Users, Registered Users 2 Posts: 40,038 ✭✭✭✭Sparks


    Especially after two wrong answers...


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    :eek:

    My God arent we all terribly serious in here


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    Sparks wrote: »
    Especially after two wrong answers...

    Ok im a bit rusty and my tutor wuld not be pleased but I should get some marks for being in the general vicinity of teh correct answer - no?


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    Can somebody throw me a bone here please people????

    Cccmmmooonnn


  • Closed Accounts Posts: 909 ✭✭✭Captain Furball


    What part don't you not understand John?


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    What part don't you not understand John?

    Thanks anyway dude - was just giving others the opportunity to apologise for hurtin me feelins

    dont know how these people sleep at night :)


  • Closed Accounts Posts: 909 ✭✭✭Captain Furball


    Lol I was under the impression you didn't know how to do something haha.
    My apologies John.


  • Closed Accounts Posts: 20 boars.ie


    What C++ specific is in this task? I mean, except "cout<<"?
    #include <stdio.h>
    
    int main(int argc, char** argv) {
     int array[11], size = 0, firsti = -1, secondi = -1, i;
     char c;
    
     for(i = 1; i < argc && size < sizeof(array)/sizeof(int); i++)
       if (sscanf(argv[i], " %d %c ", array + size, &c) == 1) size++;
    
     if (size > 0 && size < sizeof(array)/sizeof(int))
       for(firsti = 0, i = 1; i < size; i++) {
         if (array[firsti] > array[i]) {
           secondi = firsti;
           firsti = i;
         } else {
           if (array[firsti] < array[i] && (array[secondi] > array[i] || secondi < 0)) secondi = i;
         }
       }
    
     if (firsti >= 0) fprintf(stderr, "first lowest: array[%d]=%d\n", firsti, array[firsti]);
     if (secondi >= 0) fprintf(stderr, "second lowest: array[%d]=%d\n", secondi, array[secondi]);
    
     return 0;
    }
    


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 40,038 ✭✭✭✭Sparks


    john47832 wrote: »
    I should get some marks for being in the general vicinity of teh correct answer - no?
    No.


Advertisement