Question

In: Computer Science

C++ 9.12: Element Shifter Write a function that accepts an int array and the array’s size...

C++

9.12: Element Shifter Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file named data into an array. The program then passes the array to your element shifter function, and prints the values of the new expanded and shifted array on standard output, one value per line. You may assume that the file data has at least N values. Prompts And Output Labels. There are no prompts for the integer and no labels for the reversed array that is printed out. Input Validation. If the integer read in from standard input exceeds 50 or is less than 0 the program terminates silently.

Solutions

Expert Solution

Code:

#include <iostream>
#include <fstream>

using namespace std;

// function to shift the array provided size and array 
int *element_shifter(int arr[], int size) {

   // declare a new array of size+1 
   int *shifted_arr = new int[size+1];

   // make first element to 0
   shifted_arr[0] = 0;

   // copy rest of the array to new array
   for(int i = 1; i <= size; i++) {
      shifted_arr[i] = arr[i-1];
   }

   // return pointer to new array created
   return shifted_arr;
}


// main or driver fuction
int main() {
   
   // read value for n or size of array
   int n;
   cin >> n;

   // validate input for n
   if(n > 50 || n < 1) {
      return -1;
   }

   // open file in read mode
   fstream f;
   f.open("data.txt", ios::in);

   int arr[n];
   int i = 0, x;
   
   // read data file and store first n elements in array
   while(f >> x) {
      arr[i] = x;
      if(i == n-1) {
         break;
      }
      i++;
   }

   // call function to shiFt array and store pointer in a int * variable
   int *shifted = element_shifter(arr, n);

   cout << endl;
   
   // display the shifted array
   for(int i = 0; i < n+1; i++) {
      cout << *(shifted + i) << endl;
   }
}  

Text file:
data.txt:

10
2
3
4
1
13
12
223
4
5
9
0
12
4
5
6
7
4
5
6
90
7
6
5
6
1
12
2
32
1
2
232
43
434
33
2
21
12
3
34
35
54
5
6
5
7
78
79
89
89

Output:

FOR HELP PLEASE COMMENT.
THANK YOU


Related Solutions

In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file...
Write a function that accepts an int array and the array's size as arguments.
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array.
C++ ProgramWrite a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The function...
In C++ Write a function that accepts two int arrays of the same size. The first...
In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. THE FUNCTION SHOULD FIND ALL NUMBERS IN THE ARRAY THAT ARE GREATER THAN OR EQUAL TO THE AVERAGE. You need to design the function. so the output code should be: (show contents of 1st array) The numbers that are greater than the average of the first array are: (the...
1) Write a function searchValue that accepts an array of integers, the size of the array,...
1) Write a function searchValue that accepts an array of integers, the size of the array, and an integer. Find the last occurrence of the integer passed in as an input argument in the array. Return the index of the last occurrence of the value. If the value is not found, return a -1 2) Write the line of code to call the previous function assuming you have an array vec with length n, and are looking for the number...
Write a Python function that accepts three arguments: an array, the size of the array, and...
Write a Python function that accepts three arguments: an array, the size of the array, and a number n. Assume that array contains integers. The function should display all integers in the array that are greater than the number n. Test your function.
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another C++ function,lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Also, write a program to test your function. You must write our commands in the middle: Write a C++ Program: #include <iostream> using namespace std; const int ARRAY_SIZE = 15; void printArray(const int x[], int sizeX); int smallestIndex(const int x[], int sizeX); int main() {      int list[ARRAY_SIZE] = {56,...
Write a C++ function which takes an int array and its size as parameters. It returns...
Write a C++ function which takes an int array and its size as parameters. It returns an int indicating how many multiples of 3 are contained in the array. For example, if the array contains {2, 6, 8} your function should return 1. If the array contains {3, 10, 5, 6} your function should return 2. Here is the function prototype: // int array[]: array to search // size: number of elements in the array int countMult(const int array[], int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT