Reputation: 351
here is the question
- Write a program that asks the user to enter the number of kilometres run by 10 different people (Person 1, Person 2, ..., Person 10). Use an array to save the input
- Once the data has been entered, the program must analyse the data and output which person run the most kilometres.
- Modify the program so that it also shows which person run the least number of kilometres.
I think I have the algorithm in place based on my code. but the challenge I have is that it returns weird values that obviously never existed in the array.
this is an example of the final answer
how many kilometre did Person 1 run ?
1
how many kilometre did Person 2 run ?
2
how many kilometre did Person 3 run ?
3
how many kilometre did Person 4 run ?
4
how many kilometre did Person 5 run ?
5
how many kilometre did Person 6 run ?
6
how many kilometre did Person 7 run ?
7
how many kilometre did Person 8 run ?
8
how many kilometre did Person 9 run ?
9
how many kilometre did Person 10 run ?
10
The Person with the longest distance is Person 10 and he / she covered a distance of 10 km
The Person with the shortest distance is Person 32767 and he / she covered a distance of 10 km
Here is my code:
#include <iostream>
using namespace std;
class kilometer_sort
{
// This method / function helps collect
// all user input of all distance ran by
// every persons 1-10
public:
void collect_user_input()
{
int no_of_persons = 10;
int array_of_persons_speed[no_of_persons];
int person_kilometre;
for (int b = 0; b < no_of_persons; b++)
{
cout << "how many kilometre did Person " << b + 1 << " run ?\n";
cin >> person_kilometre;
array_of_persons_speed[b] = person_kilometre;
}
check_for_longest_distance(no_of_persons, array_of_persons_speed);
check_for_shortest_distance(no_of_persons, array_of_persons_speed);
}
int person_with_longest_distance_array_index, person_with_shortest_distance_array_index;
public:
void check_for_longest_distance(int n, int array_of_persons_speed[])
{
int highest_no;
int longest_dist;
for (int i = 1; i < n; i++)
{
if (array_of_persons_speed[i] > array_of_persons_speed[0])
{
longest_dist = array_of_persons_speed[i];
person_with_longest_distance_array_index = i;
}
}
cout << "The Person with the longest distance is Person "
<< person_with_longest_distance_array_index + 1
<< " and he / she covered a distance of "
<< longest_dist
<< " km\n\n";
}
public:
void check_for_shortest_distance(int n, int array_of_persons_speed[])
{
int highest_no;
int shortest_distance;
for (int i = 1; i < n; i++)
{
if (array_of_persons_speed[i] < array_of_persons_speed[0])
{
shortest_distance = array_of_persons_speed[i];
person_with_shortest_distance_array_index = i;
}
}
cout << "The Person with the shortest distance is Person "
<< person_with_shortest_distance_array_index + 1
<< " and he / she covered a distance of "
<< shortest_distance
<< " km\n\n";
}
};
int main()
{
// Instantiate Class that sort distance
kilometer_sort ks;
ks.collect_user_input();
return false;
}
I will appreciate if someone can help.
Upvotes: 0
Views: 167
Reputation: 1266
The condition of this line is never true for your inputs:
if (array_of_persons_speed[i] < array_of_persons_speed[0])
As a result the person_with_shortest_distance_array_index is never set.
So the value you are outputting is the contents of an uninitialized value (plus one)
You could try using std::max_element and std::min_element or get both at the same time with std::minmax_element instead.
Upvotes: 0
Reputation: 1330
You just need to take care of your internal variable denoting max/min amount as follows:
public:
void check_for_longest_distance(int n, int array_of_persons_speed[])
{
int longest_dist = array_of_persons_speed[0];
for (int i = 1; i < n; i++)
{
if (array_of_persons_speed[i] > longest_dist)
{
longest_dist = array_of_persons_speed[i];
person_with_longest_distance_array_index = i;
}
}
cout << "The Person with the longest distance is Person "
<< person_with_longest_distance_array_index + 1
<< " and he / she covered a distance of "
<< longest_dist
<< " km\n\n";
}
void check_for_shortest_distance(int n, int array_of_persons_speed[])
{
int shortest_distance = array_of_persons_speed[0]
for (int i = 1; i < n; i++)
{
if (array_of_persons_speed[i] < shortest_distance)
{
shortest_distance = array_of_persons_speed[i];
person_with_shortest_distance_array_index = i;
}
}
cout << "The Person with the shortest distance is Person "
<< person_with_shortest_distance_array_index + 1
<< " and he / she covered a distance of "
<< shortest_distance
<< " km\n\n";
}
Upvotes: 1