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
abruzzese [7]
3 years ago
11

We informally define the term corresponding element as follows: The first element in an array and the last element of the array

are corresponding elements. Similarly, the second element and the element just before the last element are corresponding elements. The third element and the element just before the element just before the last element are corresponding elements -- and so on. Given an array a, write an expression for the corresponding element of a[i].
Computers and Technology
1 answer:
yanalaym [24]3 years ago
7 0

The corresponding element in an array can be written as follows.

The given array is arr[len] whose size is given by integer variable len. We take another integer variable k.

Corresponding element for arr[k] = arr[len-1-k]

This applies for all expressions of k beginning from 0 upto half the size of len, i.e., len/2.

For element, arr[0], the corresponding element is arr[10-1-0] = arr[9].

The above expression is valid if the sum of index of both element and its corresponding element adds upto the index of the last element of the array.

Example: k + (len – 1 - k)  

= k + len – 1 - k

= len – 1

= index of the last element of the array

The number of elements that an array can hold is one more than the index of the last element of the array.

Suppose an array of 10 elements is given. The above expression holds true since first element of the array is always written as arr[0] and last element will be written as arr[9] for an array of 10 elements.

This is illustrated below.

The c++ program to display the element and its corresponding element along with their index is shown below.

#include <iostream>

using namespace std;

int main() {    

   int len=10, arr[len],k;    

   for(k=0; k<len; k++)

   {

       // elements are initialized to twice their index

       arr[k] = k*2;

   }

   cout<<"Element "<<" Index "<<"\t"<<" Corresponding "<<" Index"<<endl;

   for(k=0; k<len/2; k++)

   {

       cout<<arr[k]<<"\t"<<"\t"<<"\t"<<k<<"\t"<<"\t"<<"\t"<<arr[len-1-k]<<"\t"<<"\t"<<len-1-k<<endl;

   }

}

OUTPUT

Element Index    Corresponding   Index

0  0   18  9

2  1   16  8

4  2   14  7

6  3   12  6

8  4   10  5

 

The above program initializes all the elements of the array to twice the value of their corresponding index. Following this, the element, its index, the corresponding element and its index is displayed.

All the above is achieved using for loop. The size of the array and its elements can be changed.

The expression for the corresponding element is thus explained.

You might be interested in
Jin needs to add a row into his spreadsheet, but he does not want to remove any existing data. Which combination of options shou
disa [49]

Answer:

b. insert > entire row

5 0
4 years ago
Read 2 more answers
Write a method that returns a new array by eliminating the duplicate values in the array using the following method header: Publ
saw5 [17]

Answer:

Following are the program in java language

import java.util.*; // import package

class Main // main class

{

public static void main(String[] args) // main method

{

Scanner sc = new Scanner(System.in); // creating Scanner class object

int[] num= new int[10]; // declaring the array num

System.out.print("Enter 10 numbers:: ");

for (int i = 0; i < num.length; i++) // iterating over the loop

{

num[i] = sc.nextInt(); // taking 10 input integer.....

}

int[] newList1 = eliminateDuplicates(num);// calling the function eliminateDuplicates

System.out.println("The Distinct numbers are: ");

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

{

System.out.print(" " +newList1[i]); // display the new list

}

System.out.println();

}

public static int[] eliminateDuplicates(int[] list) // method definition as mention in question

{

int[] duplicate1 = new int[10]; // creating the array duplicate

int j = 0, I = 0; // variabe declaration

for (int i = 0; i < list.length; i++) // filling the duplicate with 0

{

duplicate1[i] = 0;

}

int count1 = 0; // variable declaration

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

{

boolean duplicate = false; // creating boolean variable

for(j = 0; j < count1; j++) // iterating over the loop

if(list[i] == duplicate1[j]) // checking the condition

{

duplicate = true; // assigning the duplicate variable to true

break;

}

if(!duplicate) //checking boolean variable duplicate

{

duplicate1[count1] = list[i];

count1++;

}

}

int [] newArray1 = new int[count1]; // creating a new array

for(int i = 0; i < count1; i++) // iterating over the loop

newArray1[i] = duplicate1[i]; // assign the element in the new array

return newArray1; // return array

}

}

Output:

Enter 10 numbers:::1

2

3

2

1

6

3

4

5

2

The Distinct numbers are:1 2 3 6 4 5

Explanation:

In this program we create a eliminateDuplicate method and passing array to them.In this function we create a duplicate array and assign 0 to them by iterating over  the loop then we check the required condition is duplicate or not by making comparison between the duplicate array and list array i.e if(list[i] == duplicate1[j]) . if matches found then it set duplicate = true; .Finally it created a new array and store the distinct element also return the new array element.  In the main method it printed the distinct elements .

7 0
3 years ago
Write a for loop to populate vector userGuesses with NUM_GUESSES integers. Read integers using cin. Ex: If NUM_GUESSES is 3 and
Tanzania [10]
#include <iostream>
#include <vector>

#define NUM_GUESSES 3

int main()
{
  std::vector<int> userGuesses;
  for (int i = 0, input; i < NUM_GUESSES; i++)
  {
    std::cin >> input;
    userGuesses.push_back(input);
  }
}
8 0
4 years ago
Read 2 more answers
QUICK 50 POINTS WILL GIVE BRAINLY!!!
Goshia [24]

Answer:

1. grab a bowl spoon and ice cream

2. take your spoon and put a desired amount of ice cream

3. add topping if needed

4. clean up and enjoy!!

Explanation:

6 0
3 years ago
Read 2 more answers
If i had apple and microsoft both on my computer and they both fight what would i do
riadik2000 [5.3K]

Answer:

ok

Explanation:

5 0
3 years ago
Read 2 more answers
Other questions:
  • Why do i have to wait and watch videos
    11·1 answer
  • In the code snippet, what is the “win” part called in programming?
    12·1 answer
  • Linda is searching for a new job. Which information would her potential employers likely review in her social profile?
    14·2 answers
  • WhAt is a jpeg do i look like i know what a jpeg is
    13·1 answer
  • Which of the following was (and still is) used by computer programmers as a first test program?
    13·2 answers
  • Manuel owns a small business. He needs to calculate the amount each employee owes in Federal taxes. Manuel should
    11·2 answers
  • 1 : What format would you apply to give an entry the appearance of 12-Mar-2014
    8·1 answer
  • As marketing manager, you need to have ( blank) skills and (blank) skills.
    11·1 answer
  • What menu and grouping commands is the "SORT" tool? ( please answering meeeee)
    9·1 answer
  • I found a brand-new charger wire still in its plastic package, but it's six years old and has never been used. Is it safe to use
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!