Question

Write a function that accepts an int array and the array’s size as arguments. The function...

Write a function that accepts an int array and the array’s size as arguments. 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 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 array expander function, and prints the values of the new expanded 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.

Homework Answers

Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

Where we have to paste the input file in DEV C++ ??

===================================

// nosData.txt (Input file)

45
56
67
78
89
99
88
77
66
55
44
33
22
11
17
73
39
98

=========================================

#include <fstream>
#include <iostream>

using namespace std;

int * expandArray(int arr[],int N);
int main() {
   // Declaring constant
   const int SIZE=50;
  
   // declaring variables
   string fielname="nosData.txt";
   int N;
   ifstream dataIn;
  
   //Getting the input entered by the user
   cout<<"Enter N :";
   cin>>N;
   if(N<0 || N>50)
   {
       return 1;
   }
   else
   {
       // opening the input file
       dataIn.open(fielname.c_str());
       int arr[N];
      
       /* reading the values from the file and
       * populate those values into an array
       */
       for(int i=0;i<N;i++)
       {
           dataIn>>arr[i];
       }
      
       // closing the input file
       dataIn.close();
      
       // calling the function
       int *nos=expandArray(arr,N);
      
       //Displaying the array
       cout<<"\nDisplaying Array Elements :"<<endl;
       for(int i=0;i<2*N;i++)
       {
           cout<<nos[i]<<endl;
       }
   }
  
   return 0;
}

// this function will double the array
int * expandArray(int arr[],int N)
{
   int *nos=new int[2*N];
   for(int i=0;i<2*N;i++)
   {
       nos[i]=0;
   }
   for(int i=0;i<N;i++)
   {
       nos[i]=arr[i];
   }
   return nos;
}

========================================

========================================

Output:

===================== Thank You

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a function that accepts an int array and the array’s size as arguments. The function...
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 named data into an...
Write a function that accepts an int array arr, the array’s size sz and integer n,...
Write a function that accepts an int array arr, the array’s size sz and integer n, the function should create a new array that it’s n times the array arr. The fucntion copy the content of arr to the new array n times. The Example arr = {5, 2, 1} n=3 Newarr = {5, 2, 1, 5, 2, 1, 5, 2, 1} (c++)
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i];...
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i]; } return (double)sum/n; } Rewrite the function to eliminate the array subscripting (a[i]), using pointer arithmatic instead. Write a program that reads a line of input and checks if it is a palindrome (ignoring spaces) Write a program that takes the name of a file as a command line argument, and prints the contents of the file (similar to the linux 'cat' program). Write...
C++ Write a function that takes in 3 arguments: a sorted array, size of the array,...
C++ Write a function that takes in 3 arguments: a sorted array, size of the array, and an integer number. It should return the position where the integer value is found. In case the number does not exist in that array it should return the index where it should have been if it were present in this sorted array. Use pointer notation of arrays for this question. c++ code
Write a program in c++ to Convert an array of inches to an array of centimeters....
Write a program in c++ to Convert an array of inches to an array of centimeters. The program should contain a function called inchesTOcm with three parameters (inches array that contains the values in inches, cm array to save the result in, and an integer variable that defines the length of the array). In the main function: 1. Define an array (inches) of length 3. 2. Initialize the array by asking the user to input the values of its elements....
Write C++ code to: Create a function called “ReadFile” that: Accepts a filename as input Opens...
Write C++ code to: Create a function called “ReadFile” that: Accepts a filename as input Opens the file for reading Reads an integer from the file and returns it. Create a main function that Asks the user to input a file Passes that file to the “ReadFile” function Prints the returned integer to the screen Extra Credit: Set a default argument of “intfile.txt” for the “ReadFile” function Write an overloaded function of “ReadFile” that accepts two filenames and reads an...
(a) Write a function in C++ called readNumbers() to read data into an array from a...
(a) Write a function in C++ called readNumbers() to read data into an array from a file. Function should have the following parameters: (1) a reference to an ifstream object (2) the number of rows in the file (3) a pointer to an array of doubles The function returns the number of values read into the array. It stops reading if it encounters a negative number or if the number of rows is exceeded. (b) Write a program with the...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
In PYTHON please: Write a function named word_stats that accepts as its parameter a string holding...
In PYTHON please: Write a function named word_stats that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report the total number of words (as an integer) and the average word length (as an un-rounded number). For example, suppose the file tobe.txt contains the following text: To be or not to be, that is the...
Write an application in Java which includes an algorithm that takes an array of any size,...
Write an application in Java which includes an algorithm that takes an array of any size, selects the high and low integer from the array of integers with each pass and builds a new array of integers by inserting the high and low selection with each pass. Your output should show the original array of integers and the output of each pass on a new line. Note: You can assume all integers are positive, but your code must work for...