Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Practice Questions for Test 2 1) Write the code that will read character input f

ID: 3865431 • Letter: P

Question

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.

Explanation / Answer

1.
count = 0;
   do {
      scanf("%c", &ch);
      if (ch != ' ' && ch != ' ')
         count++;
   } while (ch!=' ');
   printf(" Count = %d ",count);

2.
   double sum = 0;
   double prices[50];
   for (int i = 0; i<50; i++){
       scanf("%f", prices[i]);
       sum = sum + prices[i];
   }
   printf("%f", sum/50);

3.
int max = 0;
int num;
do {
     scanf("%d",&num);
     if (max < num){
        max = num;
     }
} while (num >= 0)
printf("%d",max);

4.
   do {
      scanf("%d",&num)
   } while (num < 1 && num > 15);   // This will prompt the user till he/she enters valid value

5.

    Scanner input = new Scanner(System.in);   
    double total = 0;
    double widgetsThisHour;

    int count = 0;
    while (count < 8)
    {
        System.out.print( “Enter the number of widgets produced this hour: “);
        widgetsThisHour = input.nextInt( );
        total = total + widgetsThisHour;
        count++;
   }
   System.out.println ( “The average hourly production of widgets is “ + total/count);

6. char reponse[25000][10];

   int count = 0; // will show how many people answered 'a' for last question
   for (int i=0; i<25000; i++)
       if (reponse[i][9] == 'a')
          count++;
  

7.
   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( );

   found = false;

   int index = 0;
   for (int i = 0; i<100 && !found; i++){
       if (IdToLookFor == ids[i]){
          found = true;
          index = i;
       }
   }
   if (found)
      System.out.println(index);
   else
      System.out.println("Not found");

8.

   for (int i=0; i<1000; i++)
       if (huge_tic_tac_toe[0][i] != 'X')
          break;
   if (i == 1000)
       cout << "X wins";
   else
       cout << "X is not winner";

9. for (int i=0; i<12; i++)
        difference[i] = highTemperature[i] - lowTemperature[i]

10.
     count = 0;
     for (int i = 0; i<25000; i++){
          for (j=0; j<6; j++)
              if (data[i][j] > 30)
                  count++;
      }

11.

     double temp[12][4];

     //Reading input
     for (int i=0; i<12; i++)
         for (j=0; j<4; j++)
             cin >> temp[i][j];


    // Max for the first year
    int max = 0;
    for (int i=0; i<12; i++)
       if (temp[i][0] > max)
          max = temp[i][0];        
    cout << max;

    // Average for all 4 years

    for (int i=0; i<4; i++){
        sum = 0;
        for (int j = 0; j<12; j++)
            sum = sum + temp[j][i];
        cout << "Average for " << i << sum/12;       
    }

   //Max tempearture for june accross 4 years
    int max = 0;

    for (int i = 0; i<4; i++)
       if (temp[5][i] > max)
          max = temp[5][i];

   cout << max;

12.
    int passwd;
    count = 0;
    do {
        cin >> passwd;
        count++;
    } while (passwd != PASSCODE && count < 3)

    if (count == 3 && passwd != PASSCODE)
       cout << "Attempts exceed three, see your administrator”;

13.
   248

   Binary : 1111 1000
   Octal :   370
   Hexadecimal: F8

14.
    int age[24600];
   
    for (int i=16; i<101; i++){
        count = 0;
        for(j = 0; j<24600; j++)
           if (i == age[j])
              count++;
        cout << "Frequency of age " << i << "is " << count;
    }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote