CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2019
    Posts
    4

    Question When the length of an array is not specified, what do you do?

    I'm reading this book called "intro to java programming 10th edition" and in chapter 7 (arrays), exercise 7.3 and .4 (i stopped at .4 since it seems it's becoming the norm).

    The questions are asking to write an indefinite number of values, knowing this i use a while loop but when i use the while loop i need the array in the expression eg: while(array[i] < 0).

    now to have [i] amount i need to know how many are there but i don't!

    i look up the answers but all of them specify the length of the array EVEN THOUGH it did not say you can.

    Here's a question (7.3):

    /*
    (Count occurrence of numbers) Write a program that reads the integers between 1
    and 100 and counts the occurrences of each. Assume the input ends with 0.
    Note that if a number occurs more than one time, the plural word “times” is used
    in the output.
    */

    i just don't know how can i continue the code when i dont know when the user will stop, he could write 10, 100 or even 1000 values! I just don't know.

    The thing given to me is that when they enter 0, you stop the program.

    The given answer from the internet is:

    Code:
    import java.util.Scanner;
    
    public class Exercise_07_03 {
    	/** Main Method */
    	public static void main(String[] args) {
    		int[] counts = new int[100]; // Counts the occurrences of numbers
    
    		// Prompt the user to enter integers between 1 and 100
    		System.out.print("Enter the integers between 1 and 100: ");
    
    		// Count occurrence of numbers
    		count(counts);
    
    		// Display results
    		for (int i = 0; i < counts.length; i++) {
    			if (counts[i] > 0)
    				System.out.println((i + 1) + " occurs " + counts[i] +
    					" time" + (counts[i] > 1 ? "s" : ""));
    		}
    	}
    
    	/** Method count reads integers between 1 and 100 
    	*   and counts the occurrences of each */
    	public static void count(int[] counts){
    		Scanner input = new Scanner(System.in);
    		int num; // holds user input
    		do {
    			num = input.nextInt();
    			if (num >= 1 && num <= 100)	
    				counts[num - 1]++;
    		} while (num != 0);
    	}
    }
    why is there a int[] counts = new int[100]; when the amount of values isn't specified ? can you please explain this to me please?

    The book says exercise is from 7.2 to 7.5 but there is nothing related to that. i read it 3 times and i still don't have an answer.

  2. #2
    Join Date
    Sep 2019
    Posts
    4

    Re: When the length of an array is not specified, what do you do?

    P.S if there is better way in which i can understand the question and how to answer it, please tell me!

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: When the length of an array is not specified, what do you do?

    counts, as the name might suggest, is an array that holds the count for each integer. The spec says an input is an integer between 1 and 100. Therefore this entered number is the index into the array. See

    Code:
    num = input.nextInt();
    if (num >= 1 && num <= 100)	
    	counts[num - 1]++;
    where the counts array element corresponding to the entered number (less 1 as array index start at 0) is incremented.

    You don't need to know how many numbers are to be entered - as you don't store them. Just input one number, test for 0 to end and if valid then increment the relevant counts element.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Sep 2019
    Posts
    4

    Re: When the length of an array is not specified, what do you do?

    Quote Originally Posted by 2kaud View Post
    You don't need to know how many numbers are to be entered - as you don't store them.
    What do you mean we don't store them ? each value given by the user has a spot in the index ( what i know, upto my knowledge).

    so if the number of values given by the user is 100 and i assigned my array to be like this: int[] test = new int[10];

    it should be fine ? doesn't seem right. (or i just didn't understand what you meant).

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: When the length of an array is not specified, what do you do?

    each value given by the user has a spot in the index
    The count for each value is stored - not the number itself. The size of the array counts is 100 because each entered number can have a value of 1 to 100. If you allowed the value of numbers entered to be between 1 and 300, then you would need:

    Code:
    int[] counts = new int[300];
    Look again at the code highlighted in post #3. The number is input and the value of the number is used as the array index. So as long as the entered values are between 1 and 100, then it doesn't matter how many numbers are entered, the size of the array counts is still 100.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Sep 2019
    Posts
    4

    Re: When the length of an array is not specified, what do you do?

    It's still a bit confusing to me but i get what you are saying, i'll see what i can do when get another question with the same thing.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: When the length of an array is not specified, what do you do?

    Quote Originally Posted by universecloud View Post
    It's still a bit confusing to me but i get what you are saying, i'll see what i can do when get another question with the same thing.
    Learning to program and understanding a programming language can be confusing at first. If you have questions, just ask them. We'll do our best to answer.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Oct 2019
    Posts
    8

    Re: When the length of an array is not specified, what do you do?

    Try other language programming and see if it's compatible.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured