Write the code that will read character input from the user until a blank (a space) is entered

Write the code that will read character input from the user until a blank (a space) is entered

Practice Questions for Test 2

1) Write the code that will read character input from the user until a blank (a space) is entered. Print how many characters were entered. Keep in mind the user may decide to enter a blank as his first character.

2) Write the code that will allow the user to enter 50 prices. Then the code should calculate and print the average of these prices.

3) Write the code to prompt and read positive integer values from the user until he/she decides to quit by entering a non-poitive integer. The code should then print the maximum number the user entered.

4) Write the code to read and validate user input for an item at the Family Dollar store. The only valid prices are in the range of 1 to 15 (inclusive) [1,15]. (Use a do-while loop)

5) Convert this for loop code to a while loop code that does the same logic.

Scanner input = new Scanner(System.in);   

double total = 0;

double widgetsThisHour;

    for(int count = 0; count < 8; count ++)

    {

        System.out.print( “Enter the number of widgets produced this hour: “);

        widgetsThisHour = input.nextInt( );

        total = total + widgetsThisHour;

   }

System.out.println ( “The average hourly production of widgets is “ + total/count);

6) Suppose you took a survey from 25000 people consisting of 10 questions. Each question
has a possible answer of ‘a’, ‘b’ ,’c’ or ‘d’. Declare the two dimensional array to hold the   responses.

Now, assume the answers have already been read into the array (do not write the code to
input the responses). Write the code to count how many people answered ‘a’ to the last question on the survey. Declare all other needed variables.

7) Write the code that will search an array for a specified value. When the loop exits, print the subscript at which the item was found. Assume all the code to fill the array has been written and the code to ask the user which ID to look for has been written

Scanner input = new Scanner(System.in);

int [ ] ids = new int[100];

int idToLookFor;

bool found = false;

int indexItemFoundAt;

int i;

//the loop is already written to read values for the ids

System.out.print(“Enter the ID to search for: “);

IdToLookFor = input.nextInt( );

//write your for loop here to find the item in the array

8) You have been "recruited" by your friend to develop a "huge tic-tac-toe". The game is very similar to regular tic-tac-toe except for the fact that the board is 1000 x 1000 rather than 3 x 3. (to win you must have all of your character(X or O) in consectutive slots) (10 points)

Let ticTacToe be a char 2D array.

char ticTacToe[1000][1000];

Note that every element in that array contains one of the following three values:

'O'

'X'

'E' : for empty/available.

Your first task is to find out if 'X' can win in the first row. Note that there is no need for
writing code to get input from the user. Just write code to compute and print if 'X' has a possible win in the first row.

9) ) double highTemperature[12];

    double lowTemperature [12];

    double difference[12];

    Assume the code has been written to read in values for the two arrays, highTemperature and lowTemperature. Write the loop to fill the array, difference with the values obtained by
subtracting highTemperature[ ] – lowTemperature[ ] for each of the twelve elements in the
arrays.

10) Suppose you have a two-dimensional array of int to hold the ages of all of our

students for three years over two semesters. Assume we have 25,000 students for each of the
six semesters.   Write the code to count how many of these students are over 30 years old.

11) Declare an array to hold the high temperatures for each month for 4 years.

Write the code to read input for all the “slots” of this array.

Write the code to write find the highest temperature of the first year.

Write the code to find the average high temperature for all four years.

Write the code to find the highest temperature for June over the four years recorded.

12) A user gets a maximum of three attempts to enter a passcode correctly.

     After three wrong attempts the user sees the message- “Attempts exceed three, see your administrator”. Write the code to read and validate the passcode. Passcode is an integer value. Declare all needed variables.

final int PASSCODE = 32098943;

13) Convert this decimal number to its binary, octal and hexadecimal representations:                     
           248

         Binary:

         Octal:

         Hexadecimal:

14) Suppose you need to count how many students of each age are enrolled at
UNCC for Fall 2017. We have 24,600 students and the ages range from 16 to 100.

a) Declare an array to hold the age of each student
b) Assume the array has now been populated with data, declare an array to count
     the frequencies for each age.
c) Write the code to calculate the frequency of each age.
Powered by