3 Replies - 662 Views - Last Post: 18 February 2013 - 10:49 PM Rate Topic: -----

#1 bulldogs2016  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-February 13

How to print out a specific subscript number?

Posted 17 February 2013 - 05:54 PM

Okay. I have been searching around this for hours. In my code I need to print out the subscript number of an array.
Example code I found in the book:
[
int main {

 const int size = 3;
 double arr[size] = {7.8,9.0,6.3}
double largest = arr[0]
for(int i =1; i<size; i++){
if(arr[i] > largest){
largest = arr[i];
}
}
cout << largest << endl;

   return 0;
}
]

Now that prints out "8". How can I the print out of "arr[1]" ?

This post has been edited by jimblumberg: 17 February 2013 - 06:22 PM
Reason for edit:: Added missing code tags. Please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: How to print out a specific subscript number?

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2990
  • Posts: 10,332
  • Joined: 08-August 08

Re: How to print out a specific subscript number?

Posted 17 February 2013 - 06:01 PM

largest = i;

and then
cout << arr[largest] << endl;

Was This Post Helpful? 2
  • +
  • -

#3 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: How to print out a specific subscript number?

Posted 18 February 2013 - 08:44 PM

Change i=1; to i=0; in the loop. Also you need a ; after 6.3 in the initialization list for array arr[3] and another one at end of line 5,

This post has been edited by buffalobill: 18 February 2013 - 08:45 PM

Was This Post Helpful? 0
  • +
  • -

#4 bulldogs2016  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-February 13

Re: How to print out a specific subscript number?

Posted 18 February 2013 - 10:49 PM

I = 1 because largest already equals arr[0] so there is no point in having the for loop check for arr[0]. This is just an example code that I got from the book. Not code that I am actually using for my project. Any errors were a quick typing mistake.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1