?
Solved

Find position of largest number in an array

Posted on 2007-10-11
7
Medium Priority
?
4,269 Views
Last Modified: 2012-05-05
A dice is being thrown many times and the occurrences of each digit are recorded. My output should produce 5 since digit5 appeared 99 times.

#include<iostream>
using namespace std;

int main()
{
      int maxlength = 6;
      int arrayDice[]={55,2,18,70,99,88};
      int largest = arrayDice[0];
      int count = 0;
      
      for (int i=0; i<maxlength; i++)
      {
            if (largest < arrayDice[i+1])
            {
                  largest = arrayDice[i+1];
                  count++;
                  
            }
      
      }
      cout << "The largest occurrence is digit " << count;
      
}
0
Comment
Question by:dandeliondream
7 Comments
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20062260
Few corrections must be made in your for loop:


// Since you assingned initial count value to 0 (number 1 on dice), and initial largest value to arrayDice[0], you can start searching largest number from 1st - index (number 2 on dice)

      for (int i=1; i<maxlength; i++)
      {

            // if number of occurrences of number i+1 on dice (=arrayDice[i]) is greater than current largest value, we update largest value and set count on i+1.

            if (largest < arrayDice[i])
            {
                  largest = arrayDice[i];
                  count = i + 1;
                 
            }
     
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20062277
Sorry, I've omited closing bracket of for loop:

// Since you assingned initial count value to 0 (number 1 on dice), and initial largest value to arrayDice[0], you can start searching largest number from 1st - index (number 2 on dice)
      for (int i=1; i<maxlength; i++)
      {

            // if number of occurrences of number i+1 on dice (=arrayDice[i]) is greater than current largest value, we update largest value and set count on i+1.

            if (largest < arrayDice[i])
            {
                  largest = arrayDice[i];
                  count = i + 1;
                 
            }
      }
0
 
LVL 3

Author Comment

by:dandeliondream
ID: 20063364
Hi UrosVidojevic,
I have amended my code as instructed but i cannot get 5 as my answer. I keep getting 6. Please advise.

#include <iostream>
using namespace std;

int main()
{
      int maxlength = 6;
      int arrayDice[]={55,2,18,70,99,88};
      int largest = arrayDice[0];
      int count = 0;
     
      for (int i=1; i<maxlength; i++)
      {
            if (largest < arrayDice[i+1])
            {
                  largest = arrayDice[i+1];
                  count = i+1;
                 
            }
     
      }
      cout << "The largest occurrence is digit " << count;
     
}

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 200 total points
ID: 20063568
>> I keep getting 6.

That's because there are too many increments. Get rid of the one in arrayDice[i+1], ie. make that arrayDice[i]
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20063573
And btw, main needs to return an int ...
0
 
LVL 4

Accepted Solution

by:
bdunz19 earned 480 total points
ID: 20063853
How about this:

int main()
{
      int maxlength = 6;
      int arrayDice[]={55,2,18,70,99,88};
      int largest = arrayDice[0];
      int count = 1;
     
      for (int i=0; i<maxlength; i++)
      {
            if (largest < arrayDice[i])
            {
                  largest = arrayDice[i];
                  count = i+1;
            }
      }
      cout << "The largest occurrence is digit " << count;
}
0
 
LVL 7

Assisted Solution

by:UrosVidojevic
UrosVidojevic earned 320 total points
ID: 20064020
"Hi UrosVidojevic,
I have amended my code as instructed but i cannot get 5 as my answer. I keep getting 6. Please advise."

Thats because you made few mistakes when modifying your code (in if statement).
Look carefully at code I gave you before, you changed arrayDice[i] into arrayDice[i+1] in two places.

if statement code should look like this:

            if (largest < arrayDice[i])
            {
                  largest = arrayDice[i];
                  count = i + 1;
                 
            }

Hope this helps.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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

862 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