Jump to content

Array

- - - - -

  • Please log in to reply
9 replies to this topic

#1
andrywak

andrywak

    Newbie

  • Members
  • Pip
  • 5 posts
The raja gave out 1 grain of rice on the first day and doubled every day for 30 days. Well, we are going to change the story a bit.
 Your program will ask the user how many grains of rice they get on the first day.
o Note: rice will double every day just like before.
 Your program will then ask the user for how many days they get rice.
 Finally, your program will build an array and print the array to show the amount of rice they get on each day.
I am trying to do this project and all I can come up with is this code

import java.util.Scanner;

class myown{

	public static void main(String args[]){

	Scanner Andrew = new Scanner(System.in);

	int rice, days, answer;

	System.out.println("Enter how many pieces of rice you begin with: ");

	rice = Andrew.nextInt();

	System.out.println("Enter how many days you will receive rice for: ");

	days = Andrew.nextInt();

	answer = 2^(days-1) ;

	System.out.println(answer);

	}

}


how am i supposed to do this with an array?
thanks in advance.

#2
nicckk

nicckk

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 629 posts
You can create an array of integers with length days like this:


int[] rice = new int[days];


You can add data to your array by calling the index you want to append the value to. Remember that array index values start at 0. So, to add the int 1 to location 0 the code is


rice[0] = 1;


You will need a loop to fill the array. I don't have time to test the algorithm but I think it should look something like this.


for(int i = 0; i < rice.length; i++)

{

     rice[i] = Math.pow(rice,2);

     rice = Math.pow(rice,2);

}


To print the array you will need a loop as well


for(int k = 0; k < rice.length; k++)

{

    System.out.print(rice[i] + " ");

}



#3
andrywak

andrywak

    Newbie

  • Members
  • Pip
  • 5 posts
Thanks.. but where exactly will this go into all the code?

#4
Simonxz

Simonxz

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
You can put it in your main method

#5
andrywak

andrywak

    Newbie

  • Members
  • Pip
  • 5 posts
ermmm.. we only started working with java in the beginning of march.. can you please show how the whole code would look together?
cheers

#6
Simonxz

Simonxz

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
It's hard to show you what it would look like without doing your entire homework. Your logic should look something like this.

*Make a class, 

*Make a main method, 

*Ask for input on how many days you want and create an array of that length (int[] rice = new int[scanner.nextInt()]),

*Loop to fill the array,

*Loop to print the array,

*Close your main method,

*Close your class



#7
andrywak

andrywak

    Newbie

  • Members
  • Pip
  • 5 posts
I had to change some stuff because my instructor did not want me to use a scanner (I dont exactly get why)

import java.io.*;

public class Rywak_Rice //This is a Rywak production. Given a number of rice grains to begin with, this program will double it every day for X amount of days.

{

	public static void main(String[] args)

	{

		BufferedReader Rywak = new BufferedReader(new InputStreamReader(System.in));

		String input;


		try{ 

			System.out.println("Input the number of grains of rice you start with.");

			input = Rywak.readLine();

			int grains = Integer.parseInt(input);


			System.out.println("Input the number of days.");

			input = Rywak.readLine();

			int days = Integer.parseInt(input);

			int[] myArray = new int[days];

			myArray[0] = grains;

			for (int i = 1; i < days; i++) 

			{

				myArray[i] = myArray[i - 1] * 2; //This is what is doublin' the grain

			}

			for (int i = 0; i < myArray.length; i++)

			{

				System.out.println("Day " + (i + 1) + " " + myArray[i]);

			}

		}

		catch (IOException ex){ //This is to compensate for stupid people, i.e. "idiot-proofing."

			System.out.println("Error");

			System.exit(1);

		}

	}

}


However, when I run it, it seems to give negative numbers and after a certain point just runs zeroes. any ideas?

#8
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
It's ok. Since ints are just 32 bit long, they can hold values up until 2^32-1. Try with a long to have a major range.
And if you wanna be cool you gotta use the shift (<< operator) and JOPtionPane !
And you don't even need an array, if you don't need to use the values later.

import javax.swing.JOptionPane;


public class MainAsd

{

	public static void main(String[] args)

	{

		String input;

		long days=0, grains=0;

		

		input = JOptionPane.showInputDialog("Input the number of grains of rice you start with.");

		grains = Long.parseLong(input);


		input = JOptionPane.showInputDialog("Input the number of days.");

		days = Long.parseLong(input);

		

		for (int i = 0; i < days; i++) {

			System.out.println("Day " + (i + 1) + " " + (grains << i));

		}

		

	}

}

One thing: try to put the least amount of code possible inside the try/catch

#9
andrywak

andrywak

    Newbie

  • Members
  • Pip
  • 5 posts
I gotta use an array cause thats what my instructor said.. >.<

#10
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
It's not difficult to add an array to my code is it :D




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users