Jump to content

C++ Cannot print the index of an array.

Ikarmue
Spoiler

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <string>

using namespace std;

 

int main()
{


   int fmax[10] = {0};
   int counter = 1;
   int max = 0;
   int KILL = 1;
   
   for (counter = fmax[counter]; counter < 9; counter++)
   {
     cout << "\nEnter a number: ";
     cin >> fmax[counter];


     if (fmax[counter] > max)
     {
    

     max = fmax[counter];
    
     }

   }


   cout << "\nThe maximum value is: " << max << endl;
   cout << "This is element number " << fmax[counter] << " in the list of numbers." << endl;
   

return 0;
}

 

I am having trouble printing to the screen the index of the maximum number that is entered in the for loop. The index always come out as zero. What am I doing wrong, and what do I need to do to get it to display correctly?

Link to comment
Share on other sites

Link to post
Share on other sites

Are you trying to print the index of the largest number within your array?

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Reno2792 said:

Are you trying to print the index of the largest number within your array?

Yes.

Link to comment
Share on other sites

Link to post
Share on other sites

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <string>
using namespace std;
 
int main()
{

   int fmax[10] = {0}; // You initialize 10 elements arraay with zeros so it looks like [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
   int counter = 1; // You set counter to 1 but then ...
   int max = 0;
   int KILL = 1;
   
  
   //...     v here you set counter to counter-th element of fmax array so fmax[counter] = fmax[1] = 0 You probably want to set counter to 0
   for (counter = fmax[counter]; counter < 9; counter++) // then you iterate from 0 to 9 so only 9 elements of array are populated
   {
     cout << "\nEnter a number: ";
     cin >> fmax[counter];

     if (fmax[counter] > max)
     {
    
     max = fmax[counter];
    
     }
   }
  //after for loop your counter is 9 cause 9 is not < then 9 so loop breaks.

   cout << "\nThe maximum value is: " << max << endl; //You display max value
   cout << "This is element number " << fmax[counter] << " in the list of numbers." << endl;// and you display counter-th element of fmax as we now know, counter is no equal to 9 so the last element of array is displayed which haven't been set in for loop iterations.
  
  
   
return 0;
}

I made some comments in your code, cause there are more things then just index of array not appearing.

 

So you are not only not displaying an index, but you displaying element of array that hasn't been set after initialization (after initializing it by {0} to 0).

 

You were unlucky/lucky enough that you haven't got segmentation fault, because you set your loop wrong, so you actually access to the last element, which exist but wasn't set in loop, and not element out of bound.

Link to comment
Share on other sites

Link to post
Share on other sites

Spoiler

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <string>

using namespace std;

 

int main()
{


   int fmax[10] = {0};
   int counter;
   int max = 0;
   
   for (counter = 0; counter < 10; counter++)
   {
     cout << "\nEnter a number: ";
     cin >> fmax[counter];


     if (fmax[counter] > max)
     {
    

     max = fmax[counter];
    
     }

   }


   cout << "\nThe maximum value is: " << max << endl;
   cout << "This is element number " << fmax[counter] << " in the list of numbers." << endl;
   

return 0;
}

Made some changes. I had already tried this, but the thing is, when I try to get it to output the index number, it's just some really low negative number -837845834 or something like that.

 

I honestly cannot figure out how to output the index of the largest number here.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Ikarmue said:

 

It is because you don't even store indexes, you store max value, not where it is at in the array, your code looks like you don't know what you're actually doing and you're trying random things.

 

But the advice is to store index, not the value. I can give you a solution but I don't know if that is the point.

Link to comment
Share on other sites

Link to post
Share on other sites

19 minutes ago, Ikarmue said:
  Hide contents

-snip-

You're not storing the index. Create a new integer and in your if statement set the counter to the int. Keeping in mind that arrays start at 0, if you want the user entered position just add 1 to it.

 

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <string>
using namespace std;

int main()
{

   int fmax[10] = {0};
   int counter;
   int max = 0;
   int location;

   for (counter = 0; counter < 10; counter++)
   {
     cout << "\nEnter a number: ";
     cin >> fmax[counter];

     if (fmax[counter] > max)
     {

     max = fmax[counter];
     location = counter;
     }
   }

   cout << "\nThe maximum value is: " << max << endl;
   cout << "This is element number " << location << " in the list of numbers." << endl;

return 0;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks!

 

I knew I was missing something, but couldn't put my finger on it.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×