The problems objective is to write a program that takes a number of data sets the user would like to enter and then within the data sets it counts how many miles you traveled. For example for the first sample input 20 mph for the first 2 hours, 30 for 2nd – 6th hour and 10 for the seventh hour. The input sequence is the n number of data sets and the the s (speed/mph) and t(time). -1 ends the sequence
Sample Input
(3
20 2
30 6
10 7)
(2
60 1
30 5
4)
15 1
25 2
30 3
10 5
-1
Sample output
170 miles
180 miles
90 miles
`#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
int n, s, t, timeDiff, newTime, sum, arrSum; //variables
int arr[100]; //array
int count = 0; //counter, which i haven’t used yet but have been in the habit of doing
while (n != -1){
scanf("%d", &n); //Number of data sets to be entered
for(int i=0; i<n ;i++){
timeDiff = t; //saves last time value
scanf("%d %d", &s, &t);
newTime = t - timeDiff; //subtracts last time value from the new
if (timeDiff == 0){
sum = s * t;
arr[i] = sum;
}
else{
sum = s * newTime;
arr[i] = sum;}
}
for (int i = 0; i < n; i++){
arrSum = arrSum + arr[i];
}
printf("You went %d miles n", arrSum);
sum = 0;
timeDiff = 0;
arrSum = 0;
newTime = 0;
for (int i = 0; i < n; i++){
arr[i] = 0;
}
count++;
}
return 0;
}
`
When I initially spotted the probelm I though it was because my array was filled so I had to empty it hence the for loop at the end (tbh im not even sure that’s entirely correct). I’m a relatively new programmer to c and have been trying to adjust, but it’s proved difficult to say the least.
Maxwell Scharkopf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.