Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,
I have an array of integers
I like to retrieve the max number and index of max number from that array without using Math.Max() function..
what is the easiest way...

Cheers
Santhosh ;P ;P

C#
function insRows()
{
    var x = document.getElementById('finalMarks').insertRow();
    var cell = x.insertCell(0)
    var a = x.insertCell(1);
    var b = x.insertCell(2);
    var c = x.insertCell(3);
    var d = x.insertCell(4);
    var element1 = document.createElement("input");
        element1.type = "checkbox";
        cell.appendChild(element1);
    a.innerHTML = document.getElementById('txtqualify').options[document.getElementById('txtqualify').selectedIndex].text
    b.innerHTML = document.getElementById('txtsem').value
    c.innerHTML = document.getElementById('txtmaxmrk').value
    d.innerHTML = document.getElementById('txtobtmrk').value
}

function function1()
{
   var myRow = document.all.finalMarks.rows.length;
   var ed='';
   var mm=0,om=0, x,z;
   for(i=0;i<myRow;i++)
   {
        r = document.all.finalMarks.rows[i];
    pr = r.cells[1].innerHTML
    mm+= parseInt(r.cells[3].innerHTML)
    om+= parseInt(r.cells[4].innerHTML)
    if (pr!=ed && ed!='')//(i==myRow-1)
        {
            prevr = document.all.finalMarks.rows[i-1];
            mycurrent_cell = document.createElement("td");
            mm-= parseInt(r.cells[3].innerHTML)
            om-= parseInt(r.cells[4].innerHTML)
            z = (om/mm)*100
            mycurrent_cell.innerHTML = z
            mm= parseInt(r.cells[3].innerHTML)
            om= parseInt(r.cells[4].innerHTML)
            alert(mycurrent_cell.innerHTML  +   '['  + pr + ']' + ed)
            //alert(!isNaN(mycurrent_cell.innerHTML))
            if(!isNaN(mycurrent_cell.innerHTML))
            {
                qualary[qualary.length] = prevr.cells[1].innerHTML
                pctary[pctary.length] = mycurrent_cell.innerHTML
                prevr.appendChild(mycurrent_cell);
                mycurrent_cell.innerHTML = z;
                document.getElementById('txtpert').value = z
             }
        }
        ed = r.cells[1].innerHTML
        alert(ed)
        if (i==myRow-1)
    {
            prevr = document.all.finalMarks.rows[i];
            //mycurrent_cell = document.createElement("td");
            x = (om/mm)*100
            mycurrent_cell.innerHTML = x
            qualary[qualary.length] = prevr.cells[1].innerHTML
            pctary[pctary.length] = mycurrent_cell.innerHTML
            prevr.appendChild(mycurrent_cell);
            if(!isNaN(x))
            document.getElementById('txtperct').value = x
        }
    }
    //document.frmMarks.txtper.value = Math.max( x, z)
    var str = sortAndReturn(pctary,qualary)
    //alert(pctary  +   '$'   + qualary)    //alert(sortAndReturn(pctary,qualary))
    //alert(str)    //alert(str[0]+','+str[1])
     document.getElementById('degr').value = str.split('$')[0]
     document.getElementById('txtmarks').value = str.split('$')[1]
}
function sortAndReturn(pctary,qualary)
{
    var myArray3 = new Array();
    for(i=0;i<pctary.length;i++)
    myArray3[i]=pctary[i];
    sort(pctary)
    //alert(sort(pctary))    //alert(myArray3)
    return qualary[indexSearch(myArray3,pctary[0])] + '$' + pctary[0]
}
function sort(a)
{
    var  max;
    for(i=0;i<a.length;i++)
    {
        max   =  i;
        for(j=i+1;j<a.length;j++)
    {
            if (a[j]>a[max])
            {
        max=j;
            }
    }
    //alert(a[max])
        if (i != max)
        {
            var swap = a[i];
            a[i] = a[max];
            a[max] = swap;
        }
    }

}
function indexSearch(a,b)
{
    for(i=0;i<a.length;i++)
    if(a[i]==b)
    {return i ;}
}


HTML CODE:

XML
<table BORDER ="6"  width="85%"align="center" bgcolor="#00FFFF"  style="font-family: Verdana;color: black;font-size: 12px">
                <tr>
                    <td align="center" width="25%" height="40"> Qualification:</td>
                    <td align="left" style="WIDTH:25%" >
                    <select id ="txtqualify" style ="width:150px;height:25px" name ="txtqualify"  > </select> </td>
                    <td align="center" width="25%" height="40">Semester:</td>
                    <td> <input type = "text" SIZE="8" maxlength = "3" id = "txtsem" name = "txtsem"> </td>
                </tr>
                <tr>
                    <td align= "center" width ="25%" height="40">Max.Marks:</td>
                    <td> <input type = "text" SIZE="8" maxlength = "3" id = "txtmaxmrk" name = "txtmaxmrk"> </td>
                    <td align= "center" width ="25%" height="40">Obtained.Marks:</td>
                    <td> <input type = "text" SIZE="8" maxlength = "3" id = "txtobtmrk" name = "txtobtmrk"> </td>
                </tr>
                <tr>
                    <td align= "center" width ="25%" height="40">Percentage:</td>
                    <td> <input type = "text" SIZE="8" maxlength = "5" id = "txtper" readonly name = "txtper">
                    <td align="center" colspan="4" valign="middle">
                        <input type="RESET" align="middle" style="width:20%" value="ADD"  onclick="insRows()" >
                         <input type="button" align="middle" style="width:20%" value="REMOVE" onclick="delRow()" >
                         <input type="button" align="middle" style="width:20%" value="CLOSE" onclick="" >
                         <input type="button" align="middle" style="width:20%" value="SUM" onclick="function1()" >
                    </td>
                    </tr>
                <tr>
                    <td  align="center" height="40"> <input type = "text" SIZE="8" maxlength = "5" id = "txtpert" name = "txtpert"> </td>
                    <td align="center"> <input type = "text" SIZE="8" maxlength = "5" id = "txtperct" name = "txtperct"> </td>
                    <td align="center"> <input type = "text" SIZE="10" maxlength = "5" readonly id = "degr" name = "degr"> </td>
                </tr>
            </table>
            <table id ="finalMarks" BORDER ="6" width="85%"align="center" bgcolor="#00FFFF"  style="font-family: Verdana;color: black;font-size: 12px">
                <thead>
                    <th height="40">Remove</th>
                    <th>Qualification</th>
                    <th>Sem/Year</th>
                    <th>Max.Marks</th>
                    <th>Obt.Marks</th>
                    <th>Percentage of Marks:</th>
                </thead>
            </table>


===============

From JSOP: This looks like homework. How can I tell? Because you're requirements are absurd. Nobody in their right mind would purposely avoid using features of the chosen language/framework unless there was a widely known and un-patched problem with said feature. Since there's nothing wrong with the built in Math.Max method, this is homework.

Next, you don't need to post the HTML code (and probably 90% of the C# code you posted) to ask this question. It's title "Quick Answers" for a reason.

Lastly, THINK about the problem, and try to come up with as many ways to address the issue as you can, and then pick the one YOU think is the best. Your instructor will be happy to ridicule your choice.
Posted
Updated 31-Mar-10 23:40pm
v4

1 solution

Hi there,

here is an easy way:

Dim ac() as Integer = {19, 125, 43, 200, 11, 2, 84}
Dim MAX as Integer = 0
Dim currentNo as Integer

for currentNo = 0 to ac.Length
If MAX < ac(currentNo) Then
MAX = ac(currentNo)
End If
next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900