Solved

Find position of largest number in an array

Posted on 2007-10-11
Medium Priority
4,269 Views
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
Question by:dandeliondream

LVL 7

Expert Comment

ID: 20062260

// 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

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

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

LVL 53

Assisted Solution

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

ID: 20063573
And btw, main needs to return an int ...
0

LVL 4

Accepted Solution

bdunz19 earned 480 total points
ID: 20063853

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

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

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
Course of the Month16 days, 15 hours left to enroll