Question

In: Computer Science

Indexing Arrays & Scalar Operations Create a new array called “myShortArray” that contains the 1st, 3rd,...

Indexing Arrays & Scalar Operations

Create a new array called “myShortArray” that contains the 1st, 3rd, and 5th elements of the array “myArray” as defined below using the indexing method.

     myArray = [1, 2, 3, 4, 5, 6];

Solutions

Expert Solution

#include<iostream>
using namespace std;
int main()
{
    //DECLARE THE ARRAYS
    int myShortArray[3],myArray[6]={1,2,3,4,5,6};
    int i,j;
    //loop to scan each element of myArray
    for(i=0,j=0;i<6;i++)
    {
       if(i%2==0) //check the odd position elements. Since array atarts with 0 index so the index with even number will be the odd position element
       //if you need to store the odd index element then the condition will be
       //if(i%2!=0)
       {
           myShortArray[j]=myArray[i];//assign the myArray element to myShortArray
           j++;
           }
   }
   //display the elements of myArray
   cout<<endl<<"Elements of myArray : ";
   for(i=0;i<6;i++)
   cout<<" "<<myArray[i];
   //display the elements of myShortArray
   cout<<endl<<"Elements of myShortArray : ";
   for(i=0;i<3;i++)
   cout<<" "<<myShortArray[i];
  
}

output


Related Solutions

Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we...
Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we will call our visible field, needs to be 5 by 5 and should initially hold the underscore character “_”.  This will be used in our game of minesweeper to represent the possible locations the player can check. The second array, which we will call our hidden field, should also be 5 by 5 but filled with the capital letter "S” which means safety. However, we...
Create a new class called Account with a main method that contains the following: • A...
Create a new class called Account with a main method that contains the following: • A static variable called numAccounts, initialized to 0. • A constructor method that will add 1 to the numAccounts variable each time a new Account object is created. • A static method called getNumAccounts(). It should return numAccounts. Test the functionality in the main method of Account by creating a few Account objects, then print out the number of accounts
5. Create this hash variable: in perl language Scalar => ‘dollar sign’, Array => ‘at sign’,...
5. Create this hash variable: in perl language Scalar => ‘dollar sign’, Array => ‘at sign’, Hash => ‘percent sign’ Process it with a foreach loop that prints the key/value pairs so that the keys are printed in sorted order: Array: at sign Hash: percent sign Scalar: dollar sign.
IN java Create a New Java Project called LastNameDuplicate. //Given an array of N elements with...
IN java Create a New Java Project called LastNameDuplicate. //Given an array of N elements with each element between 1 and N, write a program to determine whether there are any duplicates. //You must prompt the user for the array elements. //Display the contents of the array, along with the values that are duplicated and how many times they appeared in the array. //NOTE: N should be at least 15. Input Validation: Verify that each element entered has a value...
int[]array={-40 ,60 ,78 ,-51 ,65 ,-95 ,77 ,-48 ,-66 ,71}; Create two int arrays called negative...
int[]array={-40 ,60 ,78 ,-51 ,65 ,-95 ,77 ,-48 ,-66 ,71}; Create two int arrays called negative and positive of length 10. Go through the given array and place the negative values into the array called negative and the positive values into the array called positive. Write code to find the smallest value in the given array and print the value out. Write code to find the largest value in the given array and print it out. Create an int array...
Making a blackjack game in javascript Create a function called createDeck() that will create an array...
Making a blackjack game in javascript Create a function called createDeck() that will create an array of objects (52). Each object contains two properties: suit and value. The suit property will be either 'Hearts', 'Clubs', 'Diamonds', or 'Spades'. The value property will be either 'Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three' , or 'Two''. Note: We will probably want to store possible suits and values in separate arrays. Create a function called shuffleDeck() that will...
array • First, create a function called addNumber, which has a formal parameter for an array...
array • First, create a function called addNumber, which has a formal parameter for an array of integers and increase the value of each array element by a random integer number between 1 to 10. o Add any other formal parameters that are needed. • Second, create another function called printReverse that prints this array in reverse order. • Then, you need to write a C++ program to test the use of these two functions.
Java: int[]array={-40 ,60 ,78 ,-51 ,65 ,-95 ,77 ,-48 ,-66 ,71}; 1)     Create two int arrays called...
Java: int[]array={-40 ,60 ,78 ,-51 ,65 ,-95 ,77 ,-48 ,-66 ,71}; 1)     Create two int arrays called negative and positive of length 10. Go through the given array and place the negative values into the array called negative and the positive values into the array called positive. 2)     Write code to find the smallest value in the given array and print the value out. 3)     Write code to find the largest value in the given array and print it out. 4)     Create an int...
Description: One problem with dynamic arrays is that once the array is created using the new...
Description: One problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This assignment asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have the following: • A private member variable called...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the kth smallest item in an array. Use the first item as the pivot. Compare sets of results using a static call counter. Reset counter before running another search. Create a Test drive to exhaustively test the program. // Assume all values in S are unique. kSmall(int [] S, int k): int (value of k-smallest element) pivot = arbitrary element from S:  let’s use the first...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT