Question

In: Computer Science

Write a C++ program to read a data file containing the velocity of cars crossing an...

Write a C++ program to read a data file containing the velocity of cars crossing an intersection. Then determine the average velocity and the standard deviation of this data set. Use the concept of vector array to store the data and perform the calculations. Include a function called “Standard” to perform the standard deviation calculations and then return the value to the main function for printing.

Data to use. 10,15,20,25,30,35,40,45,50,55.

Please use something basic.

Solutions

Expert Solution

CODE:

#include<iostream>
#include<bits/stdc++.h>
#include<vector>
#include<cmath>

using namespace std;

//function Standard accepts two parameters numbers vector list and an int mean
//mean is the average of numbers in the vector list
float Standard(vector<int> numbers, int mean){
float variance = 0;
//firs calculating the variance
for(int i=0;i<numbers.size();i++){
variance += pow(mean - numbers.at(i),2);
}
//calculating the variance
variance = variance/numbers.size();
//square root of variance is the standard deviation
float stdDev = pow(variance,0.5);
//returning the standard Deviation
return stdDev;
}

//main method
int main(){
//declaring a vector
vector<int> numbers;
fstream file;
//opening the file
file.open("input.txt",ios::in);
int number;

//populating the vector list with the numbers in the txt file
while(file>>number){
numbers.push_back(number);
}

int sum = 0;
//calculating the average of the numbers
for(int i=0;i<numbers.size();i++){
sum += numbers.at(i);
}
sum = sum/numbers.size();
cout<<"Average velocity: "<<sum<<endl;

cout<<"Standard Deviation: "<<Standard(numbers,sum)<<endl;
//closing the file
file.close();
return 0;
}
_______________________________________

CODE IMAGES:

_____________________________________________

OUTPUT:

input.txt

numbers in the txt file should be entered in the same format for proper functioning of the code

input.txt:

10
15
20
25
30
35
40
45
50
55

_____________________________________________________

Feel free to ask any questions in the comments section
Thank You!


Related Solutions

Write a C++ program that does the following: Read and input file containing the following PersonAName,...
Write a C++ program that does the following: Read and input file containing the following PersonAName, PersonBName, XA,YA, XB, YB where the coordinates of PersonA in a 100 by 100 room is XA, YA and the coordinates of PersonB is XB, YB. Use square root function in cmath sqrt() to calculate the shortest distance between two points. A file will be uploaded in this assignment that will list coordinates of two people. The program should use a function call that...
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
Write a C program that will read different data types from the following file and store...
Write a C program that will read different data types from the following file and store it in the array of structures. Given file: (This file have more than 1000 lines of similar data): time latitude longitude depth mag magType nst gap dmin 2020-10-19T23:28:33.400Z 61.342 -147.3997 12.3 1.6 ml 12 84 0.00021 2020-10-19T23:26:49.460Z 38.838501 -122.82684 1.54 0.57 md 11 81 0.006757 2020-10-19T23:17:28.720Z 35.0501667 -117.6545 0.29 1.51 ml 17 77 0.1205 2020-10-19T22:47:44.770Z 38.187 -117.7385 10.8 1.5 ml 15 100.22 0.049 2020-10-19T22:42:26.224Z...
Write a Fortran program that is able to read in the data file. The file has...
Write a Fortran program that is able to read in the data file. The file has lines with the structure: 19990122 88888 30.5 Where: i) the first is an 8 digit code with the date: yyyymmdd (yyyy is the year, mm is the month, and dd is the day) ii) the second is the five digit odometer reading of a car iii) the third is the amount of fuel put into the car on that date to fill the tank...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional int array. Your program must (1) read the name of the file from the command-line arguments, (2) open the file, (3) check that each line has the same number of characters and (4) check that the only characters in a line are ones and zeros. If there is no such command-line argument or no such file, or if any of the checks fail, your...
Introduction This program will read from a file containing inventory data, and display summary information to...
Introduction This program will read from a file containing inventory data, and display summary information to the user. Instructions and Information For this project, input will be from file, and output will be to standard output (the console). The file will be read, and the information stored in the appropriate data format and data structure. Then, the inventory data will be printed out, listing the frequency of each item next to its name. The frequency means the combined sum of...
Write a c program that creates a struct to be able to read a .img file...
Write a c program that creates a struct to be able to read a .img file and a .pat file.
Done in C++, Write a program to read the input file, shown below and write out...
Done in C++, Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets 6 Philadelphia 31, Tampa Bay 20 Green Bay 19,...
Write a program in Java that reads a file containing data about the changing popularity of...
Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file...
(Write/read data) Write a Program in BlueJ to create a file name Excersise12_15.txt if it does...
(Write/read data) Write a Program in BlueJ to create a file name Excersise12_15.txt if it does not exist. Write 100 integers created randomly into the file using text I/O. Integers are separated by spaces in the file. Read data back from the file and display the data in increasing order. After writing the file to disk, the input file should be read into an array, sorted using the static Arrays.sort() method from the Java API and then displayed in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT