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
jenyasd209 [6]
3 years ago
8

Suppose that a is a one-dimensional array of ints with a length of at least 2. Which of the following code fragments successfull

y exchange(s) the values of the first two elements of a?I a[ 0 ] = a[ 1 ]; a[ 1 ] = a[ 0 ];II int t = a[ 0 ]; a[ 0 ] = a[ 1 ]; a[ 1 ] = t;III a[ 0 ] = a[ 0 ] - a[ 1 ]; a[ 1 ] = a[ 0 ] + a[ 1 ]; a[ 0 ] = a[ 1 ] - a[ 0 ]; A) I only B) II only C) III only D) I and II only E) II and III only
Computers and Technology
1 answer:
Lyrx [107]3 years ago
6 0

Answer:

E) II and III only

<h2>Explanation of II</h2>

The chunk of code given in II uses an array a and a variable t which is called temporary variable and it holds the value of some other variable or an array element temporarily.

So the first line of the code int t=a[0] the temporary variable t holds the current value of the array a i.e the array element at first index of the array a[0] is copied to variable t.  This variable t is used to hold the original value of a[0] so that if the value of a[0] is overwritten later, it wont lose its original value.

a[0]=a[1] This line of code stores the value of a[1] in a[0]. This means that a[0] holds the array element at next index a[1] . This means the original value of a[0] is no longer the same because now it contains the value of a[1].  

The next line a[1]=t. This means that now a[1] holds the value which was stored in variable t. As the variable t was used to store the original value of a[0]. So that value is now stored in a[1].

Lets take an example: array a contains the two elements 1  at a[0] and 2 at a[1] position.

So according to the first statement

t=a[0] means that t holds value of array a at 0 index. We have 1 at 0th index of array so t=1

Next statement a[0]=a[1] means the value at a[1] is copied to a[0]

So at a[1] the value is 2. So this value 2 is stored in a[0] position in the array.

The last statement a[1]=t means now the value stored in t is copied to a[1]

As t stored the value of a[0] which was array element 1 so now this array element is copied to a[1]th position of the array. So a[1] holds 1 now.

This is how the elements 1 and 2 are swapped as now a[0] holds 2 and a[1] holds 1.

<h2>Explanation for III</h2>

Suppose that a[0]=1 and a[1]=2

The first statement a[ 0 ] = a[ 0 ] - a[ 1 ]; means that the value at a[0] is subtracted by the value at a[1]. That means 1-2 as 1 is the array element at a[0] and 2 is the array element at a[1] so 1-2=-1 This value is now stored at a[0] th position of array.  Now a[0]=-1 and a[1]=2

Next statement a[ 1 ] = a[ 0 ] + a[ 1 ];  means adding array element at a[0] and a[1] and the result of this addition is stored in a[1]. As a[0] is -1 and a[1] is 2 So a[0]+a[1]= -1+2=1 So a[1]=1 This means 1 will be placed at a[1] th position of array a. Now a[0]=-1 and a[1]=1

Note that a[1] now holds the value 1 which was the original value of a[0]  in start. This is because the first statement of code subtracts value a[1] from value of a[0] and then adds back value of a[1] to value of a[0] to keep the original value of a[0] safe for swapping just like in II the original value of a[0] is kept by temporary variable t.

The last line a[ 0 ] = a[ 1 ] - a[ 0 ]; means subtracting value at a[0] from value in a[1]. As we know that a[0]=-1 and a[1]=1 So a[0]=a[1]-a[0]=1+1=2 So a[0]=2.

Note that a[0] now holds the value 2 which was the original value of a[1]  in start. In the statement a[ 1 ] = a[ 0 ] + a[ 1 ] the value of a[0] is added to a[1] to get new value of a[1] and in statement a[ 0 ] = a[ 1 ] - a[ 0 ]; the value of a[0] is subtracted from that of a[1] in order to get previous original value of a[1]. So in the end a[0]=2 and a[1]=1 This is how the two value are swapped.

Explanation of I being false:

The line of code a[0] = a[1]; copies the value of a[1] to a[0]

This means the original value at a[0] is changed. This is called to overwrite the value. It is required to exchange values of elements in a[0] and a[1]. In order to swap two elements their original values should be retained but in this statement overwrites the original value in a[0]. So I is wrong.

 

You might be interested in
There Are Two Programs in this Question who will attempt first i will give brainliest ans reward and big points:-
Korolek [52]

Answer:

here you go ,could only do Question 2.try posting question 1 seperately maybe someone else can also try to help

Explanation:

Question 2.

#include <iostream>

using namespace std;

// class BankAccount

class BankAccount{

  

    // instance variables

    private:

        int accountID;

        int balance;

  

    public:

      

        // constructor

        BankAccount(int accountID, int balance){

            this->accountID = accountID;

            this->balance = balance;

        }

      

        // getters and setters

        void setAccoutnId(int accountID){

            this->accountID = accountID;

        }

      

        int getAccountId(){

            return accountID;

        }

      

        void setBalance(int balance){

            this->balance = balance;

        }

      

        int balanceInquiry(){

            return balance;

        }

};

class CurrentAccount : public BankAccount{

  

    public:

  

        // constructor

        CurrentAccount(int accountID, int balance):BankAccount(accountID,balance){

          

        }

      

        // function amount to withdraw

        void amountWithdrawn(int amount){

            setBalance(balanceInquiry()-amount);

        }

      

        // function to deposit amount

        void amountDeposit(int amount){

            setBalance(balanceInquiry()+amount);

        }

};

class SavingsAccount : public BankAccount{

  

    public:

      

        // constructor

        SavingsAccount(int accountID, int balance):BankAccount(accountID,balance){

          

        }

  

        // function amount to withdraw

        void amountWithdrawn(int amount){

            setBalance(balanceInquiry()-amount);

        }

      

         // function to deposit amount

        void amountDeposit(int amount){

            setBalance(balanceInquiry()+amount);

        }

      

      

};

int main()

{

    // calling function of Current Account

    cout<<"Current Account : "<<endl;

    CurrentAccount current(122,100000);

    current.amountWithdrawn(10000);

    cout<<"Your balance after withdraw : ";

    cout<<current.balanceInquiry()<<endl;

    current.amountDeposit(30000);

    cout<<"Your balance after deposit : ";

    cout<<current.balanceInquiry()<<endl;

    cout<<endl<<endl;

  

    // calling function of Savings Account

    cout<<"Savings Account : "<<endl;

    SavingsAccount saving(125,80000);

    saving.amountWithdrawn(5000);

    cout<<"Your balance after withdraw : ";

    cout<<saving.balanceInquiry()<<endl;

    saving.amountDeposit(20000);

    cout<<"Your balance after deposit : ";

    cout<<saving.balanceInquiry();

    return 0;

}

4 0
2 years ago
In attempts to improve their contribution to the environment, a company decided to adapt to green computing. Which of these tech
timama [110]

Answer:

All of them apply

Explanation:

Green computing is the process of using environmental friendly computers and its associated components, accessories and other supporting devices.

Virtualization helps us to reduce the number of hardware that we are using.

Grid computers helps us to reduce the use of number of machines and thus supporting the environment in a right way.

Recycling process. Any material that we use needs to be recycled effectively and also in an environmental friendly way.

Autonomic computing uses eco-friendly system which in turn hugely support green computing.

8 0
3 years ago
A _____ is a control that tells the computer what task to perform.
sammy [17]
I'm pretty sure the answer is code
7 0
3 years ago
Read the poem again and work in pairs or groups to do the following tasks.
kap26 [50]

Answer:

i have no idea what the answer is

Explanation:

8 0
3 years ago
Microwaves are used
Phoenix [80]
D) both a and b
Microwaves are used to heat foods in ovens. We all know that. They are also used for data and information transfer. Microwaves are used in wifi, gps, and radio astronomy. 
6 0
3 years ago
Other questions:
  • a problem exists when the current condition differs from a desired condition. This idea defines which step in problem-solving?
    13·2 answers
  • Advancements in technology that might be helpful for individuals who need accommodations to perform computer tasks include _____
    15·2 answers
  • Write a program that prompts the user for a measurement inmeters and then converts it into miles, feet, and inches.
    9·1 answer
  • A tower or mini tower pc is a type of all- in -one unit true or false
    9·2 answers
  • What is the range of possible sizes for side<br> x?<br> 4.0<br> 2.7
    8·1 answer
  • Wamna play mm2<br><br><br>imma send u my username​
    11·1 answer
  • State the keyboard key used to move the cursor to the line​
    10·1 answer
  • What is the computer generation​
    15·1 answer
  • Insertion point shows where the typed text will appear.
    5·1 answer
  • There are two main types of hard drive available to a computer. State what they are and describe their use.
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!