Question

In: Computer Science

Write an algorithm to create seven subroutines (functions) described in the following and call them in...

Write an algorithm to create seven subroutines (functions) described in the following and call them in the main function

  1. To generate 100 random numbers between 1-100 in a randomData.txt file
  2. To read the 100 random numbers from randomData.txt and store them in an array
  3. Print the data in the array
  4. Find the smallest and the largest of the random numbers and their array position
  5. Insert an element of value100 in the 51th position of the array
  6. Delete all the elements of the array having values between 50-80 and print the residual array
  7. Sort the data in the final array(residual) in ascending order and print

Solutions

Expert Solution

------------------------------------------------------------------------------------------------

// C++ Code

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<time.h>

using namespace std;

void generateRandomNumbers(ofstream *fout){
// generating 100 random numbers between 1 to 100 and storing them in randomData.txt file
int i,r;
for(i=0;i<100;i++){
r = rand()%100 + 1;
*fout << r << endl;
}
}

void readRandomNumbers(ifstream *fin,int *randArray){
for(int i=0;i<100;i++){
*fin >> randArray[i];
}
}

void printRandomArray(int *randArray){
cout << "The elements of Random array are : { ";
for(int i=0;i<99;i++){
cout << randArray[i] << ",";
}
cout << randArray[99] << " }" << endl << endl;
}

void FindMaxMinRandArray(int *randArray){
int maxNum,minNum,maxIndex,minIndex;
maxNum = randArray[0];
minNum = randArray[0];
maxIndex = 0;
minIndex = 0;
for(int i=1;i<100;i++){
if(maxNum < randArray[i]){
maxNum = randArray[i];
maxIndex = i;
}
if(minNum > randArray[i]){
minNum = randArray[i];
minIndex = i;
}
}
cout << "The maximum Number in the random Array is " << maxNum << " at index " << maxIndex << endl;
cout << "The minimum Number in the random Array is " << minNum << " at index " << minIndex << endl;
}

void insertElementInRandArray(int *randArray){
// Element has to inserted at 51-th position in Array
// Since, the array is 0-indexed, 51-th position corresponds to 50-th index
randArray[50] = 100;
}

int deleteElements(int *randArray){

// Delete elements between 50-80(both inclusive) in the array
// Let k be the number of deleted elements in the array
// the function returns 100 - k

int temp,i,j;
i = -1;
for(j=0;j<100;j++){
// if the element is < 50 and > 80, swap with i-th element
if(randArray[j] < 50 || randArray[j] > 80){
i = i+1;
temp = randArray[j];
randArray[j] = randArray[i];
randArray[i] = temp;
}
}
//delete the numbers from (i+1) to n
//here i set them to be simply 0
for(j=i+1;j<100;j++){
randArray[j] = 0;
}

return (i+1);
}

void sortResidualArray(int *randArray,int n){
int i,j,minIndex,minNumber,temp;
for(i=0;i<n;i++){
minIndex = i;
minNumber = randArray[i];
for(j=i+1;j<n;j++){
if(minNumber > randArray[j]){
minNumber = randArray[j];
minIndex = j;
}
}
// swap the elements at minIndex and i
temp = randArray[minIndex];
randArray[minIndex] = randArray[i];
randArray[i] = temp;
}

//print the sorted Array
cout << endl << "The sorted elements of Random array are : { ";
for(i=0;i<n-1;i++){
cout << randArray[i] << ",";
}
cout << randArray[i] << " }" << endl << endl;
}

int main(){
int i,j,n;
srand(time(0));
ofstream fout;
fout.open("randomData.txt");
generateRandomNumbers(&fout);
fout.close();
ifstream fin;
fin.open("randomData.txt");
int *randArray = (int*)malloc(sizeof(int)*100);
readRandomNumbers(&fin,randArray);
printRandomArray(randArray);
FindMaxMinRandArray(randArray);
n = deleteElements(randArray);
cout << endl << "The Remaining Number of elements in the residual array are " << n << endl;
sortResidualArray(randArray,n);

return(0);
}

------------------------------------------------------------------------------------------------

// Screenshot of output

------------------------------------------------------------------------------------------------


Related Solutions

Using the string functions below, write new functions to do the following, and test them in...
Using the string functions below, write new functions to do the following, and test them in your main() function: Determine whether the first or last characters in the string are any of the characters a, b, c, d, or e. Reverse a string Determine whether a string is a palindrome (spelled the same way forward or backward FUNCTIONS REFERENCE: string myString = "hello"; // say we have a string… // … we can call any of the following // string...
Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method called stringToListOfWords() which takes in a String converts it into a list of words. We assumes that each word in the input string is separated by whitespace.3 2Use the equals() method. 3The split() method of String can split an input up along a provided special string called a regular expression or regex. A regex is much like a code...
Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method called removeAllInstances() which takes in a List and item4 . The method then proceeds to remove each item in the list that matches the given item. For example, if the method is passed the List [1, 4, 5, 6, 5, 5, 2] and the Integer 5, the method removes all 5’s from the List. The List then becomes [1,...
Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method called isPermutaion() which takes in two List objects which contain the same types. It returns true if the Lists are permutations of each other and false if not. Two lists are permutations if they contain the same elements, including the same number of duplicates, but they don’t have to contain the elements in the same order. For example, [1,2,...
Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method named allMultiples() which takes in a List of integers and an int. The method returns a new List of integers which contains all of the numbers from the input list which are multiples of the given int. For example, if the List is [1, 25, 2, 5, 30, 19, 57, 2, 25] and 5 was provided, the new list...
Write a Python program that: Create the algorithm in both flowchart and pseudocode forms for the...
Write a Python program that: Create the algorithm in both flowchart and pseudocode forms for the following requirements: Reads in a series of positive integers,  one number at a time;  and Calculate the product (multiplication) of all the integers less than 25,  and Calculate the sum (addition) of all the integers greater than or equal to 25. Use 0 as a sentinel value, which stops the input loop. [ If the input is 0 that means the end of the input list. ]...
Write a C program with call to functions to produce the output given below. // the...
Write a C program with call to functions to produce the output given below. // the requirements are that there should be 5 files; intList.h, intList.c, hw3.h, hw3.c, and main.c. please only C and use Linked List. thank you. For the 5 different files, he wants it this way: 1) main.c This file just consists of the main() function, which only consists of the displayClassInfo() function call, and the runMenuHw3() function call. 2) intList.h This file would have the IntNode...
Write C++ program as described below. Call this programChill.cpp. In April 2018 the lowest and the...
Write C++ program as described below. Call this programChill.cpp. In April 2018 the lowest and the highest temperatures ever recorded in Ohio are 25F and 81F, respectively. The wind chill factor is the perceived temperature when taking temperature and wind into account. The standard formula for wind chill factor (from the US Weather Service) is: 0.0817 * (3.71 * sqrt(Wind) + 5.81 – 0.25 * sqrt(Wind)) * (Temp – 91.4) + 91.4 where ‘sqrt’ is the square root, Wind is...
Create two series of return at 5 periods in length. Call them Return Series A and...
Create two series of return at 5 periods in length. Call them Return Series A and Return Series B. These two series of returns are independent of one another and that you are a risk-averse investor, explain which series of returns is preferable. Now, create two series of costs at 5 periods in length. Call them Cost Series C and Cost Series D. These two series of costs are independent of one another and that you hold and aversion to...
I have to write an initial algorithm and a refined algorithm for the following problem: Display...
I have to write an initial algorithm and a refined algorithm for the following problem: Display the square of numbers from 0 to some user inputted value. For example if the user enters 3 then the program will need to display the square of 0, 1, 2 and 3. Must use a counter loop. Must use 2 methods. One method that gets the number from the user and returns it. A second method is passed that number as a parameter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT