Question

You are to write three functions for this lab: mean, remove, display. Download the main.cpp file...

You are to write three functions for this lab: mean, remove, display. Download the main.cpp file provided to get started.

Read the comments in the main function to determine exactly what the main function should do.

//include any standard libraries needed


// - Passes in an array along with the size of the array.
// - Returns the mean of all values stored in the array.
double mean(const double array[], int arraySize);

// - Passes in an array, the size of the array by reference, and the index of a value to be removed from the array.
// - Removes the value at this index by shifting all of the values after this value up, keeping the same relative order of all values not removed.
// - Reduces arraySize by 1.
void remove(double array[], int &arraySize, int index);


// - Passes in an array and the size of the array.
// - Outputs each value in the array separated by a comma and space, with no comma, space or newline at the end.
void display(const double array[], int arraySize);


const int ARR_CAP = 100;

int main(int argc, char *argv[]) {

// verify file name provided on command line

// open file and verify it opened

// Declare an array of doubles of size ARR_CAP.

  
// Fill the array with up to ARR_CAP doubles from the file entered at the command line.

  
// Call the mean function passing it this array and output the
// value returned.
  
// Ask the user for the index (0 to size - 1) of the value they want to remove.

  
// Call the display function to output the array.


// Call the remove function to remove the value at the index
// provided by the user.
  
  
// Call the display function again to output the array
// with the value removed.


// Call the mean function again to get the new mean


   return 0;
}

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

#include<iostream>
#include<fstream>
using namespace std;
double mean(const double array[], int arraySize);
void remove(double array[], int &arraySize, int index);
void display(const double array[], int arraySize);
const int ARR_CAP = 100;

int main(int argc, char *argv[])
{
   ifstream infile; //declare file object
   int i,index,size;
   double mn;
   if(argc!=2) //check the validity of command
   {
      cout<<endl<<"Invalid file name or command.";
      exit(0);
       }
       //open the file specified in command line
       infile.open(argv[1]);
       //check for opened or not
       if(!infile)
       {
          cout<<endl<<"Unable to open the file";
          exit(0);
       }
       //declare an array of double with size ARR_CAP
double arr[ARR_CAP];
//loop to read ARR_CAP numbers from file
       for(i=0;i<ARR_CAP;i++)
       infile>>arr[i]; //store the numbers into the array
       //assign the value of ARR_CAP into size, because ARR_CAP is a constant value
       //so after remove function it can not be reduced so we will use size.
   size=ARR_CAP;     
   cout<<endl<<"Mean of array elements are : "<<mean(arr,size)<<endl;
//loop to validate the index. It will accept the values between 0 and ARR_CAP-1(INCLUSIVE)
   do
   {
   cout<<endl<<"Enter the index to delete the element";
   cin>>index;  
   }while(index<0 || index>=size);
   cout<<endl<<"ARRAY ELEMENTS BEFORE REMOVAL:\n";
   display(arr,size); //call to display function
   //call to remove function
   remove(arr,size,index);
   cout<<endl<<"ARRAY ELEMENTS AFTER REMOVAL:\n";
   //call to display function
       display(arr,size);
       //call to mean()
       cout<<endl<<"Mean of array elements are : "<<mean(arr,size)<<endl;
}
//method to remove the array element from the index provided
void remove(double array[], int &arraySize, int index)
{
   int i;
   //loop to shift the numbers upward fromthe index
   for(i=index;i<arraySize;i++)
   {
       array[i]=array[i+1]; //shift the numbers upward
   }
   arraySize=arraySize-1;//update the size of array
}
//method to compute the mean of the array elements
double mean(const double array[], int arraySize)
{
    int i;
    double sum=0; //declare sum and initialize it to 0
    for(i=0;i<arraySize;i++)
    {
       sum=sum+array[i]; //find the sum of all numbers
   }
   return sum/arraySize; //return the mean
}
//method to display the numbers
void display(const double array[], int arraySize)
{
    int i;
    for(i=0;i<arraySize;i++)
    {
        if(i%15==0) //condition is uded just for print the numbers in formatted manner
        cout<<endl;
        if(i!=arraySize-1)
        cout<<array[i]<<", ";//printh the array elements by using comma and space
        else
           cout<<array[i];//print the las element
   }
}

OUTPUT

PROVIDE THE FILE NAME IN PARAMETER WHICH RESIDES IN EXECUTE MENU

Add a comment
Know the answer?
Add Answer to:
You are to write three functions for this lab: mean, remove, display. Download the main.cpp file...
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*** * In this lab, you will write a program with three recursive functions you...

    **IN C*** * In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab, keep all functions in a single source file: main.c Here are the three functions you will write. For each function, the output example is for this array: int array[ ] = { 35, 25, 20, 15, 10 }; • Function 1: This function is named printReverse(). It takes in an array of integers...

  • Consider the following C++code snippet and what is the output of this program? # include<iostream> using...

    Consider the following C++code snippet and what is the output of this program? # include<iostream> using namespace std; void arraySome (int[), int, int); int main () const int arraysize = 10; int a[arraysize]-1,2,3,4,5, 6,7,8,9,10 cout << "The values in the array are:" << endl; arraySome (a, 0, arraySize) cout<< endl; system ("pause") return 0; void arraySome (int b[], int current, int size) if (current< size) arraySome (b, current+1, size); cout << b[current] <<""; a) Print the array values b) Double...

  • Use two files for this lab: your C program file, and a separate text file containing...

    Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...

  • pls help Write a method void remove(int *a, int index) that will remove the number at...

    pls help Write a method void remove(int *a, int index) that will remove the number at the given index and shift all remaining numbers one position to the left in the array a. Assume 1that the last element of the array is -1. Now, write a main function that will define an array int A[40]=[3, 5, 9, 17, 24, -1]; read from user input an index; and call the method remove passing array A and the index given by the...

  • #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort,...

    #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort, search // Define computeAvg here // Define findMax here // Define findMin here // Define selectionSort here ( copy from zyBooks 11.6.1 ) // Define binarySearch here int main(void) { // Declare variables FILE* inFile = NULL; // File pointer int singleNum; // Data value read from file int valuesRead; // Number of data values read in by fscanf int counter=0; // Counter of...

  • What's wrong with my code? : I'm trying to use recursive functions to display and count...

    What's wrong with my code? : I'm trying to use recursive functions to display and count all the even numbers of an array. However, I can't seem get the program output the number of even integers . Here's the output I want: Here are the 5 even numbers: // Luckily, however, my code does output all the even numbers. But, I also want the program to tell me how may even integers there are. 2 8 14 18 22 MY...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • Variable Size Array with Classes, Testing. Study Code and Object Definition Windows of Microsoft ...

    Variable Size Array with Classes, Testing. Study Code and Object Definition Windows of Microsoft Visual Studio described here. As you work on the below project, demonstrate to the instructor the usage of this feature. Create a project titled Lab11_VarArrayTest. Implement the dynamically expanding and contracting array of doubles described in the previous lab as a class. You should use this class definition. The class attributes are a pointer to the dynamically allocated array dAarray and the array size size This...

  • This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and...

    This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...

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
ADVERTISEMENT