how to write the avreage velocity to the screen.

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;

for(int k=0;k<9;k++)
cout<<t[k]<<endl;

for(int k=0;k<9;k++)
for(int i=0;i<9;i++) {
vort=(t[k]*v[i]);
cout<<vort<<endl;
}



getch();
}
I tried a suitable code for this question but it is not enough.can anybody help me please?
the average velocity is found by the adding up all the numbers in v , and dividing by how many of them there are.

vort=(t[k]*v[i]);
I've no idea what you're calculating here.
Last edited on
#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;



for(int i=0;i<n;i++) {
cout<<dizi[i]<<endl;
vort+=dizi[i]/9;

}
cout<<vort<<endl;






getch ();
return 0;
}
what is the wrong in my code;
this program is running but I am not sure the total calculating is not true
You should use the coding format.
 
std::cout << "Hello\n";


Have you tried single-stepping through your code? What IDE are you using? Do you know how to use a debugger?
1
2
3
4
5
6
for(int i=0;i<n;i++) {
cout<<dizi[i]<<endl;
vort+=dizi[i]/9;

}
cout<<vort<<endl;


You should find the total, then divide by the hours.
1
2
3
4
for(int i = 0; i < n; i++) {
   vort += dizi[i];
}
cout << "Average speed = " <<  (vort / n) << endl;
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.
thank you so much
Registered users can post here. Sign in or register to post.