A file called speed.txt contains 9 time (h) vs speed (km/h)
measurements of a car.Write a program to read data into a arrays
and output the avreage velocity to the screen.
1 25
2 23
3 24
4 28
5 27
6 26
7 20
8 22
9 24
#include <iostream>
#include <conio.h>
using namespace std;
int main(){
const int n=9;
int v[n]={25,23,24,28,27,26,20,22,24};
int t[n]={1,2,3,4,5,6,7,8,9};
int vort;
for(int i=0;i<9;i++)
cout<<v[i]<<endl;
#include <iostream>
#include<conio.h>
#include<cmath>
using namespace std;
int main () {
const int n=9;
int dizi[n]={25,23,24,28,27,26,20,22,24};
int t[n]={1,2,3,4,6,7,8,9};
double vort=0.0;
The algorithm you have, although inefficient, is correct. The one alonso12 provided is better. But the underlying problem with your code is that your doing integer division, but you should be doing floating point division. If you're learning C++ it's worthwhile to understand this issue. For example, the following slight modification to your code will illustrate the problem:
1 2 3 4
for(int i=0;i<n;i++) {
cout<<dizi[i]<<endl;
vort+=dizi[i]/9.0; //note divide by 9.0 not 9
}
This will coerce the compiler to perform floating point arithmetic.