Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Determining Maximum values of a sequence and saving all the positions.

Constantin StamatiadisConstantin Stamatiadis ItalyMember Posts: 3

Hi guys,
my question might be quite simple, but I'm stuck and I can't go on. I'm trying to do a C program that determines the maxim value of an array inserted by a user, until here it's not difficult. The thing is that I'd like to print also the position of the maximum, but not only the position at which it occurs the first time, but all the position. That's basically my problem, I don't really know how to save the position. I'm using a "for loop' in order to determine the maximum and also the position, but by doing this also the positions of the previous maxima are saved, so then and the end I get a wrong output.
I hope my problem is clear :P

Comments

  • pseudocoderpseudocoder Member Posts: 701 ✭✭✭

    If you post your code, someone can probably provide you with a workable solution within your style.

    You only mentioned printing the max and their positions, so in general, you know the old max, the value of input at the current pos, and the pos.

    // declare and initialize variables
    
    loop(...)
    {
        // is max less than input at current position? 
        // assign max to input's value at position
        // write / format new max message    
        // write / format positions occur at message
    
        // is input at pos equal to max ?
        // write out positions
    } 

    If you need to use the max and its positions later, then you'll need to use a file or perhaps another array holding the positions. The idea would be the same... that is, when you detect a new max, rewind the file or reset the positions array to zero. To end the data set, mark it with -1 or something.

    HTH

Sign In or Register to comment.