1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Romashka [77]
3 years ago
7

Your colleague developed a function, which is supposed to reverse an array of integers, e.g., {1, 5, -10, 7, 23} => {23, 7, -

10, 5, 1}. Test cases show that the function does not work correctly. Please look at the following code and identify all the troubles. Please specify which part(s) of the code will not work and explain why not.
void ReverseArray(int arr[], size_t elements_no) {
size_t i = 0; int x = 0; for (i = 0; i <= elements_no; i++)
{ x = arr[i]; arr[i] = arr[elements_no - i]; arr[elements_no - i] = x; }

}
Computers and Technology
1 answer:
Aliun [14]3 years ago
8 0

Answer:

The parts of the code which makes the program not to work properly is:

1. for (i = 0; i <= elements_no; i++)

2. arr[i] = arr[elements_no - i];

3. arr[elements_no - i] = x;

The correction of the above lines are:

1. for (i = 0; i <= elements_no/2; i++)

2. arr[i] = arr[elements_no - i-1];

3. arr[elements_no - i-1] = x;

Explanation:

It should be noted that the code end up not reversing the array. This happens because the programmer iterates through all the array elements.

i.e.

1. for (i = 0; i <= elements_no; i++)

This will result in the original array.

To solve this problem, the iteration has to stop at the middle element and the correction is:

1. for (i = 0; i <= elements_no/2; i++)

Also, to swap the individual elements 1 has to be subtracted from each array index. So, the following lines:

2. arr[i] = arr[elements_no - i];

3. arr[elements_no - i] = x;

is corrected to:

2. arr[i] = arr[elements_no - i-1];

3. arr[elements_no - i-1] = x;

<em>See attachment for complete corrections</em>

Download txt
You might be interested in
Write a program to calculate the total salary of the employees in an office. The program will first prompt the user to enter the
raketka [301]

Answer:

import java.util.Scanner;

public class Salary

{

public static void main(String[] args) {

   

    int numberOfEmployees;

    double payRate, numberOfHours, totalPay = 0;

       Scanner input1 = new Scanner(System.in);

       Scanner input2 = new Scanner(System.in);

       System.out.print("Enter the number of employees: ");

       numberOfEmployees = input1.nextInt();

       

       for(int i=1; i<=numberOfEmployees; i++) {

           System.out.print("Enter the pay rate for employee " + i + ": ");

           payRate = input2.nextDouble();

           System.out.print("Enter the number of hours worked: ");

           numberOfHours = input2.nextDouble();

           

           totalPay += (payRate * numberOfHours);

       }

       

       System.out.println("Total payment is: "+ totalPay);

}

}

Explanation:

- Declare the variables

- Ask the user the for the number of employees

- Inside the for loop, ask the user for pay rate and hours worked for each employee

- Calculate the total pay

- <u>Outside of the loop</u>, print the total pay

6 0
3 years ago
Read 2 more answers
Which of the following is a preferable method to secure wireless access in a SOHO?
NARA [144]
Use a Ghost program follow throught with 2hyttlg5:6\:56
6 0
3 years ago
What are organization tools?
Nonamiya [84]
An app or software created to optimize your daily task performance
4 0
2 years ago
Howie is a business analyst who is given access to the database of a product prior to its launch. He needs to understand the dat
Kisachek [45]

Answer:

D) use SQL or graphical tools to query the database            

Explanation:

To query the database using SQL or graphical tools will help Howie to retrieve bulk of records from the database according to his requirements and it will help to understand and work with the data in a better way than using the data in raw form.

SQL is a language of database used to manipulate and manage the data in the relational database and to query the database.

It is an easy and faster way to query using SQL or graphical tools because no coding is required here.

Different graphical tools are used to present the data in the database in a way that can be easily understood by a person. Different interfaces are provided to visualize the database tables or schema diagrammatically e.g in the form of graphs etc. Different views of the database can be provided to different users according to their requirement and what information they required to be presented.

6 0
2 years ago
def transfer(bank, log_in, userA, userB, amount): ''' In this function, you will try to make a transfer between two user account
laiz [17]

Answer:

def transfer(bank, log_in, userA, userB, amount): ''' In this function, you will try to make a transfer between two user accounts. bank is a dictionary where the key is the username and the value is the user's account balance. log_in is a dictionary where the key is the username and the value is the user's log-in status. amount is the amount to be transferred between user accounts (userA and userB). amount is always positive. What you will do: - Deduct the given amount from userA and add it to userB, which makes a transfer. - You should consider some following cases: - userA must be in the bank and his/her log-in status in log_in must be True. - userB must be in log_in, regardless of log-in status. userB can be absent in the bank. - No user can have a negative amount in their account. He/she must have a positive or zero balance. Return True if a transfer is made. For example:

Explanation:

i know this much

8 0
2 years ago
Other questions:
  • Why is it unlikely that you will find the ip address 192.168.250.10 on the internet?
    15·1 answer
  • Is a procedure to copy one or more files from backup media to the original disk or a replacement disk when data or programs have
    8·1 answer
  • A smart refrigerator can use what to detect when you are running low on milk, and then send a reminder to you on a wireless.
    8·1 answer
  • Excel assigns the name ____ to the first excel table created in a workbook.
    10·1 answer
  • Which CSS attribute would change an element's font color to blue
    15·2 answers
  • A network administrator is configuring the triggering mechanism for the network-based IPS by defining a pattern of web surfing a
    10·2 answers
  • One of the best ways to shoot a picture is to frame the subject in the middle of the screen or viewer. T or F
    14·2 answers
  • Which type of image format is constructed using proportional formulas rather than pixels? This task contains the radio buttons a
    12·2 answers
  • Is this App for real?​
    5·1 answer
  • Merging refers to dividing a single cell into multiple cells. *TrueFalse
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!