Solved

Array problem

Posted on 2006-07-18
6
442 Views
Last Modified: 2008-02-01
I am trying to write a function that displays the index of the highest value in the array

The teach gave us  main function and we are just supposed to write the function that finds the index for the highest number

here is the code

#include <iostream>
#include <iomanip>
using namespace std;

//====================================================================
// put your function prototype here
//====================================================================

double FindIndexHighest (double p[], int size);


int main()
{//DO NOT CHANGE ANYTHING IN THE MAIN()
    double x[] = { 11.11, 66.66, 88.88, 33.33, 55.55 };
    double y[] = { 9, 6, 5, 8, 3, 4, 7, 4, 6, 3, 8, 5, 7, 2 };
    double z[] = { 123, 400, 765, 102, 345, 678, 234, 789 };

    cout << fixed <<showpoint << setprecision(2);

    int index = FindIndexHighest (x, sizeof (x) / sizeof (x[0]));
    cout << "Array x:  element index = " << index
         << "  element contents = " << x[index] << '\n';
    index = FindIndexHighest (y, sizeof (y) / sizeof (y[0]));
    cout << "Array y:  element index = " << index
         << "  element contents = " << y[index] << '\n';
    /*cout << "Array z:  element index = "
         << FindIndexHighest (z, sizeof (z) / sizeof (z[0]))
         << "  element contents = "
             << z[FindIndexHighest (z, sizeof (z) / sizeof (z[0])) ] << '\n';*/
    return 0;
}

//====================================================================
// put your function definition here.
//====================================================================

double FindIndexHighest (double p[], int size){
      double highest = 0;
      int index;
      for(double i=0; i<size; i++){
            if (p[i]>highest){
                  highest = p[i];
                  index = i;
            }
      }
      return index;
}


when I try to run this I get the erro message
 error C2108: subscript is not of integral type
in referance to my if statement in my occurances function definition.
i think the program show work otherwise but I have no idea what this error message means.
0
Comment
Question by:vlahomt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 

Author Comment

by:vlahomt
ID: 17133818
I commented out the array z section of main because I was getting the same erro message there when my code was not correct now that I think my function should work i just need know what that error message means so i can fix the problem which should probally fix the problem in the array z cout statments.
0
 
LVL 14

Expert Comment

by:wayside
ID: 17134053
   for(double i=0; i<size; i++){
          if (p[i]>highest){   <---------here is your problem
               highest = p[i];
               index = i;
 

array indices must be ints, you declared i as a double:

    for(int i=0; i<size; i++){   <----- change to int
          if (p[i]>highest){
               highest = p[i];
               index = i;
 
etc.
0
 
LVL 14

Expert Comment

by:wayside
ID: 17134076
The reason is, a double as an index makes no sense. What would

p[1.543276]

mean, if it could be a double?

So the compiler helpfully flags the error for you.



0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vlahomt
ID: 17134263
that fixes the function but I get the same error message if I un comment the cout statement reffering to the array z
0
 
LVL 15

Accepted Solution

by:
efn earned 300 total points
ID: 17134340
The reason is the same.  FindIndexHighest returns a double and the output statement uses it as a subscript.
0
 
LVL 15

Expert Comment

by:efn
ID: 17135346
Thanks, but with all due respect, I think wayside deserves most or all of the points here.  If you would like to change your decision, you can post a request in the Community Support topic area to get a moderator to undo your acceptance.
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

617 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