Question

C++ no std: cout but cout<<" ". also nothing ".h": in the header files: Also if...

C++ no std: cout but cout<<" ". also nothing ".h": in the header files: Also if statements should be used for the file portion. (filein.eof is not aloud)

generate integer random numbers and place them in a file. Each number on a single line. The numbers should range between 1-200 and the program should generate between 100-150 numbers. This means each time the program is executed, a file is created containing between 100-150 numbers with values between 1-200. Make sure you close the file once the numbers are generated. This should be done in a separate function B- The program should then read the file and create a dynamic array that holds the numbers. The size of the array is the number of elements in the file just read. This should be in a separate function.

Print values of the array: this should be done by creating a function that accepts two arguments, a const integer pointer to the array and size printValues(const int *arr, int size).

D- The main program should ask the user to enter a "key value" and then, -by a function call- find all numbers that are above that key value, store them in a dynamic array and return that array. Then your program should call the printValues function passing the new array. This should be implemented in as a pointer returning from a function (i.e. the function should return a pointer to the new array)

4- Print the array in reverse . Your program (via a function) should create a new copy of the array, except that the elements should be in reverse. The function should return a pointer to the new array. Then call printValues to show the values of the new array.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ PROGRAM:

#include <iostream>

#include <stdlib.h>

#include <time.h>   

#include <fstream>

using namespace std;

void RandomNumbers();

int* ReadFile(int&);

void printValues(const int*, int);

int* searchNumbers(const int*,int,int&);

int main() {

int *arr =NULL;

int arrSize;

RandomNumbers();

arr = ReadFile(arrSize);

cout<<"Initial array elements : "<<endl;

printValues(arr,arrSize);

int searchSize;

int *searchResults = searchNumbers(arr,arrSize,searchSize);

printValues(searchResults,searchSize);

return 0;

}

void RandomNumbers()

{

srand(time(NULL));

ofstream fileOut;

fileOut.open("beta.txt");

int n = rand()%50 +100;

int num;

for(int i=0;i<n-1;i++)

{

num = rand() % 199 + 1;

fileOut<<num<<endl;

}

num = rand()%199 +1;

fileOut<<num;

fileOut.close();

}

int* ReadFile(int &size)

{

ifstream fileIn;

fileIn.open("beta.txt");

int count=0;

int num;

int *arr;

if(fileIn.is_open())

{

while(!fileIn.eof())

{

fileIn>>num;

count++;

}

size=count;

fileIn.seekg (0, fileIn.beg);

arr = new int[size];

int i=0;

while(!fileIn.eof())

{

fileIn>>arr[i];

i++;

}

}else

cout<<"\n Unable to open file";

fileIn.close();

return arr;

}

void printValues(const int *arr, int n )

{

for(int i=0;i<n;i++)

{

cout<<*(arr+i)<<endl;

}

}


int* searchNumbers(const int *arr,int n,int &searchSize)

{

int key;

int *searchResults ;

cout<<"\n Enter the key value : ";

cin>>key;

int count=0;

for(int i=0;i<n;i++)

{

if(*(arr+i) > key)

count++;

}

searchResults = new int[count];

searchSize = count;

count=0;

for(int i=0;i<n;i++)

{

if(*(arr+i) > key)

{

*(searchResults+count) = *(arr+i);

count++;

}

}

cout<<"Array with numbers > "<<key<<endl;

return searchResults;

}

Add a comment
Know the answer?
Add Answer to:
C++ no std: cout but cout<<" ". also nothing ".h": in the header files: Also if...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • 3460:209 Assignment 9-B Assignment9-B: The Element Shifter The purpose of this assignment is to help gauge...

    3460:209 Assignment 9-B Assignment9-B: The Element Shifter The purpose of this assignment is to help gauge your skills in writing small programs that involve pointers. The program also contains functions and may perform input, output, files and file processing, use arrays and vectors and/or c-string/string arrays, flow of control, and/or calculations. PROGRAM SPECIFICATION For this program, we are going to expand a standard array by dynamically allocating a new one with a larger footprint. The program will use a function...

  • #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the...

    #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the array"<<endl;    cin>>SIZE;     int *numlist = new int[SIZE];     // Read SIZE integers from the keyboard     for (int i = 0; i<SIZE; i++ )    {        cout << "Enter value #" << i+1 << ": ";        cin >> numlist[i];     }     // Display the numbers in a reverse order     for (int i = SIZE; i > 0; i--...

  • Note: None of these functions should use cout. It is OK to use cout while debugging...

    Note: None of these functions should use cout. It is OK to use cout while debugging to check how your function is working, but your final submission should not contain cout in any function but main. Head ==== Name the source file for this section head.cpp. Write a function named "head" which takes an array of integers as an argument, and returns the first element in the array. Write a function named main which outputs the result of testing your...

  • 1. The following program calls the function countLarger that accepts three arguments: an integer array, an...

    1. The following program calls the function countLarger that accepts three arguments: an integer array, an integer size that indicates how many elements are in the array, and an integer n. The function countLarger should return the number of integers in the array that are greater than the value of the argument n. Update the program to include the definition of the function countLarger. #include <iostream> using namespace std; int countLarger(int[], int, int); // Function prototype int main() const int...

  • - Write a program that performs several operations on an array of positive ints. The program...

    - Write a program that performs several operations on an array of positive ints. The program should prompt the user for the size of the array (validate the size) and dynamically allocate it. Allow the user to enter the values, do not accept negative values in the array. - Your program should then define the functions described below, call them in order, and display the results of each operation: a) reverse: This function accepts a pointer to an int array...

  • 1. Your project will include the following three files: A header file: dynamicArray.h that includes a...

    1. Your project will include the following three files: A header file: dynamicArray.h that includes a list of function prototypes as enumerated in the next section. An implementation file: dynamicArray.cpp that implements the functions declared in the header file. A test driver file: dynamicArray-main.cpp that includes the main() function so that you can test all the functions you've implemented above. 2. The header file dynamicArray.h will include the following list of functions: constructing a dynamic array of the specified size...

  • c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input:...

    c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input: an int output: boolean function: Return true if the number is a prime number and return false otherwise */ //TO DO ************************************** /* Write a function checkPrime such that input: an array of int and size of array output: nothing function: Display the prime numbers of the array */ //TO DO ************************************** /* Write a function SumofPrimes such that input: an int output: nothing...

  • (C++ 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. The first element...

    (C++ 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. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the 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....

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT