Question

In: Computer Science

One dimensional dynamic array Write a function that returns the number of integers in an input...

One dimensional dynamic array

Write a function that returns the number of integers in an input file stream with the following interface:

int findNumber(ifstream &x);

Then, use this number to dynamically allocate an integer array.

Write another function that reads each number in an input file stream and assign the value to the corresponding array element with the following interface:

void assignNumber(ifstream &x, int y[ ]);

In your main( ), first open “in.dat” as an input file. Next, apply findNumber( ) function on this input stream. Then, you close the file stream. Dynamically allocate a one-dimensional array based on the number of integers in in.dat. After that, you re-open “in.dat” as an input file, and apply assignNumber( ) function on the file stream and assign each array element with the corresponding value in in.dat.

At the end of main( ), print out every element of the dynamically allocated array as an evidence of your work in the format of a screenshot.

Solutions

Expert Solution

Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you

input file: in.dat
-------
35
12
40
56
10
16

main.cpp
-----
#include <iostream>
#include <fstream>
using namespace std;

int findNumber(ifstream &x);
void assignNumber(ifstream &x, int y[ ]);
int main(){
   ifstream infile;
   int n;
   int *arr;
  
   infile.open("in.dat");
   if(infile.fail()){
       cout << "ERROR: could not open file in.dat" << endl;
       return 1;
   }
  
   n = findNumber(infile);
   infile.close();
   arr = new int[n];
   infile.open("in.dat");
   assignNumber(infile, arr);
   infile.close();
   cout << n << " numbers read from file are-" << endl;
   for(int i = 0; i < n; i++)
       cout << arr[i] << endl;
  
   delete []arr; //deallocate memory
  
  
   return 0;
}


int findNumber(ifstream &x){
   int count = 0;
   int num;
   while(x >> num)
       count++;
   return count;
      
}
void assignNumber(ifstream &x, int y[ ]){
   int i = 0;
   int num;
   while(x >> num){
       y[i++] = num;
   }
}

output
----
6 numbers read from file are-
35
12
40
56
10
16


Related Solutions

Python: Write a function that receives a one dimensional array of integers and returns a Python...
Python: Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions. Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result. Below...
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and...
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and returns true if the matrix is an upper matrix and returns false otherwise. Remark: A matrix M is an upper matrix if it is a square matrix (row size = column size) and every element M[i][j] = 0 for all i > j. That is all the elements of the matrix below the main diagonal are zero.
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Write a class VectorInt to implement the concept of one dimensional array of integers with extendable...
Write a class VectorInt to implement the concept of one dimensional array of integers with extendable array size. Your class should support storing an integer at a specific index value, retrieving the integer at a specific index value, and automatically increasing storage for the saved values.
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and...
Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and returns a second vector that contains only the odd numbers. It will return zero if all elements are even. Use error-traps to check against probable errors in user input. In case of an error, it will return NaN. You are allowed to use Matlab built-in function round(). Check your code with the following arrays: >> y1 = [18, -5, 89, -7, 4, 10, 12,...
Let A be an array of non-decreasing N integers. Write an algorithm that returns the number...
Let A be an array of non-decreasing N integers. Write an algorithm that returns the number of pairs of integers in A that are equal. Analyze your algorithm thoroughly. Your analysis should include a thorough examination of both the best and the worst-case scenarios. This includes a description of what the best and worst cases would look like. You should include both space and time in your analysis, but keep in mind that space refers to “extra space,” meaning in...
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers as follows: Assign {35,20,-43,-10,6,7,13} -Create a one dimensional array of 7 Boolean values as follows: Assign {true,false,false,true,false,true,false} -Create a one dimensional array of 7 floating-point values as follows: Assign {12.0f,1.5f,-3.5f,-2.54f,3.4f,45.34f,22.13f} -Declare sum as integer and set it to 0. -Declare sumf as float and set it to 0.0f. -Use a for loop to go through each element of the Boolean array, and if an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT