Question

C++ program on visual studio.

Name your program dynamic arrays cpp 6. Here are 4 files: uuunheksust aumhers2 and waubera3izst aad uumbers.4 st. The first two coutains a header line telling you how many data items follow The first one is very sma l, so you can easily verify you are getting the right results: the second is larger, too large to verify by hand The second pair are like the first two, except they are inst a list of numbers (no header, e., no count at the top a. Write a program that can calculate the average, min and max of the numbers in the first 2 files. It should rcad the first value of points), allocate an array the corroet size, rcad the data into that array, then analyze the array and print the count, average min & max of the array a. Use a function to read the data. a second function to find the average. and a third function to find the min & max. ead the count and allocate the array itselfin the read data routine your read datar...) routine will have to pass back both the count and the pointer after it allocates the array b, I r omurend that you print out the Iirst few elements aud the last few elem ul be data ile EL you read them in to Luake sure you get all the values. Soule p slal s ill the various routiues ould also riul be helpful while writing ing to make sure things are working properly. c. Make sure you delete the dynamic array properly and set your pointer back to NULL b. Modify your program to work with an array of filenames, and read and print the statistics for each file in the list. You can use an array of strings for the filename mbers l txt, n11m .txt mbers e. Wrile a modified version of the program ir part a. ulat van work with the files numbers3.ixu and LuLubers4.xl he uuly differerke is bat the file doesnl lell u the size upfront. Your prograILL will have lo read uhe file twice once to count how many values there are, and then (after allocating an array read in the data and get the min, nmax and averag a. Use a function count valuestfilename) that reads the file and counts the number of values b. Note: if youre not care l, you can end up reading and counting the last elemeat twice! Make sure you dont do that...remember, cou is your friend here. You ca ot strings tor the array Name your programs read nums.cpp (part by and read nums nh.cpp (part c)

File number 1

File number 2

100
0.6586 0.3846 0.4242 0.0112 0.8436 0.5168 0.1788 0.8928 
0.1873 0.1028 0.3814 0.2861 0.4140 0.8359 0.4081 0.0883 
0.2221 0.1202 0.6439 0.9184 0.7060 0.0387 0.0478 0.4748 
0.5952 0.0356 0.0935 0.3628 0.1593 0.1353 0.6460 0.9259 
0.7315 0.0014 0.8023 0.4504 0.1959 0.4978 0.8705 0.0927 
0.2421 0.2144 0.9471 0.7074 0.4388 0.9107 0.8313 0.3972 
0.3291 0.9885 0.4689 0.7887 0.6331 0.9285 0.0664 0.2858 
0.8726 0.6144 0.7742 0.4244 0.1807 0.3594 0.7822 0.0160 
0.3048 0.1838 0.9963 0.6201 0.3480 0.6696 0.5898 0.0534 
0.2794 0.0626 0.7458 0.9797 0.6402 0.2789 0.7918 0.4829 
0.8573 0.3744 0.8515 0.6129 0.7993 0.1410 0.6169 0.4777 
0.2654 0.3925 0.1797 0.9534 0.3515 0.1373 0.2590 0.5262 
0.8967 0.5786 0.5796 0.9753 

File 3

0.6587 0.0946 0.2470 0.9736 0.7696 0.3633 0.7904 0.7180 
0.0182 0.2538

File 4

0.6588 0.3307 0.0923 0.7707 0.8266 0.0856 0.5901 0.2587 
0.5662 0.1436 0.8865 0.8686 0.3430 0.8731 0.0120 0.9793 
0.3566 0.3511 0.1464 0.9237 0.2305 0.6419 0.7171 0.3879 
0.9802 0.8434 0.8524 0.9803 0.6393 0.1710 0.9556 0.7918 
0.6051 0.4253 0.8847 0.2974 0.1751 0.1901 0.6016 0.0529 
0.6532 0.6909 0.6270 0.3081 0.9844 0.0913 0.7804 0.9384 
0.5104 0.0808 0.4637 0.1207 0.7664 0.1757 0.8443 0.2810 
0.4681 0.5441 0.0504 0.2413 0.3203 0.4950 0.2709 0.5870 
0.9040 0.0031 0.9806 0.3421 0.7810 0.8820 0.6110 0.5777 
0.5918 0.4771 0.6382 0.9131 0.8696 0.7814 0.6373 0.5891 
0.6919 0.1354 0.3276 0.2666 0.8467 0.0274 0.7036 0.2401 
0.2492 0.0497 0.1755 0.1430 0.8819 0.5356 0.9924 0.9056 
0.2592 0.8239 0.6592 0.4372 0.3992 0.7785 0.9229 0.4604 
0.7436 0.4442 0.3306 0.7299 0.5410 0.2965 0.8120 0.9367 
0.6246 0.9924 0.6948 0.2888 0.5946 0.9179 0.3516 0.7335 
0.6317 0.7017 0.6118 0.7904 0.8157 0.8506 0.6783 0.3412 
0.3074 0.3373 0.5280 0.2740 0.6918 0.2992 0.3041 0.3716 
0.8320 0.0446 0.2283 0.8051 0.6274 0.3045 0.8478 0.2471 
0.3591 0.8741 0.1698 0.8507 0.9717 0.5391 0.1541 0.7754 
0.7072 0.9070 0.3792 0.8400 0.1867 0.5889 0.1523 0.8331 
0.2234 0.2612 0.8041 0.6179 0.3373 0.4647 0.3963 0.3560 
0.7369 0.2827 0.4297 0.3351 0.3868 0.0790 0.0090 0.8626 
0.2649 0.0000 0.6278 0.9614 0.6589 0.9136 0.5878 0.9059 
0.9191 0.5276 0.1577 0.0641 0.5367 0.5814 0.0401 0.3747 
0.6924 0.5495 0.6036 0.5485 0.6035 0.0843 0.7249 0.9070 
0.7063 0.6156 0.5080 0.2265 0.4235 0.3792 0.4684 0.5095 
0.9370 0.5267 0.9792 0.6033 0.4888 0.9496 0.2659 0.2492 
0.0191 0.4682 0.4594 0.1840 0.8174 0.2051 0.8201 0.8615 
0.7371 0.2059 0.7935 0.9230 0.2111 0.6422 0.2651 0.0267 
0.2366 0.2076 0.4245 0.9873 0.6739 0.4704 0.2711 0.8266 
0.9413 0.8225 0.4302 0.8097 0.1124 0.5928 0.6481 0.1508 
0.7231 0.8831 0.5020 0.4352 0.3359 0.8441 0.4119 0.7372 
0.6741 0.8087 0.5038 0.8104 0.9070 0.8843 0.7849 0.9730 
0.9305 0.5505 0.5111 0.2630 0.8931 0.1373 0.4432 0.8164 
0.1316 0.6408 0.1194 0.9835 0.6316 0.9561 0.5249 0.3313 
0.1764 0.4484 0.4166 0.1489 0.9874 0.5722 0.5663 0.0751 
0.0402 0.0365 0.7163 0.9608 0.4102 0.4177 0.5507 0.9265 
0.0336 0.8749 0.2448 0.4612 0.6280 0.6056 0.0160 0.7277 
0.1214 0.6448 0.4698 0.0216 0.3909 0.1478 0.8785 0.5933 
0.0555 0.3265 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

(a)

C++ Program:

// Program that finds Average, Min and Max of array
#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

//Function that reads data into array from file
double* read_data(int &len)
{
   string fileName;
   int i;
   double *arr;

   //Reading file name
   cout << "\n Enter file Name: ";
   getline(cin, fileName);

   //OPening file in read mode
   fstream fin(fileName, ios::in);

   //If file was failed to open
   if(fin.fail())
   {
       cout << "\n Error Opening file... ";
       return NULL;
   }

   //Reading length
   fin >> len;

   //Allocating memory
   arr = new double[len];

   //Reading values from file
   for(i=0; i<len; i++)
   {
       fin >> arr[i];
   }

   //Returning length of array
   return arr;
}

//Function that calculates average
void calc_average(double *arr, int n)
{
   double sum=0;
   int i;
   double average;

   //Iterating over array
   for(i=0; i<n; i++)
   {
       sum = sum + arr[i];
   }

   //Calculating average
   average = sum / (double)n;

   //Printing average
   cout << "\n\n Average of values: " << average << " \n";
}

//Function that finds min and max values
void calc_min_max(double *arr, int n)
{
   double min, max;
   int i;

   //Initially assume that first element is min and max
   min = arr[0];
   max = arr[0];

   //Iterating over array
   for(i=0; i<n; i++)
   {
       //Comparing min element
       if(arr[i] < min)
       {
           //Updating min element
           min = arr[i];
       }

       //Comparing max element
       if(arr[i] > max)
       {
           //Updating max element
           max = arr[i];
       }
   }

   //Printing average
   cout << "\n\n Minimum Element: " << min << " \n";
   cout << "\n Maximum Element: " << max << " \n";
}

//Main function
int _tmain(int argc, _TCHAR* argv[])
{
   double *arr = NULL;
   int length;

   //Reading data from file
   arr = read_data(length);

   //Finding average
   calc_average(arr, length);

   //Finding min and max
   calc_min_max(arr, length);

   getchar();

   return 0;
}

Sample Output:

c:luserslsaibabuidocumentslvisual studio 20101Project Enter file Name: number1.txt Average of values 0.59872 Mininum Element:

_______________________________________________________________________________________________

(b)

// Program that reads data from array of files and prints average, min and max

#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

//Function that reads data into array from file
double* read_data(string fileName, int &len)
{
   int i;
   double *arr;

   //OPening file in read mode
   fstream fin(fileName, ios::in);

   //If file was failed to open
   if(fin.fail())
   {
       cout << "\n Error Opening file... ";
       return NULL;
   }

   //Reading length
   fin >> len;

   //Allocating memory
   arr = new double[len];

   //Reading values from file
   for(i=0; i<len; i++)
   {
       fin >> arr[i];
   }

   //Returning length of array
   return arr;
}

//Function that calculates average
void calc_average(double *arr, int n)
{
   double sum=0;
   int i;
   double average;

   //Iterating over array
   for(i=0; i<n; i++)
   {
       sum = sum + arr[i];
   }

   //Calculating average
   average = sum / (double)n;

   //Printing average
   cout << "\n\n Average of values: " << average << " \n";
}

//Function that finds min and max values
void calc_min_max(double *arr, int n)
{
   double min, max;
   int i;

   //Initially assume that first element is min and max
   min = arr[0];
   max = arr[0];

   //Iterating over array
   for(i=0; i<n; i++)
   {
       //Comparing min element
       if(arr[i] < min)
       {
           //Updating min element
           min = arr[i];
       }

       //Comparing max element
       if(arr[i] > max)
       {
           //Updating max element
           max = arr[i];
       }
   }

   //Printing average
   cout << "\n\n Minimum Element: " << min << " \n";
   cout << "\n Maximum Element: " << max << " \n";
}

//Main function
int _tmain(int argc, _TCHAR* argv[])
{
   double *arr = NULL;
   int length, i;

   //Array of file names
   string fileNames[] = {"number1.txt", "number2.txt"};

   for(i=0; i<2; i++)
   {
       cout << "\n\n File Name: " << fileNames[i] << " \n";

       //Reading data from file
       arr = read_data(fileNames[i], length);

       //Finding average
       calc_average(arr, length);

       //Finding min and max
       calc_min_max(arr, length);
   }
  
   getchar();

   return 0;
}


Sample Output:

cusers saibabu documents visual studio File Name: number1.txt Average of values: 0.59872 Mininum Element: Maxinum Element: 8.

____________________________________________________________________________________________

(c)

// Program that reads data from array of files and prints average, min and max

#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

//Function that counts number of values
int count_values(string fileName)
{
   int len=0;
   double val;

   //OPening file in read mode
   fstream fin(fileName, ios::in);

   //If file was failed to open
   if(fin.fail())
   {
       cout << "\n Error Opening file... ";
       return 0;
   }

   //Counting values in file
   while(fin.good())
   {
       fin >> val;

       //Accumulating length
       len = len + 1;
   }

   //Closing file
   fin.close();

   return len;
}

//Function that reads data into array from file
double* read_data(string fileName, int len)
{
   int i;
   double *arr;

   //OPening file in read mode
   fstream fin(fileName, ios::in);

   //If file was failed to open
   if(fin.fail())
   {
       cout << "\n Error Opening file... ";
       return NULL;
   }

   //Allocating memory
   arr = new double[len];

   //Reading values from file
   for(i=0; i<len; i++)
   {
       fin >> arr[i];
   }

   fin.close();

   //Returning length of array
   return arr;
}

//Function that calculates average
void calc_average(double *arr, int n)
{
   double sum=0;
   int i;
   double average;

   //Iterating over array
   for(i=0; i<n; i++)
   {
       sum = sum + arr[i];
   }

   //Calculating average
   average = sum / (double)n;

   //Printing average
   cout << "\n\n Average of values: " << average << " \n";
}

//Function that finds min and max values
void calc_min_max(double *arr, int n)
{
   double min, max;
   int i;

   //Initially assume that first element is min and max
   min = arr[0];
   max = arr[0];

   //Iterating over array
   for(i=0; i<n; i++)
   {
       //Comparing min element
       if(arr[i] < min)
       {
           //Updating min element
           min = arr[i];
       }

       //Comparing max element
       if(arr[i] > max)
       {
           //Updating max element
           max = arr[i];
       }
   }

   //Printing average
   cout << "\n\n Minimum Element: " << min << " \n";
   cout << "\n Maximum Element: " << max << " \n";
}

//Main function
int _tmain(int argc, _TCHAR* argv[])
{
   double *arr = NULL;
   int length, i;

   //Array of file names
   string fileNames[] = {"number3.txt", "number4.txt"};

   for(i=0; i<2; i++)
   {
       cout << "\n\n File Name: " << fileNames[i] << " \n";
      
       length = count_values(fileNames[i]);

       //Reading data from file
       arr = read_data(fileNames[i], length);

       //Finding average
       calc_average(arr, length);

       //Finding min and max
       calc_min_max(arr, length);
   }
  
   getchar();

   return 0;
}

Sample Output:

clusers saibabuldocuments visual studio 2010 File Name number3.txt Average of values:0.48872 Minimum Element 0.0182 Maxinun E

Add a comment
Know the answer?
Add Answer to:
C++ program on visual studio. File number 1 File number 2 100 0.6586 0.3846 0.4242 0.0112...
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
  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • 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...

  • C programming The program will require the following structure: struct _data { char *name; long number;...

    C programming The program will require the following structure: struct _data { char *name; long number; }; The program will require command line arguments: int main(int argv, char **argc) { Where argv is the number of arguments and argc is an array holding the arguments (each is a string). Your program must catch any case where no command line arguement was provided and print a warning message (see below). You MUST include/use the following functions, defined as follows: int SCAN(FILE...

  • Write a program that will take input from a file of numbers of type double and...

    Write a program that will take input from a file of numbers of type double and output the average of the numbers in the file to the screen. Output the file name and average. Allow the user to process multiple files in one run. Part A use an array to hold the values read from the file modify your average function so that it receives an array as input parameter, averages values in an array and returns the average Part...

  • c++ by microsoft visual studio i wanna the answer for header file , cpp file ,...

    c++ by microsoft visual studio i wanna the answer for header file , cpp file , main file. 1). The function remove of the class arrayListType removes only the first occurrence of an element. Add the function removeAll to the class arrayListType that would remove all occurrences of a given element. Also, write the definition of the function removeAll and a program to test this function. 2)Add the function min to the class arrayListType to return the smallest element of...

  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • C++ Programming

    PROGRAM DESCRIPTIONIn this project, you have to write a C++ program to keep track of grades of students using structures and files.You are provided a data file named student.dat. Open the file to view it. Keep a backup of this file all the time since you will be editing this file in the program and may lose the content.The file has multiple rows—each row represents a student. The data items are in order: last name, first name including any middle...

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