Howdy, Stranger!

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

Categories

how do you

scuba22scuba22 Member Posts: 61
find and output the largest index value of a partucular array?
do i need to list them all in a greater than sort of "if"?
Michele
«1

Comments

  • stoberstober Member Posts: 9,765 ✭✭✭
    : find and output the largest index value of a partucular array?
    : do i need to list them all in a greater than sort of "if"?
    : Michele
    :

    Loop through the array and use another variable to keep track of the largest value.
  • scuba22scuba22 Member Posts: 61
    : :
    I'm trying like this:
    [code}

    for (x=0; x < SIZE; x++){
    infile >> arr[x];
    largest = arr[x];
    for (int z=1; z,20; z++)
    if(arr[z]>largest)
    largest = arr[z];


    outfile<<setw(40) << x <<setw(6)<< arr[z] << endl;}

    }

    [/code]
    and getting an error that largest is an undeclared identifier.
    Hey stober, thanks for the help. Much appreciated
    M

    find and output the largest index value of a partucular array?
    : : do i need to list them all in a greater than sort of "if"?
    : : Michele
    : :
    :
    : Loop through the array and use another variable to keep track of the largest value.
    :

  • scuba22scuba22 Member Posts: 61
    : : :
    meant z<20
    still getting the same error


    : I'm trying like this:
    : [code}
    :
    : for (x=0; x < SIZE; x++){
    : infile >> arr[x];
    : largest = arr[x];
    : for (int z=1; z,20; z++)
    : if(arr[z]>largest)
    : largest = arr[z];
    :
    :
    : outfile<<setw(40) << x <<setw(6)<< arr[z] << endl;}
    :
    : }
    :
    : [/code]
    : and getting an error that largest is an undeclared identifier.
    : Hey stober, thanks for the help. Much appreciated
    : M
    :
    : find and output the largest index value of a partucular array?
    : : : do i need to list them all in a greater than sort of "if"?
    : : : Michele
    : : :
    : :
    : : Loop through the array and use another variable to keep track of the largest value.
    : :
    :
    :

  • scuba22scuba22 Member Posts: 61
    [b][red]This message was edited by scuba22 at 2002-10-27 17:55:43[/red][/b][hr]
    : : : :
    i am getting the largest index value with L (below) but the Location coming out is 3 and it should be 14...do you see a bug?
    Mish
    [code]
    int L=0;
    for (x=0; x < SIZE; x++)
    infile >> arr[x];
    L = arr[0];
    for (int z=1; zL)
    L = arr[z];
    outfile<<setw(40) << arr[z] <<setw(6)<< L << endl;


    [/code]
  • stoberstober Member Posts: 9,765 ✭✭✭
    You're close
    [code]
    int foo(int arr[])
    {
    int L = -10000;
    for (int x=0; x < SIZE; x++)
    {
    infile >> arr[x];
    if(arr[x] > L)
    L = arr[x];
    outfile<<setw(40) << arr[z] <<setw(6)<< L << endl;
    }
    return 0;
    }
    [/code]

  • scuba22scuba22 Member Posts: 61
    :
    i'm not following you - in your code there is no z incremental loop.
    so how can i express arr[z].
    i know i am sooo close!





    You're close
    : [code]
    : int foo(int arr[])
    : {
    : int L = -10000;
    : for (int x=0; x < SIZE; x++)
    : {
    : infile >> arr[x];
    : if(arr[x] > L)
    : L = arr[x];
    : outfile<<setw(40) << arr[z] <<setw(6)<< L << endl;
    : }
    : return 0;
    : }
    : [/code]
    :
    :

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : :
    : i'm not following you - in your code there is no z incremental loop.
    : so how can i express arr[z].
    : i know i am sooo close!
    :
    :
    :
    :
    :
    : You're close
    : : [code]
    : : int foo(int arr[])
    : : {
    : : int L = -10000;
    : : [red][b]int ndx=0;[/red][/b]
    : : for (int x=0; x < SIZE; x++)
    : : {
    : : infile >> arr[x];
    : : if(arr[x] > L) [red][b]{
    : : L = arr[x];
    : : ndx=x; // ALSO tag the slot index!
    : : }[/red][/b]
    : : outfile<<setw(40) << arr[z] <<setw(6)<< L << endl;
    : : }
    : : return 0;
    : : }
    : : [/code]
    : :
    : :
    :
    :
    [blue]Now [b]ndx[/b] will have the array index of the larges value.[/blue]
  • scuba22scuba22 Member Posts: 61
    THANKS ALOT!!





    i'm not following you - in your code there is no z incremental loop.
    : : so how can i express arr[z].
    : : i know i am sooo close!
    : :
    : :
    : :
    : :
    : :
    : : You're close
    : : : [code]
    : : : int foo(int arr[])
    : : : {
    : : : int L = -10000;
    : : : [red][b]int ndx=0;[/red][/b]
    : : : for (int x=0; x < SIZE; x++)
    : : : {
    : : : infile >> arr[x];
    : : : if(arr[x] > L) [red][b]{
    : : : L = arr[x];
    : : : ndx=x; // ALSO tag the slot index!
    : : : }[/red][/b]
    : : : outfile<<setw(40) << arr[z] <<setw(6)<< L << endl;
    : : : }
    : : : return 0;
    : : : }
    : : : [/code]
    : : :
    : : :
    : :
    : :
    : [blue]Now [b]ndx[/b] will have the array index of the larges value.[/blue]
    :

  • stoberstober Member Posts: 9,765 ✭✭✭
    Another loop is not necessary. Run that function in your compiler and see how it works.
  • scuba22scuba22 Member Posts: 61
    : thanks stober....
    I did something really similar and it's working well:









    Another loop is not necessary. Run that function in your compiler and see how it works.
    :

«1
Sign In or Register to comment.