Array keeps counting 0 as a number when I didn'

why start counting at 0
why do arrays start at 0
what does zero indexing mean in a database
in a language with zero based indexing at what index will the 100th item in an array be
zeroth element
1 indexed languages
o indexed
zero based index c#

I'm trying to make a game show program where I enter the number of contestants, how fast they hit the buzzer, the fastest and slowest times for hitting the buzzer, and the number of people that hit the buzzer faster than average.

Everything goes well with my program when I have 5 contestants. But if I enter 4 contestants or anything else less than 5, it messes up some things. When I have 4 contestants, my fastest time is 0, even when I'm not putting in a 0 as a time, and the number of contestants that hit the buzzer faster than average is also messed up, having one extra than it should, because it's putting in a 0.

How do I stop this program from adding in a 0 when I don't have 5 contestants?

import java.util.Scanner;

public class FastestFingers {

    public static void main(String[] args) {

            //declare varialbes
        int contestants;
        int [] milisecs = new int [6];

        int fastest, slowest, i;
        int faster = 0;
        double average;

        Scanner scanInt = new Scanner (System.in);

        //make user enter number of contestants


                System.out.println ("Enter # of contestants: ");
                contestants = scanInt.nextInt();
        //make user enter times
        for(i=1;i<=contestants;i++)
            {
                System.out.println ("Time (ms): ");
                milisecs[i] = scanInt.nextInt();
            }
        //calculate fastest time
        fastest = milisecs[1];
                for(i=1;i<milisecs.length;i++)
                    {
                        if(milisecs[i] < fastest)
                            {
                                fastest = milisecs[i];
                            }
                    }
        System.out.println ("Fastest: " + fastest);
        //calculate slowest time
        slowest = milisecs[1];
                for(i=1;i<milisecs.length;i++)
                    {
                        if(milisecs[i] > slowest)
                            {
                                slowest = milisecs[i];
                            }
                    }
                    System.out.println ("Slowest: " + slowest);
        //tell program how to find average
        int total = 0;
        for(i=1;i<milisecs.length;i++)
            {
                total = total + milisecs[i];
            }
        average = total/contestants;
        //find numbers faster than the average
        int count = 0;
        for(i=1;i<milisecs.length;i++)
            {
                if(milisecs[i]<average)
                    {
                        count++;
                    }
            }
        System.out.println ("Faster than average: " + count);
    }
}

You create an int[] of fixed size 6. Then you compare the last 5 values to find the lowest. All int values are default 0. If you only set 4 values to anything larger than 0 the fifth value will always be 0 and therefore your lowest value.

You can fix this problem by setting the size of your array to the number of contestants.


Also remember that arrays start counting at 0 not at 1. Your loops should therefore be:

for(i=0;i<milisecs.length;i++), etc.

Why do we count starting from zero?, Since the number before one is zero, it must have an index of zero. An array is "indexed" from zero and "counted" from one. Make it simple, keep it cheap. Count occurrences of the average of array elements with a given number Find element in a sorted array whose frequency is greater than or equal to n/2. Count pairs in an array such that frequency of one is at least value of other

The problem is that you initialize the array to have six elements.

int [] milisecs = new int [6];

By default all of these values will be zero. Then in your loop you only initialize four of them, meaning that two will still be zero. Change your code to:

System.out.println ("Enter # of contestants: ");
contestants = scanInt.nextInt();
int [] milisecs = new int [contestants];

This will ensure that you will only have as many time slots as you have contestants.

Also array indexes start at zero, so you should change

fastest = milisecs[1];

to

fastest = milisecs[0];

And in your loops, start your variable at 0 instead of 1.

Arrays Start at Zero: What's up with that? - Noteworthy, While array indexing seems like an arbitrary choice, in a binary system And actually, if we think about it, we did have a 0 at the beginning of our number sequence, we If you count out 10 numbers in sequence starting from 0, the 10th The problem with allowing the index to change would be keeping  If you receive the input as, let's say, array of numbers, you can simply loop trough it using for or foreach and add additional condition to check for 0 if you want to preliminary exit: For Each number As Integer In numbers If (number mod 2 = 0) Then Count = Count + 1 End If If (number = 0) Then Exit For End If Next

Array Index should start with 0. Please be careful with this. You code reads input and assigns to the milisecs starting from index 1.

The Complete Guide to Loops in JavaScript, As a result, I found some things I didn't know existed. In this (some of the Array methods are similar to loops / iterators: Array.values(), For loops usually start counting from 0. Loops are often used to increment numbers. If we have an array which is defined as int x [100] = {1,0,3}; then we have an array of 100 (one hundred) elements. The leading three elements have initial values {1, 0, 3} and other 97 elements have values 0 (zero). Please, feel the difference between array element and array element value.

Uber Interview Questions - Move Zeroes, By keeping two arrays: one for non-zero numbers and one for all zeroes, Keep a counter count of the number of zeroes and traverse the array  Distance, count, start & end points all vary I can only make the panel width vary with some constant louvers spacing by having the formula for the number of Louvers to be the louvers width (panel width - 250mm )divide by the spacing. but in the case that the width cant be divide nicely. then the spacing will slightly differ from what i want.

Javanotes 5.1.2, Section 7.2 -- Programming With Arrays, double sum; // The sum of the numbers in A. sum = 0; // Start with 0. for (int i = 0; find the largest number in A. The strategy is to go through the array, keeping track Like several other examples from that chapter, the applet had a flaw: It didn't  I was just curious if there was a way to check the length of an array? I didn't see any direct answers to this on the forums and the Array function page on the website didn't say anything about checking the length either.

Facebook Interview Question: Given an array of ints = [6, , Given an array of ints=[6, 4, 0, 5, 0, 0, 0, 1, 0] move all non zero numbers to the left and zeros to the right. How can you now Just keep pointers to the first and last elements of a new array. I had my interview before but did not get the offer. Previous: Write a C++ program to find a number which occurs odd number of times of a given array of positive integers. In the said array all numbers occur even number of times. Next: Write a C++ program to find the two repeating elements in a given array of integers.

Comments
  • But wouldn't that affect the numbers when I have 5 players? Or 3? Or 2? Is there a way I can make it so I can have any number of contestants and still have the program work correctly?
  • What specifically? If you have an array with size == numberOfContestants you will not have any issues as long as the number of contestants is a valid integer value > 0.