Solved

print index of a number in an array

Posted on 2003-11-01
15
242 Views
Last Modified: 2010-04-15
I'm going back to school in Jan and trying to brush up my skills the problem is find the maxium value in an array and print its corresponing index for the life of me I can't remember how this is what i have so far
#include <stdio.h>

int max_array(int a[], int num_elements);
void print_array(int a[], int num_elements);

void main(void)
{
   int a[10] = {1, 2, 0, 0, 4, 5, 6, 9, 9, 17};
   int max;

   printf("\nArray:\n");
   print_array(a, 10);

   max = max_array(a, 10);
   printf("The max is %d\n", max);
   sleep(10);
}

int max_array(int a[], int num_elements)
{
   int i, max=-32000;
   for (i=0; i<num_elements; i++)
   {
       if (a[i]>max)
       {
          max=a[i];
       }
   }
   return(max);
   
   
}

void print_array(int a[], int num_elements)
{
   int i;
   for(i=0; i<num_elements; i++)
   {
       printf("%d ", a[i]);
   }
   printf("\n");
   
}
0
Comment
Question by:Serena2345
  • 6
  • 4
  • 2
  • +2
15 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 9665075
what do u need?
if u need the index of the max number, then in the max_array function keep another variable for the index of the max value..and when u done browsing the array, instead of returning the max number return the index only.. and then in the main function u can print the index and appropriate number from the array..
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9665089
Try this,

int max_array( int a[], int num_elements )
{
    int i, max = 0;

    for( i = 1; i < num_elements; ++i )
    {
        if( a[i] > a[max] )
            max = i;
    }
    return max;  
}

Cheers!
Exceter
0
 
LVL 1

Accepted Solution

by:
guitardude101 earned 50 total points
ID: 9665437
if you want your funtion to work with negative as well as positive numbers init max to the first subscript.

Exceter:
Your loop should be
 for( i = 0; i < num_elements; ++i )

since mine inits to the first subscript I can start the loop at 1.


int max_array( int a[], int num_elements )
{
    int i, max = a[0];
    for( i = 1; i < num_elements; ++i )
    {
        if( a[i] > a[max] )
            max = i;
    }
    return max;  
}


0
 

Author Comment

by:Serena2345
ID: 9665917
will this also print out the index number?
0
 
LVL 1

Expert Comment

by:guitardude101
ID: 9666502
I just re read this and found a problem the function to return the max value should be

int max_array( int a[], int num_elements )
{
    int i, max = a[0];
    for( i = 1; i < num_elements; ++i )
    {
        if( a[i] > max )
            max = i;
    }
    return max;  
}


the function to return the index of max value should be

int max_array_index( int a[], int num_elements )
{
    int i, max = a[0],  maxindex = 0;
    for( i = 1; i < num_elements; ++i )
    {
        if( a[i] > max )
        {
            max = a[i]
            maxindex = i;
        }
    }
    return maxindex;  
}



0
 

Author Comment

by:Serena2345
ID: 9666594
you're the best!! thanks
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9666684
>> Exceter:
>> Your loop should be
>> for( i = 0; i < num_elements; ++i )

No it shouldn't be...

>> if you want your funtion to work with negative as well as positive numbers init max to the first subscript.

Did you even read the read the code I posted!? If you had tried to run my code you would have seen that is DOES, in actuality, work with negative numbers. For example,

int a[] = { -45, -327, -8, -23  };
printf("%i\n", max_array( a, 4 ));

-- output --
2

The function I posted stores the subscript of the value in max NOT the value itself... The asker wanted the index the value is found at NOT the value itself. Why the devil would you store the value in max? That makes no sense as you'd have to declare another variable, which is completely unnecessary.

>> if( a[i] > a[max] )

You can plainly see that I used max as a subscript NOT the value itself. I started the loop at 1 because I already set max to subscript 0. Therefore, if there is only one element in the array, I simply return subscript 0 as containing the highest value in the array. If not, the loop will then compare subscript i, which is intialized to 1, to subscript max, which initialized to 0. If it is greater than the value found at offset max then max is replaced with the counter value, which is 1. The process is then repeated until all elements have been processed and subscript max is returned.

In the future, please grant your fellow experts the courtesy of reading their comments before you attempt to critique them.
Exceter
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:Serena2345
ID: 9666699
I used it as a subscipt and I got the right answer I didn't try the other way yet
0
 

Author Comment

by:Serena2345
ID: 9666798
okay I ran both of them and the one that ends with maxindex =i gave the intended restult. The other one worked as long as the largest number in the array was in the last postion when I switched them around it still gave me 4 no matted what the value was. I want to thank everyone for their help.
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9667280
>> The other one worked as long as the largest number in the array was in the last postion when I switched them around it
>> still gave me 4 no matted what the value was.

To which function are you refering?
0
 

Author Comment

by:Serena2345
ID: 9667286
max_array_index
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9667316
>> max = a[i]

This is the only blatantly obvious error I found, it's just a typo, no big deal. It should have been,

max = a[i];

What data are you using as input? All issues of style aside, that function should be giving the correct value. Have you tried the function I posted?

Exceter
0
 

Author Comment

by:Serena2345
ID: 9667328
not yet I'll try it later tonight
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9880835
Sorry friends, this is slightly out of the question, but Akshay ol' buddy, pls post your views on:

http://www.experts-exchange.com/Programming/Q_20816080.html
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9880838
(others are requested too).
0

Featured Post

Save on storage to protect fatherhood memories

You're the dad who has everything. This Father's Day, make sure your family memories are protected. My Passport Ultra has automatic backup and password protection to keep your cherished photos and videos safe. With up to 3TB, you have plenty of room to hold the adventures ahead.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now