[C++] L2 Computing delete array part


SUBMITTED BY: viceversa

DATE: Nov. 24, 2015, 4:34 p.m.

FORMAT: Text only

SIZE: 2.5 kB

HITS: 716

  1. if(pc.readable() == 1) // Check if received data
  2. {
  3. // Case 101 being to clear all arrays
  4. pc.scanf("%d", &interrupt); // Receive input
  5. if(interrupt == 101) // If 101, clear arrays
  6. {
  7. pc.printf("\nDeleting arrays...\n");
  8. total_capture_no = 0;
  9. free (sched_time_at_start_ptr);
  10. free (sched_end_time_ptr);
  11. free (sched_count_rate_ptr);
  12. free (sched_running_time_ptr);
  13. }
  14. else if(interrupt < 0) // Error check as you can't have a negative element of the array
  15. {
  16. pc.printf("\nError! Please choose a number that corresponds to a relevant array element\n");
  17. }
  18. else if(interrupt > total_capture_no) // Error check so input reference isn't bigger than the array
  19. {
  20. pc.printf("\nError! Please choose a number that corresponds to a relevant array element\n");
  21. }
  22. else // Only remaining numbers correspond to a specific element in each of the arrays
  23. {
  24. // Sets all of the values in that array element to 0
  25. sched_time_at_start_ptr[interrupt] = 0;
  26. sched_count_rate_ptr[interrupt] = 0;
  27. sched_running_time_ptr[interrupt] = 0;
  28. sched_end_time_ptr[interrupt] = 0;
  29. // Shifts each of the array elements located to the right one place to the left
  30. for(int i = interrupt; i < total_capture_no; i++)
  31. {
  32. sched_time_at_start_ptr[i] = sched_time_at_start_ptr[i+1];
  33. sched_count_rate_ptr[i] = sched_count_rate_ptr[i+1];
  34. sched_running_time_ptr[i] = sched_running_time_ptr[i+1];
  35. sched_end_time_ptr[i] = sched_end_time_ptr[i+1];
  36. }
  37. total_capture_no = total_capture_no - 1; // Makes the array one smaller to account for the deleted elements
  38. }
  39. }

comments powered by Disqus