int parkingTickets[] = {3,6,7,4,8,10,0};
int ndays = 7;
for(k = 0; k < ndays; k++) {
if (parkingTickets[k] > parkingTickets[ndays]) {
mostTickets = parkingTickets[k];
}
}
The problem with this solution is that you did not initialize the mostTickets variable and you have no else clause. This code will work for you.
int parkingTickets[] = {3,6,7,4,8,10,0};
int ndays = 7;
int mostTickets = -1;
for(int k = 0; k < ndays; k++) {
if (parkingTickets[k] > mostTickets) {
mostTickets = parkingTickets[k];
}
}
After that, mostTickets will contain the value of the largest number in the array. This solution will take O (n) to complete, as we iterate over the array and some work for comparison.
source
share